Displaying a panel in another windows form on button click c# - c#

I'm working on a c# program and I want a panel to appear on a form when a button is clicked in another. So when the add button is clicked on form2 the panel requesting the details for this to be possible will be displayed on form 1.
I currently have a static method set up in form1 which can be accessed from form2 - however due to panel.Show() being non static it won't allow me to use this in the function.
In Form1 I have:
public static void showPanel()
{
panel.Show()
}
In my second form I have the following:
private void btn_add_Click(object sender, EventArgs e)
{
form1.showPanel();
this.Hide();
}
I have tested with just having the static function show a message box which works. Is it possible to do it the way I want or do I need to take a few steps back and try a different technique?

Are we talking about a new instance of the second form if so you can try to instantiate the new form using:
Form newForm = new YourFormName(potential parameters);
newForm.showPanel();
newForm.Show();
If you want to execute the form on an open form you can give the first form a reference(field with instance) of the second form. Or you can try using: Application.OpenForms. if you give it [1] it'll give you the second open form probably. You can also use .OfType to get the correct form in case your form order isn't always the same.

Related

How can I open a WinForm multiple times?

I tried opening a second form using a button on my main form, but when I close the second window, I can't open it again.
I added the following code to my main form:
settings_window secondForm = new settings_window();
private void settings_button_Click(object sender, EventArgs e)
{
secondForm.Show();
}
But when I try to open the second form named settings_window the second time, I get the following error: System.ObjectDisposedException.
I found the following code to fix this but I don't know where to place it:
private void settings_window_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true; // Do not close the form.
}
You can avoid storing references of Forms and use a simple generic method that shows a Form when an instance of it already exists or creates a new one (and shows it) when none has been created before:
private void ShowForm<T>() where T : Form, new()
{
T? f = Application.OpenForms.OfType<T>().SingleOrDefault();
if (f is null) {
f = new T();
f.FormClosing += F_FormClosing;
}
f.Show();
BeginInvoke(new Action(()=> f.WindowState = FormWindowState.Normal));
void F_FormClosing(object? sender, FormClosingEventArgs e)
{
e.Cancel = true;
(sender as Form)?.Hide();
}
}
When you need it, call as ShowForm<SomeFormClass>(), e.g.,
ShowForm<settings_window>()
Note:
This code uses a local method to subscribe to the FormClosing event.
You can use a standard method, in case this syntax is not available.
BeginInvoke() is used to defer the FormWindowState.Normal assignment. This is used only in the case you minimize a Form, then right-click on its icon in the TaskBar and select Close Windows from the Menu. Without deferring this assignment, the minimized Form wouldn't show up again.
When the starting Form closes, all other Forms close as well
This code supposes nullable is enabled (e.g., see object? sender). If nullable is disabled or you're targeting .NET Framework, remove it (e.g., change in object sender)
Is secondForm a private field of the main form class?
It should work then.
Alternative solution is to show it as as modal - ShowDialog()
Also if you want to save some data in your second form, just use some data initialization from constructor, then saving to first/parent form.
I think you need to create a new instance of the form each time you want to open it. It will create a new instance of the settings_window form each time the button is clicked.
private void settings_button_Click(object sender, EventArgs e)
{
// Create a new instance of the form
settings_window secondForm = new settings_window();
secondForm.Show();
}
Your code shows a class that you have named settings_window, which gives us a hint about what its intended use might be. In general, for a form that behaves "like" a settings window, that you can call multiple times using the same instance, you can declare a member variable using this pattern:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// Provide some means, like a menu or button, to show 'secondForm'
settingsMenu.Click += onClickSettingsMenu;
// Dispose the settings_form when the MainForm does.
Disposed += (sender, e) => secondForm.Dispose();
}
// By instantiating it here, its public default or persisted
// properties are immediately available, for example even
// while the main form constructs and loads the initial view.
settings_window secondForm = new settings_window();
private void onClickSettingsMenu(object? sender, EventArgs e)
{
if(DialogResult.OK.Equals(secondForm.ShowDialog()))
{
// Apply actions using the properties of secondForm
}
}
}
This is suitable for any form when you want to:
Repeatedly show and hide the form (e.g. a "Settings" form where the user can change the options multiple times).
Retrieve the default or the persisted properties of the form from the outset even if it's never been shown.
Use any of the form's public properties (e.g. GameLevel, SortType etc.) at any given moment while the app is running, even if the form isn't currently visible.
Display the form modally meaning that "no input (keyboard or mouse click) can occur except to objects on the modal form".
The reason it works is that calling ShowDialog (unlike calling Show) intentionally does not dispose the window handle, and this is to support of this very kind of scenario. The app is then responsible for disposing the resource when the app closes.
The Microsoft documentation for ShowDialog explains how this works.
Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

