Now, I have two forms, called form1 and form2, in the form1 there's a button, when I click it, then open the form2
private void button1_Click(object sender, EventArgs e)
{
Ges.produit.Add(p);
Form3 f= new Form3();
f.dataGridView1.Rows.Clear();
foreach (var item in Ges.produit)
{
f.dataGridView1.Rows.Add(item.Id, item.Name, item.Qty, item.Prac, item.Prav, item.Disc);
}
this.MdiParent = f.MdiParent;
f.Show();
this.Hide();
}
Question: in the form2, I created a button when I click it, the form2 close and get back to form1 without opening another form1 . How to do?this
You can do this a few ways, one way is to pass a reference of the parent form to the child form in its constructor, and then when you close the child form call show against this reference.
Another way to accomplish this is when you create the child form inside the parent form, at the same time also wire up the FormClosed event of the child form to re-display the parent form.
The following is an example for both of these methods
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
var form2 = new Form2(this);
this.Hide();
form2.Show();
}
private void btnShowForm3_Click(object sender, EventArgs e)
{
var form3 = new Form3();
form3.FormClosed += (o, args) =>
{
this.Show();
};
this.Hide();
form3.Show();
}
}
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 form1)
:this()
{
_form1 = form1;
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
_form1.Show();
}
}
Related
I would like to ask for help. I would like to achieve that in Form1 in the panel I open Form2 in which there is a button, when the user presses in the panel on Form1 opens Form3
Here I am in the Form1 code where the user presses the button that is in the window, and thus Form2 opens in the panel
{
public partial class Form1 : Form
{
private Form activeForm;
public Form1()
{
InitializeComponent();
}
public void OpenSlaveForm(Form podrizenyForm, object btnSender)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = podrizenyForm;
podrizenyForm.TopLevel = false;
podrizenyForm.FormBorderStyle = FormBorderStyle.None;
podrizenyForm.Dock = DockStyle.Fill;
this.pnlMain.Controls.Add(podrizenyForm);
this.pnlMain.Tag = podrizenyForm;
podrizenyForm.BringToFront();
podrizenyForm.Show();
string nazev = podrizenyForm.Text.ToUpper();
}
private void btnForm1_Click(object sender, EventArgs e)
{
OpenSlaveForm(new Forms.Form2(), sender);
}
}
}
Here I am in Form2 and I try to open Form3 through the button in the Form1 panel
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnForm2_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.OpenSlaveForm(new Forms.Form3(), sender);
}
}
Thank you in advance for any help
You are creating a new Form1 instance when you press the button in Form2, that adds the new Form3 to that new instance instead in the one you are seeing on the screen.
You need to pass a reference to the current Form1 instance to Form2:
public partial class Form2 : Form
{
Form1 mainInstance;
//Add a new constructor which accepts a reference to Form1
public Form2(Form1 MainInstance)
{
mainInstance = MainInstance;
InitializeComponent();
}
public Form2()
{
InitializeComponent();
}
private void btnForm2_Click(object sender, EventArgs e)
{
//Now use that instance to create Form3
mainInstance.OpenSlaveForm(new Forms.Form3(), sender);
}
}
Now, change the button call in Form1:
private void btnForm1_Click(object sender, EventArgs e)
{
//Use the new constructor in order to pass the current instance
OpenSlaveForm(new Forms.Form2(this), sender);
}
I have a TreeView with dinamically created nodes.
I want to reach the nodes from other form, when i close the other form.
But it does not work.
Sample:
form1
//show the other form (form2)
private void button1_Click(object sender, EventArgs e)
{
using (Form1 form1 = new Form1())
{
using (Form2 form2 = new Form2(form1))
{
form2.StartPosition = FormStartPosition.CenterParent;
form2.ShowDialog();
}
}
}
//call this method from other form (form2) when close
internal void example()
{
MessageBox.Show(treeView1.Nodes.Count.ToString());
}
//create the nodes
private void Form1_Load(object sender, EventArgs e)
{
TreeNode node = new TreeNode("aaaa");
treeView1.Nodes.Add(node);
node = new TreeNode("bbbb");
treeView1.Nodes.Add(node);
node = new TreeNode("cccc");
treeView1.Nodes.Add(node);
}
form2
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
form1.example();
}
}
MessageBox.Show(treeView1.Nodes.Count.ToString());
result: 0
Problem is in this line:
using (Form1 form1 = new Form1())
you're sending new instance of Form1 to your Form2. You should send current instance of Form1, like this:
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
form2.StartPosition = FormStartPosition.CenterParent;
form2.ShowDialog();
}
}
You construct Form1 twice. Once to show a form with a button. When the button is pressed you create another instance of Form1. This second instance is given to Form2. But... the Load event of the second instance is never called, meaning its tree is never filled. The Load event is only called when the form is displayed, and this instance is never displayed.
I suggest you follow the answer of Nino.
Or... if you insist to have multiple instances of Form1, move the code which fills the tree, inside the constructor of Form1, not in the Load-eventhandler.
I have 2 form and I want change Form1.Text when its run with a button on
Form2 !!!! I dont whant make a instance Form1.
Thanks friends**
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void ChangeForm1Text_Click(object sender, EventArgs e)
{
Form1
}
}
In this particular case (you need a reference to the Owner form) there is a simple shorcut that doesn't require to keep a local instance of the first form
Just pass the instance of Form1 as the Owner in the ShowDialog call
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.ShowDialog(this);
}
Now you could reference the property Owner inside the Form2 code
private void ChangeForm1Text_Click(object sender, EventArgs e)
{
this.Owner.Text = "your new caption for form1";
}
One possible way for you to do this is to pass the instance of Form1 into a constructor on Form2`. So add this constructor and field to Form2:
private readonly Form1 _parentForm1;
public Form2(Form1 parentForm1) : this()
{
_parentForm1 = parentForm1;
}
Now when you create your Form2 in the button click, create it like this:
Form2 F2 = new Form2(this);
F2.ShowDialog();
Then you can use _parentForm1 anywhere in your instance of Form2 to refer back to the other form.
I have a Form (form1) which has a ListBox and a Button. On clicking the button, it opens another Form (form2). In this Form I have a TexBox and a Button. On clicking this button, whatever I entered in this Textbox, should have to go into the ListBox in form1. Please help me to find out the solution.
step 1 : Set the Modifiers property of Listbox as Public
step 2 : in button click of Form1, put
Form2 fm2 = new Form2(this);
fm2.ShowDialog();
step 3: in Form2, put the following declaration at top level
private Form1 _fm1;
Also add a constructor :
public Form2(Form1 fm1)
{
_fm1 = fm1;
InitializeComponent();
}
step 4: in button click of Form2, put the following lines:
_fm1.ListBox1.Items.Add(Textbox1.Text);
this.Close(); //close the Form2
Hope this helps.
Make a static instance of form1 public static Form1 _Form1;,then you have access it from other forms with out make an instance:
I write an example:
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Form1 = this;
}
public static Form1 _Form1;
public void AddItem(object value)
{
listBox1.Items.Add(value);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 _Form2 = new Form2();
_Form2.Show();
}
}
Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1._Form1.AddItem("your item");
}
}
Make Form1 the Owner of Form 2 and then you can access Form1 from Form2:
Form1:
private void Form1Button_Clicked(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Owner = this;
f2.Show();
}
public void AddListItem(object text)
{
YourListBox.Items.Add(text);
}
Form2:
private void Form2Button_Clicked(object sender, EventArgs e)
{
Form1 f1 = (Form1)Owner;
f1.AddListItem(YourTextBox.Text;);
Close();
}
When you open the second form pass a reference to the current form:
var form2 = new Form2(this);
form2.Show();
In the constructor of form2 save the reference and use the reference in the click handler
class Form2
{
private Form1 _form1;
public Form2()
{
// ...
}
public Form2(Form1 form1):this()
{
_form1 = form1;
}
void Button_Click(object sender, EventArgs e)
{
if(_form1 != null)
{
_form1.AddValue(textBox1.Text);
}
}
}
It would be way better to define an interface that contains the method 'AddValue' and instead of using the type Form1 use the interface:
interface IAddValues
{
void AddValue(string value);
}
class Form1: IAddValues
{
// ...
public void AddValue(string value)
{
// ...
}
}
// somewhere in form1:
var form2 = new Form2(this as IAddValues);
form2.Show();
In the constructor of form2 save the reference and use the reference in the click handler
class Form2
{
private IAddValues _valueAdder;
public Form2()
{
// ...
}
public Form2(IAddValues valueAdder):this()
{
_valueAdder = valueAdder;
}
void Button_Click(object sender, EventArgs e)
{
if(_valueAdder != null)
{
_valueAdder.AddValue(textBox1.Text);
}
}
}
Stuck in listening event to another form.
When I try to close my Form2, nothing happens on Form1. I want to do something in Form1 when Form2 closes.
Here is my code for Form1
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
Form2 frm2= new Form2();
frm2.FormClosing += new FormClosingEventHandler(frm2_FormClosing);
}
void frm2_FormClosing(object sender, FormClosingEventArgs e)
{
throw new NotImplementedException();
}
You will need to show the object which you are implementing it's FormClosing event. Since the new object you are creating is in your constructor I assume that frm2 isn't the Form which you are showing, which means you are not handling the event.
public Form1()
{
InitializeComponent();
Form2 frm2 = new Form2();
frm2.FormClosing += frm2_FormClosing;
frm2.Show();
}
void frm2_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Form2 is closing");
}
you create a new instance of form2, and listen to its closing event - but from your posted code you don't ever show it? Not sure what I'm missing but what you think should work does work - i.e :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.FormClosing += frm2_FormClosing;
frm2.Show();
}
void frm2_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("form 2 closed");
}
}