I'm programming c# GUI and I have 2 forms.
Form1 is my main form, and it has a button to open form2.
When the button in form1 is being clicked, I hide form1, create a new object of form2 and show form2.
I have a back button in form2. I want the behavior of this button to close form2, and show again the hidden form1.
How can I do it?
Have your form1 subscribe to the VisibleChange event of form2 and act accordingly. It will have to "remember" whether form2 is visible or hidden (or query it directly).
The alternatives are:
Your form2 will need a reference to form1.
This can be done in a number of ways - passing it in a constructor parameter, adding a property and assigning form1 to it.
Either of these ways will tightly couple these forms to each other (bad thing).
Have you tried
Form1.Visible = true;
Form1.Activate();
And then in the Form1 VisibleChanged Event you simply write
Form2.Close();
Related
Currently I have two forms(Form1 (base form) and Form2), from form1 I open form2, I want form1 to be inaccessible (unable to click buttons and access any other objects on that form) while form 2 is still open, and once form2 is closed I can access form1 again, I don't know what this behavior is called
I know how to put the form always on top via newForm.TopMost = true and how to check if a form is open via Application.OpenForms.OfType<Alert_Form>().Any(), Anyone know the sniplet I need to achieve what I want for form1 and possible (any other form)
Thanks and cheers
You simply call ShowDialog on your Form2 instance rather than Show. That will display a modal dialogue, which is the behaviour that you describe.
I have 2 windows forms (Form1 and Form2). (C#)
In Form1 I have a Text and a LinkLabel. When I click the LinkLabel in Form1, Form2 shows.
In Form2, there is a TextBox and 2 buttons (Ok and Cancel).
What I am trying to do is have the string from the Text in Form1 show up in the TextBox in Form2. The user will then be able to edit the Text in the TextBox, and can Press the Ok button to confirm the changes or press the Cancel button to discard the changes. If the Ok button is pressed, Form2 will close and the Text in Form1 will be changed to whatever was entered into the TextBox from Form2.
Pass the string into Form2's cstor. Form1 has a reference to the Form2 object because it constructs Form2. Add a property to Form2. Set the property and read the property after Form2 closes in Form1. Conditionally use the value to set the textbox value in Form1 based on the DialogResult.
Might be useful from MSDN
http://msdn.microsoft.com/en-us/library/aa288422(v=vs.71).aspx
And for form to show:
Form2 frm = new Form2();
frm.Show();
Similar question to others... Here's some solutions I've offered in the past...
Option 1
Option 2
I'm wondering today if there is any way to show a messagebox with a separate form as it's parent. As an example, say I have Form1 and Form2. From Form1, how do I call a messagebox to show with Form2 as the parent? In my case, Form2 is dynamically created, if that makes a difference.
You can pass a Form instance as the first parameter to MessageBox.Show().
I have a program with two forms, but Form1 is opening up over Form2, which isn't good since you need to use Form2 first. How can you force focus to Form2 when the application runs?
Use ShowDialog for Form2 instead of Show.
You can also use Application.OpenForms property to get the already opened form Form2 and then call its Focus method like:
if(Application.OpenForms["Form2"] != null)
Application.OpenForms["Form2"].Focus();
Let's say I have two forms: Form1 and Form2. Form1 has a textbox, Textbox1 and Button1.
When Button1 is clicked, a new instance of Form2 is created and shown. If Form2 needs to access Textbox1, how should expose it? Should Form2 have a public variable to hold a reference to the textbox? Or, when Form2 closes, should it invoke some public method on Form1 that updates the textbox? Thanks for your advice.
You should probably add a public property to the first form that exposes the textbox's text.
However, much more importantly, you should name your controls and forms.
There is (almost) nothing worse than a form with controls textBox1, button1, comboBox13, checkBox37.
If Form2 needs the text box value from Form1 when it loads, I would add the value to Form2's constructor method and pass it in that way.
If Form1 needs to obtain a new value entered in Form2 you can create a delegate with an Event that passes the value back to an assigned event handler in Form1.
Creating a public property may be the fastest solution, but I would try to stay away from circular references between forms if that is the case.
Hope this helps!