How can I hide Form2 using a button from Form1? - c#

I have two windows forms, Form1 and Form2. In Form1 I have two buttons: "show form 2" and "hide form 2". When I press "show form 2", it shows the second form. But I don't know how to hide the second form when I press "hide form 2". Any ideas?

If Form2 is open you can access it by:
Form form2 = Application.OpenForms["Form2"];
And then inside button click event, call:
form2.Hide();

Related

Passing parameter among three forms C#

I have application which has three form: 1, 2, 3
- Form1: Always visible and has a button btnLogin. When I click on btnLogin, Form2 is opened.
- Form2: has 2 buttons: btnSearch and btnAddNew. When I click on btnAddNeww, Form3 is opened.
- Form3: has two text box and a button: txtA.text, txtB.text and btnOK. If I click on btnOK, two string in two text boxs will pass to Form1.
How can I do that, please help me!
Make your textboxes access modifiers in Form1 public (you can see this on properties of textbox):
Then you can call them directly using:
Form1 frm1 = Application.OpenForms.OfType<Form1>().Take(1).SingleOrDefault();
frm1.txtBox1.Text = txtA.Text + txtB.Text;

Word loses focus (minimized) after closing non-modal form

I have a problem with the Word Add-in I'm developing.
The problem:
If I open non-modal form called Form1, and inside this form I press a button that opens another modal form called Form2. Now I close both forms, but Word windows is losing focus and minimized.
Note that this is not happening when I just open Form1 and close it.
When I open a non-modal form (let's call it Form1) by using Form1.Show(IWin32Window);
where the IWin32Window object is created by this method:
public static MyWin32Window getWordWindow()
{
IntPtr wordWindow = NativeMethods.FindWindowW("OpusApp", Globals.ThisAddIn.Application.ActiveWindow.Caption + " - Microsoft Word");
MyWin32Window myWin = new MyWin32Window(wordWindow);
return myWin;
}
The problem occurs only when I open another form from Form1, let's say Form2 is opened, but Form2 is modal form and opened with:
Form2.ShowDialog();
It works fine, but when I close Form2 and then Form1, the Word window is minimized. How can I prevent this behavior?
I also tried setting Form1 as the owner of Form2, like this:
Form2.ShowDialog(this);
but same result.
I found that I was able to resolve this problem by using
Form.Show();
instead of ShowDialog().

Pass a value to and from two different forms C#

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

Better way to Display forms one on top of another

I have a Main form.I want to launch another form from it and launch another from the launched form.I want to ensure that the Main form is not editable when sub forms are displayed so i use showdialog()
Mainform>(Showdialog)>form1>(showDialog+dispose)>form2(dispose)>Mainform
From Mainform i call form2.ShowDialog() then from form2 i use the following code to launch another form
this.visible=false;
form3.showdialog();
this.dispose();
But there are some problems in this.Is there a better way to achieve what im looking for?
edit:more description
I have a Main form,User clicks a Button on Mainform>Form1 is lauched>User clicks a Button in Form1>Form 2 is lauched(diposing/hiding form1) after form2 is closed Mainform should be brought to front and made editable,until then all other forms should be on top of Mainform and Mainform should be un-editable
The problem is that you have to specify the MainForm as the parent for (both) form2 and form3. When you use the overload of ShowDialog that has no parameters, WinForms uses the active form as the parent, so form3's parent becomes form2 automatically. You are then trying to close/dispose form2 causing form3 to become orphaned.
There are several options for getting the reference to MainForm, but the simplest is to use:
form2/3.ShowDialog(Application.OpenForms["MainForm"]);
Assuming that you have set the Name property on MainForm to "MainForm".
In your code, this.dispose() is executed only after form3 is closed. What i think you want is to close form2 after form3 was closed, so you can call this.Close() instead of this.Dispose().
this.visible=false;
form3.showdialog();
this.Close();
Or maybe, after form3 is shown up you dont need form2 any more. That meas:
this.visible=false;
//show instead of showdialog so it wont wait until form3 is closed
form3.show();
this.Close();
It looks like you are trying to implement something like a wizard. The best solution would be to launch all the child forms sequentially, in the main form.
If you need to pass data along the sequence, you should pass it from each dialog to the main form, which then passes it to the next dialog.
MainForm:
Form1 f = new Form1();
if (f.ShowDialog(this) == DialogResult.OK) {
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
Form1 (button click which will open the form 2):
button1_click(object sender, EvengArgs e) {
this.DialogResult = DialogResult.OK;
Close();
}

c# gui - navigate between forms

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();

Categories

Resources