I am a newbiest in c# and window form
i am doing a project and i meet some problem
how can i navigate forms within the window( i have a menu strip, when click it will show a item "Brand", so when i click it, it should open up within the window , i don't want something using the mdiparent/container, i have form1 and form2, then i put the menu strip in form1, which there is some thing inside form1, if use the mdiparent/container, the form1 content/thing will block the form2 )
2.i use the below code and the problem is i want to close the form1 which i click on " Brand" in the menu strip...but how???
public partial class Form1 : Form
{
// i put the menu strip in form1 design
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Check_Click(object sender, EventArgs e)
{
Form2 Check = new Form2();
Check.Show();
}
}
You cannot just close the Form1 as it is the main form, but you can hide it. Use this.Hide().
private void Check_Click(object sender, EventArgs e)
{
Form2 Check= new Form2();
Check.Show();
Hide();
}
[EDIT]
Not sure if this is what is asked. But...
There are many ways to implement navigation between forms, for example:
In Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Tag = this;
form2.Show(this);
Hide();
}
In Form2:
private void button1_Click(object sender, EventArgs e)
{
var form1 = (Form1)Tag;
form1.Show();
Close();
}
I think you should create usercontrols rather than different forms. Then you can add your usercontrols in your main panel according to the selection in the menu.
Initially something like below
this.panel.Controls.Clear();
this.panel.Controls.Add(new UserControl_For_Form1());
Once the user click some other selection in menu.
this.panel.Controls.Clear();
this.panel.Controls.Add(new UserControl_For_Form2());
If you really want to use the way that you are using at the moment. Below code will help.
Add a Form1 property for the Form2 and parse the form1 instance to the Form2 with its constructor.
public partial class Form2 : Form
{
private Form1 form1;
public Form2(Form1 myForm)
{
InitializeComponent();
form1 = myForm;
}
}
Show the form2 and hide the form1.
private void Check_Click(object sender, EventArgs e)
{
Form2 Check= new Form2(this);
Check.Show();
Hide();
}
In form2 closing event now you can show the form1 instance which is in the form2 and close the form2.
Using of MDI form is another option for you.
It has been 7 years since this question was asked but I wanna give an answer in case if someone is still looking for a solution. If you are using DevExpress, you can add Navigation Frame to your program. You can switch between different components such as GridControl, GroupBox and so on. So you dont have to create an extra form in order to navigate between forms.
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;
}
}
I am trying to create a two-form Windows Application in C#. For simplicity and to help me figure it out before using it elsewhere, I have essentially created a Windows form application with two forms: Form1 and Form2, Form1 showing on startup. At the click of a button, I can get Form1 to "disappear" and Form2:
private void button1_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
x.Show();
this.Hide();
}
And this works great. However, when I want to return to Form1 (by making it visible again) and unload Form2, I am unsure how to proceed with coding Form2 to return the user to Form1 using a button click as well. I'm not sure what to refer to to make that form visible again, instead of having to create a new Form1 and loading it, thereby leaving my original startup form sitting in memory.
Any help you can provide would be awesome! Thanks in advance,
-Jan
As Alfie comment suggests, you need to control your instances of each form somehow.
I'd suggest a static class with two variables. When you start up you link the forms to these public properties in the static class.
something like this:
public static class App {
public static Form Form1;
public static Form Form2;
}
on startup or the click method, you'd say something like:
private void button1_Click(object sender, EventArgs e)
{
if (App.Form1 != null)
{
App.Form1 = new Form1();
}
App.Form1.Show();
App.Form2.Hide();
}
Do this:
private void button1_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
this.Hide();
x.ShowModal();
this.Show();
}
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.
I have two forms. First, Form1 has a group box, some labels and a listbox. I press a button and new Form2 is opened and contains some text. I want to transfer the text in Form2 to the listbox in the Form1.
So far, what I have done is make modifier of listbox to public and then put this code in the button of Form2
Form1 frm = new Form1();
frm.ListBox.items.Add(textBox.Text);
But amazingly, this does not add any value. I thought I was mistaken with the insertion so I made the same procedure. This time, I made a label public and added textbox value to its Text property but it failed.
Any ideas?
Try adding a parameter to the constructor of the second form (in your example, Form1) and passing the value that way. Once InitializeComponent() is called you can then add the parameter to the listbox as a choice.
public Form1(String customItem)
{
InitializeComponent();
this.myListBox.Items.Add(customItem);
}
// In the original form's code:
Form1 frm = new Form1(this.textBox.Text);
Let's assume Form1 calls Form2. Please look at the code:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
frm.VisibleChanged += formVisibleChanged;
}
private void formVisibleChanged(object sender, EventArgs e)
{
Form2 frm = (Form2)sender;
if (!frm.Visible)
{
this.listBox1.Items.Add(frm.ReturnText);
frm.Dispose();
}
}
}
Form2:
public partial class Form2 : Form
{
public string ReturnText { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.ReturnText = this.textBox1.Text;
this.Visible = false;
}
}
The answer is to declare public property on Form2 and when form gets hidden. Access the same instance and retrieve the value.
Below code working perfect on my machine.
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.listBox1.Items.Add(textBox1.Text );//ListBox1 : Modifier property made public
f1.ShowDialog();
}
Ok, If you are Calling Sequence is like, Form1->Form2 and Form2 updates the value of Form1 then you have to use ParentForm() or Delegate to update the previous form.
Form1 frm = new Form1();
frm is now a new instance of class Form1.
frm does not refer to the original instance of Form1 that was displayed to the user.
One solution is, when creating the instance of Form2, pass it a reference to your current instance of Form1.
Please avoid the concept of making any public members like you said
>>i have done is make modifier of listbox to public and then in form2 in button code<<
this is not a good practice,on the other hand the good one is in Brad Christie's Post,I hope you got it.
This code will be inside the form containing myListBox probably inside a button click handler.
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.myListBox.Items.Add(frm2.myTextBox.Text);
frm2.Dispose();