Can I have different form_loads inside a form that can be assigned to a specific event/action? C#

I'm making a windows form app and I have one form that represents a base form and it gets data from a different class (that's almost like a database):
private void base_form_Load(object sender, EventArgs e)
{
database_class dc = new database_class();
button1.Text =dc.Name;
}
NOTE: The reason why this code is in the form_Load is because it doesn't show up, unless I put it there, which I find strange, but it might not be?
I have a main form, that acts like a menu - it has four buttons on it and all the buttons lead to the base form. The database class is actually supposed to change the names of the controls in the base form based on the what button is chosen in the main form(menu). The base form has a lot more buttons than the main form.
Since this is confusing, here's an example of what I want to do: If the menu had options (buttons) Mozart, Beethoven, Liszt, Chopin - when people click on Mozart they're supposed to get buttons with the names of his compositions, if they click Beethoven then they get his compositions and so on.(These buttons in the base form do lead to something else, if that's important/helpful). The reason I'm not making separate forms for these menu buttons, is because I have a lot of buttons and I don't think making plenty of forms is ideal (it's a simple app, I don't want to slow it down with a lot of forms).
My question is what is the best way to do this? Do I have to somehow assign the data I want to the button (mouse) click events in the menu (main form)? Is there a possibility of having different form loads in the base form, that can be assigned to the mouse clicks in the menu?
Thank you for your time.
Example constructor as requested - form has a richTextBox that gets populated by the constructor and also a string value that's used later:
public partial class Form2 : Form
{
public string _filename;
public Form2(string text, string filename)
{
InitializeComponent();
richTextBox1.Text = text;
_filename = filename;
}
}
Create an instance of this form from the main form:
private void button1_Click(object sender, EventArgs e)
{
string textForRTB = "Some text";
string valueForFilename = "\some\file\name";
Form2 frm2 = new Form2(textForRTB, valueForFilename);
frm2.Show();
}

Detect CloseEvent through different forms

PROBLEM SOLVED
SHORT STORY
I want to detect "FormClosing()" event through different forms, ie, when form1 is closed that is instantiated within form2, can form2 detect when user presses exit in form1?
LONG STORY
My team and I are working on a windows form application. Project has two forms: one is the main form page and the other is accessed via this main form. Main form looks like this:
And the second one looks like this:
If you press "Ekle/Sil" buttons within the main form, you are directed to form 2 where you can edit database entries. When you press "Sayfayı Yenile" button in the main form, the content of the text areas are refreshed by re-fetching entries from the database.
My problem is, I want to automatically refresh the main form when the user closes the second form. My research suggests I should use an "FormClosing()" event to detect a closing form. However, I want to detect this from the main form. Instantiating main form in second form's source code doesn't seem to be a reliable solution. Anyone can tell me how to do this?
EDIT
I solved the problem:
1) Created a public method within the main form that refreshes the page.
2) Send "this" property from the main form when creating the second form.
3) Added an "FormClosed()" handler within the second form that invokes this public method.
Still, I'm looking for a better solution.
EDIT 2
Better solution InBetween's answer
Simply use the Form.Closed event of the new child windows form. Everything is handled from the main form:
void EkleSil_Clicked(object sender, EventArgs e) //or whatever method is called when button is clicked
{
var newChildForm = new ChildForm();
newChildForm.Closed += childFormClosed;
newChildForm.Show();
}
void childFormClosed(object sender, EventArgs e)
{
((Form)sender).Closed -=childFormClosed;
updateShownData();
}
You can create a event in the second form and raise it when the form is closing .
Handle the event in the main form and refresh the main form when the event is raised
Another option would be to pass Form1 as an argument to Form2. Then use the Form.Closing event in Form2 and use the Form1 reference to trigger something.
Form1 form1Ref;
public Form2(Form1 mainform)
{
form1Ref = mainform;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.SomeMethod();
}

