I have multiple form application. Form 1 is Login form for user validation. Form1 goes to Form2(Menu form). Form 2 leads to Form3 which is only popupform and hides the form2 when it is open.Form 3 goes to Form 4. Now from Form4 ,with button click, I need to restore the Form2 without creating a new instance. I tried using singelton approach, getting error.Code as follows as described above.
Form1:
private void click_Click(object sender, EventArgs e)
{
if ((user.Text == username) && (pswd.Text == password))
{
Form2 menu = new Form2();
menu.Username = user.Text;
//hides the form1
this.Hide();
menu.ShowDialog();
}
}
Form2:
private static Form2 instance;
public static Form2 Instance
{
get
{
if (instance == null)
{
instance = new Form2();
}
return instance;
}
}
private void button_Click(object sender, EventArgs e)
{
//Hide the form2
Hide();
//Bring up your PopUp form
using (Form3 form3 = new Form3())
form3.ShowDialog();
}
Form3:
private void button_Click(object sender, EventArgs e)
{
Hide();
Form4 form4 = new Form4();
form4.Show();
}
Form4:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.Instance;//error occured as Mainmenu does not contain a reference for Instance and no extension method accepting a first argument of type 'Mainmenu'
}
Basically, I want to restore the Form2 that was hided.Any help would be appreciated!Thanks
How about keeping track of the Forms parents so you have direct access.
For example in your Form1:
menu.Parent = this;
Hide();
menu.ShowDialog();
Then in your Form2 when Form3 is called:
using (Form3 f3 = new Form3())
{
Hide();
f3.Parent = this.Parent;
f3.ShowDialog();
}
Then when you want to dispose of Form3:
this.Parent.Show();
this.Parent.Focus();
this.Dispose();
Something along this lines for as many open forms as you need. If you need to drop back to the previous form, then make the Parent 'this' instead of 'this.Parent'
Related
I designed a form for login that named as form1, and have 2 more form form2,form3
form2 items showing in panel from form1
and what I want to do
when I click the button in panel ( the item from form2 ) want to show form2 and hide form1 but the code isnt working
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
Form3 frm3 = new Form3();
frm1.Hide();
frm3.Show();
};
form3 is opening but form1 isnt hiding
Its not hiding because you created a new instance for form1 which is already instantiated.
You must call the Hide() method on the same instance used to call the Show() method.
If you added this code inside form1 class ,then change it like this
private Form1 frm1
public Form2()
{
frm1 = new Form1()
}
private void button_show_form1_Click(object sender, EventArgs e)
{
frm1.Show();
};
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
frm1.Hide();
};
You creating both forms in your codesnipped. Form1 is not the form you want to close, i think. frm1 is only another instance of Form1, but not the openend instance von Form1. You must have anywhere another instance of Form1. You must use the right referenz to the right instance.
It is important to know that WinForms create an instance of the startup form. In our case the startup form would be Form1. So, when you say
Form1 frm1 = new Form1();
You're actually creating a new (second) instance of Form1. This means that, in code, there are two different Form1's.
What we want to do is check with our application to get the instance of Form1 that already exists.
// This goes in Form2. It returns an instance of Form1, if it exists.
private Form getForm1()
{
// Application holds information about our application, such as which forms are currently open.
var formCollection = System.Windows.Forms.Application.OpenForms;
// Now we loop through the open forms in search of the form we want, Form1.
foreach (Form frm in formCollection)
{
if (frm.Name.Equals("Form1"))
{
return frm;
}
}
return null;
}
Now that we can get the existing instance of Form1 we can use it to make the form hidden.
private void button1_Click(object sender, EventArgs e)
{
var form1 = getForm1();
if (form1 != null) form1.Hide();
}
Something to know here is that when a form is hidden it is not closed. So, we need to make sure that Form1 becomes visible again. For example, we can set Form1 to be visible when Form2 closes.
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
// The question mark (?) checks to see if the result of
// getForm1() is null. Same thing that is happening in
// button1_click
getForm1()?.Show();
}
Complete Form2 Code
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm1 = getForm1();
var frm3 = new Form3();
if (frm1 != null) frm1.Hide();
frm3.Show();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
// The question mark (?) checks to see if the result of getForm1() is null. Same thing that is happening in button1_click
getForm1()?.Show();
}
// This goes in Form2. It returns an instance of Form1, if it exists.
private Form getForm1()
{
// Application holds information about our application, such as which forms are currently open.
// Note that Open and Visible have different definitions.
var formCollection = System.Windows.Forms.Application.OpenForms;
// Now we loop through the open forms in search of the form we want, Form1.
foreach (Form frm in formCollection)
{
if (frm.Name.Equals("Form1"))
{
return frm;
}
}
return null;
}
}
Let’s I have MDIParentForm (Home), Child Form (Form1, Form2), Home has one Menu (Add) and Form1 has button (btnOk)
'''''Here is C# Code''''''
public partial class Home : DevExpress.XtraBars.Ribbon.RibbonForm
{
public Home()
{
InitializeComponent();
}
public void CreateForm(Form frm)
{
frm.MdiParent = this;
frm.WindowState = FormWindowState.Maximized;
frm.Show();
}
private void btnAddForm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Form1 frm = new Form1();
CreateForm(frm);
}
}
//Up to this 1st level my code works fine.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDone_Click(object sender, EventArgs e)
{
//From Here I am not able show the Form2.
Home MDIParentForm = new Home();
Form2 frm = new Form2();
MDIParentForm.CreateForm(childForm);
}
}
Action: On the click of Add Menu I used to open Form1 (It works fine) and on the click of btnOk I want to open Form2 (which is another MDIChild form).
Let me know how we can accomplish this task.
What you do is you create yet another instance of your MDI parent and you set the Form2 as a child of this newly created MDI parent. This is clearly wrong, you want both to be children of the very same parent.
Either do this directly:
private void btnDone_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
// both have the same MDI parent
frm.MDIParent = this.MDIParent;
frm.Show();
}
or, if you insist on reusing your CreateForm
private void btnDone_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
((Home)this.MDIParent).CreateForm( frm );
}
Both approaches depend on this.MDIParent set in form1 and make sure the same instance is used for newly created form2.
After open FORM 2.dialog from FORM 1, I want to close FORM 1 via button from FORM 2.
FORM 1
private void btnaddIPrange_Click(object sender, EventArgs e)
{
new form2().ShowDialog();
}
FORM 2
private void btnIPRangeCancel_Click(object sender, EventArgs e)
{
//close FORM 1(I don't know the code to close it)
this.Close();
}
Form2 will need a reference to Form1. You can do that several ways.
For example, in Form1 you set the Owner property of the new Form2 instance to this:
private void btnaddIPrange_Click(object sender, EventArgs e)
{
Form2 myForm = new Form2(); // Creates instance of Form2.
myForm.Owner = this; // Assigns reference to this instance of Form1 to the Owner property of Form2.
myForm.Show(); // Opens Form2 instance.
// You can also call myForm.Show(this);
// instead of the above two lines to automatically assign this form as the owner.
}
Then in Form2:
private void btnIPRangeCancel_Click(object sender, EventArgs e)
{
if(this.Owner != null) // Check for null.
this.Owner.Close(); // Closes Form1 instance.
this.Close(); // Closes current Form2 instance.
}
If all your forms are members of the same parent form, you can just call:
var ParentalForm = this.ParentForm as Foo_MainForm;
Make sure the child forms are public/internal members on the form.
Then:
ParentalForm.Foo_FormWantingClosed.Close();
Or in just one line:
(this.ParentForm as Foo_MainForm).Foo_FormWantingClosed.Close();
Off the top of my head.
Another idea! Since the form1 is sender, you can cast the object as form1 and close it directly. For instance:
private void OpenForm2(object sender, EventArgs e)
{
var callingForm = sender as form1;
if (callingForm != null)
{
callingForm.Close();
}
this.Close();
}
My Winform application has 3 form: MainForm, Form1 and Form2.
MainForm has an UserControl; when application starts, it calls MainForm, MainForm will be loaded and added Form1 onto UserControl:
private void MainForm_Load(object sender, EventArgs e)
{
Form1 frm = new Form1() { Dock = DockStyle.Fill, TopLevel = false, Visible = true };
xtraUserControl1.Controls.Add(frm);
}
On Form1, I use delegate:
public delegate void Tranferdata(string txt);
public Tranferdata _tranfer;
private void Gettxt(string txt)
{
tbx_Recieve.Text = txt;
}
Form1 has a button to call Form2:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog(this);
}
Form2 will send value to Form1 after closed, Code on Form2:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
using (Form1 frm1 = (Form1)this.Owner)
{
frm1._tranfer(tbx_Numb.Text);
}
}
private void button1_Click_1(object sender, EventArgs e)
{
this.Close();
}
But my code doesn't work correctly. It crashed at 'using (Form1 frm1 = (Form1)this.Owner)' and show the System.InvalidCastException.
How can I fix this?
The reason it crashed on that line is because you didn't set Form1 to be the owner of Form2.
To fix, first have Form1 as a class variable and not as a local variable:
Form1 form1;
private void MainForm_Load(object sender, EventArgs e)
{
form1 = new Form1() { Dock = DockStyle.Fill, TopLevel = false, Visible = true };
xtraUserControl1.Controls.Add(form1);
}
Then you can use it to assign Form2's owner when you create it:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Owner = form1;
frm2.ShowDialog(this);
}
And also, like Ron Beyer mentioned in his comment, consider removing the using statement, it will close Form1, it doesn't sound like that's what you want to do.
Now, while that approach will work, there are some questions that you should consider here:
Why did you choose to use the "Owner" property in the first place? Do you really need it? Having Form1 as the owner of Form2 means that Form2 will close when Form1 is closed. Since you use ShowDialog on Form2 it will block the user from closing Form1 while Form2 is showing so it seems unneeded.
If the reason for using the Owner property is just for the sake of using the delegate than you could have just added a property to Form2 to be of type Form1, which would have given you type safety and superior code.
But there is even a better way: MainForm can register to the Closed event for Form2 and call the method on Form1. This will remove unneeded dependencies (Form2 and Form1 will not know about each other at all) making the code healthier.
I found the solution to this problem:
In the Form 2:
public void SetParent(Form1 frm)
{
frm1 = frm;
}
and call it in Form 1 at event call form2:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.SetParent(this);
frm2.ShowDialog();
}
How to enable control of a form based in form it was loaded?
For example if Form3 was open from Form1 the button that will be enable in Form3 is button1 and if Form3 was open from Form2 the button that will be enable in Form3 is button2.
Yes you can, just set the Owner of the Form3 when you Show it, then in Form3's Load EventHandler check the Type of the Owner to determine which button to enable. Something like this should work.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show(this);
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm2 = new Form2(); //Show Form2 for Testing
frm2.Show();
}
}
Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show(this);
}
}
Form3
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
if (Owner == null) return; //Check to make sure there is an Owner
if (Owner.GetType() == typeof(Form1))
button1.Enabled = true;
else if (Owner.GetType() == typeof(Form2))
button2.Enabled = true;
}
}
I think if (typeof(ParentForm) == typeof(Form1)) will do what you want. Keep in mind this check is based purely on the type so if you have multiple instances of Form1 you could run into some problems.
What he means is if you have a form that is of type Form than you wouldn't know which form is the parent if you only look at types.
Form1 opens Form3 and both forms are of type System.Windows.Forms
Form2 opens Form3 and both forms are of type System.Windows.Forms
If all three forms have different types than you can check if types are different.
Otherwise you have to check names.
form1.Name = "form1";
form2.Name = "form2";
form3.Name = "form3";
You will open child forms from within form1 with
form3.ShowDialog(this);
In form3 you can check for parent form and check for it's name. If it's form2 do something otherwise do something else.