I want to add a node from one form to the other one. I have the following code, but it does not work:
From Form2:
private void button2_Click(object sender, EventArgs e)
{
_HauptFenster = new Form1();
_HauptFenster.AddGroup(textBox1.Text);
this.Close();
}
to Form1:
public void AddGroup(string name)
{
MessageBox.Show(name);
Einträge.Nodes.Add(name);
}
I can see the real name of the node in the MessageBox, but the node is not being added to the TreeView on Form1.
I see two possible scenarios:
You have a Form2 and you need to add a node to Form1's treeview (assuming Form1 has already shown). In that case, you can't use _HauptFenster = new Form1(); which is a new Form1, not the one you already have. What you should do is to make a reference of form1 in form2:
public partial class Form1 : Form
{
Form1 form1;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 form1):this()
{
this.form1= form1;
}
private void button1_Click(object sender, EventArgs e)
{
form1.AddGroup("something");
this.Close();
}
}
and change modify the way you open Form2:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2= new Form2(this);
form2.Show();
}
Form1 has not been shown yet, because you forgot to bring it up:
private void button1_Click(object sender, EventArgs e)
{
Form1 form1= new Form1 ();
form1.Show();
form1.AddGroup("something");
this.Close();
}
I found a solution:
Form1 master = (Form1)Application.OpenForms["Form1"];
master.Einträge.Nodes.Add(name);
I think this is what ShAkKiR said; specifically, scenario 1, where I was trying to add it to a TreeView control in a new instance of Form1, instead of adding to to the existing instance of Form1.
Related
I have two forms Form1 and Form2, Form 1 with a dataGridView and a button where Form2 has one button.
When i click the button1 in Form1, it will open Form2 (overlapping).
Now i need to clear the values of the datagridview in Form1 when i press the
button in Form2 also the same button click event should close the
Form2.
Any idea how to do it?
Any help is appreciated
This is Form 2:
private DataGridView _dgv;
public Form2(DataGridView dgv)
{
_dgv = dgv;
InitializeComponent();
}
private void buttonClearRows_Click(object sender, EventArgs e)
{
_dgv.Rows.Clear();
Close();
}
This is Form1:
private void buttonOpenForm2_Click(object sender, EventArgs e)
{
new frm2(dataGridView1).ShowDialog();
}
You need to pass a reference to Form1 into Form2, either by consctructor or by a property. Sample uses constructor. Change the names of the controls to the ones you have. Consider this as a pseudo code sample.
Form1 (some logic removed for brewity)
public class Form1
{
...
public void Clear()
{
DataGridView1.Rows.Clear();
}
public void btnOpenForm2_Click(object sender, EventArgs e)
{
var form2 = new Form2(this); // create a new form2, and pass a reference to form1
form2.Show(); // show the form.
}
...
}
Form2 (some logic removed for brewity)
public class Form2
{
private Form1 _parent; // this will hold the parent until Form2 is disposed.
...
public void Form2(Form1 parent)
{
_parent = parent; // assign Form1 instance to a field.
}
public void btnClearGrid(object sender, EventArgs e)
{
_parent.Clear(); // clear the rows in the datagridview instance within form1.
}
...
}
If I understand what you're asking, I strongly suggest to hide access to DataGridView in Form1 from outside the class, to avoid unexpected behavior in future.
You could instead add a function in Form1 and manage button1 click event in this way:
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
dialog.ShowDialog(this);
}
public void ClearRows() { dataGridView1.Rows.Clear(); }
}
Then in Form2 you can easily handle click on button in this way:
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
((Form1)this.Owner).ClearRows();
this.Close();
}
}
It's very simply, in the form1 you can create an instance of Form2 and after the show you can set the reference of dataGridView in child form2.
Fundamental is who you set the dataGridProperty modifier=public(visual property F4) in the form1
This is Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.dataGridViewPassed = this.dataGridView1;
f.ShowDialog();
}
}
This is Form 2:
public partial class Form2 : Form
{
public DataGridView dataGridViewPassed = null;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridViewPassed.Rows.Clear();
}
}
How can I change title of Form 1 from my From 2? Here is my code:
Form1:
public void setTitle(string title)
{
this.Text = title;
}
Form2:
private void buttonOk_Click(object sender, EventArgs e)
{
Form1 f1= new Form1();
f1.setTitle(textBoxTitle.Text);
this.Hide();
}
What am I doing wrong?
You should pass Form1 as parameter in Form2's constructor.
Form1 Form_one;
public Form2(Form1 form1):this()
{
Form_one = form1;
}
private void buttonOk_Click(object sender, EventArgs e)
{
Form_one.setTitle(textBoxTitle.Text);
this.Hide();
}
In the method you want to show Form2 you should call like that;
Form2(this).Show();
You should have the actual instance of Form1 which is currently displayed.
While displaying Form1, keep an instance to it in Form2. (I am assuming that you are displaying Form1 from Form2. If not, you should provide that Form1 instance to Form2 while creating an instance of Form2)
public class Form2 : Form
{
private Form1 form1;
private void OpenForm1()
{
form1 = new Form1();
form.Show()
}
}
Then, invoke the setTitle() on that instance:
private void buttonOk_Click(object sender, EventArgs e)
{
form1.setTitle(textBoxTitle.Text);
this.Hide();
}
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();
}
I am getting a odd error that I cant recall ever getting before. I am trying to make a few menus for a small game but somehow something is wrong with my reference to Form1.
Here is the code:
public partial class Form1 : Form
{
Form2 Form2 = new Form2();
Form3 Form3 = new Form3();
public string difficulty = "Makkelijk";
public string guesses = "Normaal";
public Form1()
{
InitializeComponent();
}
private void playButton_Click(object sender, EventArgs e)
{
//Form3.difficulty = difficulty;
//Form3.guesses = guesses;
Form3.Show();
this.Hide();
}
private void optionsButton_Click(object sender, EventArgs e)
{
Form2.Show();
this.Hide();
}
private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
Form2:
public partial class Form2 : Form
{
Form1 Form1 = new Form1();
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Woord toevoeg query
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
//Form1.difficulty = comboBox1.Text;
//Form1.guesses = comboBox2.Text;
this.Close();
}
}
Is there anything wrong with this?
Thanks in advance.
This is because you are initializing the Form2 inside the Form1 and in Form2 you are initializing Form1, which makes circular initialization and causes to stackoverflow exception.
You are creating a new Form1 in the ctor of Form2 and Form2 in the ctor of Form1.
Each time you create one of those, you create the other as well and so you get into an infinite loop which evantualy fills up your stack.
As said before, circular initialization is causing your exception.
One way of solving it is to make Form2 accept Form1 as constructor parameter.
Form1 form;
public Form2(Form1 form1)
{
form = form1;
InitializeComponent();
}
The first line of Form1 creates a new Form2. The First line of Form2 creates a new Form1. This will keep happening until you run out of memory.
Remove the
Form1 Form1 = new Form1();
from the Form2 definition.
How to Add or Remove a control from another form that is active and currently showing? I am using the following code:
private void button2_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.Controls.RemoveByKey("button1");
}
But it is not working because of new initialization of Form1.
If the form, from which a control is to be removed, is a child of another form you can access the form through the OwnedForms property. And after accessing it, you can remove controls from it. For example
Form1
private void button1_Click(object sender, EventArgs e)
{
var form = new Form3 { Owner = this };
form.Show();
}
private void button2_Click(object sender, EventArgs e)
{
if(OwnedForms.Length > 0)
{
var form = OwnedForms[0];
//assuming there's a control with id 'One':
form.Controls.RemoveByKey("One");
}
}
From the comments, it is clear that Form2 does not have a reference to Form1.
You didn't post any code on how you are displaying Form2, but here is an example on how it could work by passing the reference through the constructor:
public class Form2 : Form {
private Form1 _Form1;
public TestForm(Form1 form1) {
InitializeComponent();
_Form1 = form1; // <- this is the reference from Form1
}
}
Then your removing action on Form2 would look like this:
private void button2_Click(object sender, EventArgs e) {
_Form1.Controls.RemoveByKey("button1");
}
When creating Form2 from Form1, this is an example on how it would be passed:
private Form2 _Form2;
private void button1_Click(object sender, EventArgs e) {
_Form2 = new Form2(this); // <- this is the reference of Form1 you are passing
_Form2.Show();
}
As you already know you need the one instance of Form1.
If this is your main form and you used the Visual Studio wizard to create a Windows Forms Application you can look in the Program class. There is usually the main form built.
Now you need to assign the new Form1() expression to a variable and assign it to the second form with button button2.