change active form to show another form

I have a Form1 and another one that I added. Form1 is being run by program.cs at the start. I need to hide Form1 and show options form by the press of a button.
private void submitPassword_Click(object sender, EventArgs e)
{
options optionForm = new options();
optionForm.Show();
}
the above code opens the options form on top, but I need it to replace the current form. how can I achieve this?
Hide current form using this.Close() before showing new one and make sure you are using parameterless Application.Run so program won't close when you close it's main form.
You can use "Hide" and "Show" method :
private void submitPassword_Click(object sender, EventArgs e)
{
options optionForm = new options();
this.Hide();
optionForm.Show();
}
private void submitPassword_Click(object sender, EventArgs e)
{
options optionForm = new options();
optionForm.Show();
this.Hide();
}
Similar solutions where one form calls and acts on another... Such as this one I answered for another. You could do a similar process... pass in your first form to the second... Then show the second... Then, you could HIDE your first form (via this.Hide() ). Then, in your second form, when you click whatever button to select your choice, and need to return back to the first form, you could then use the original form's reference passed INTO the second form to re-Show it, such as in the click on the second form...
this.PreservedForm.Show(); // re-show original form
this.Close(); // and CLOSE this second form...

Changing text of textbox on another form

Ok so
i have 2 form lets call them main and second forms
On main there is nothing but a textbox(lets call it T1) which is PUBLIC so it supposed to be reachable from any form.
On second there is nothing but a textbox(T2) which is public, and a button(pub)(call it B)
On the codes, there is nothing in main
On the codes of second there is
public string s1
and in codes of button B:
s1 = T2.Text;
MAIN mainredirect = new MAIN();
MAIN.T1.Text = s1;
and thats it. what i am doing wrong?
p.s: there is no error that shown by vs, so its not syntax error
Don't understand what you trying to achieve, but probably you forget to simply Show() created form.
EDIT:
Readed your comments. As i understand your main form opens second form like a dialog and you want to get entered value from it.
Code for your main form will be:
private void callSecondFormButton_Click(object sender, EventArgs e)
{
SecondForm second = new SecondForm();
second.ShowDialog();
mainFormTextBox.Text = second.Result;
}
For your second form:
public string Result = string.Empty;
private void secondFormCloseButton_Click(object sender, EventArgs e)
{
Result = secondFormTextBox.Text;
Close();
}
callSecondFormButton - button on the main form that calls your second form;
mainFormTextBox - text box on your main form;
SecondForm - your second form that will be called from main;
Result - public field of second form for retrieving result of entering text;
secondFormCloseButton - button on the second form that will update Result and close dialog.
In the main form need first to create second form instance and show form. After executing ShowDialog main form will wait for closing opened form. After closing it will retrieve resulted text.
Is this WinForms? It's a little hard to tell what you are trying to do. Have you stepped through with a debugger? Is the string getting set? How are you confirming it is not? Is it because the form is not yet loaded?
You are showing only snippets. It should be very easy for you to further isolate this using the debugger.

Categories

Resources