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();
}
Related
i have Form1 and Form2, in Form1 i have some textboxes like username,password and more... and a textbox "region". when user hits "region"(Form1.hide()), then Form2 opens witch has 5 labels with names of regions on it.
so how can i make that when user clicks on a name of region in Form2, Form1 will have the region on it? and keep all the data that the user entered before the region click.
something like this(in form 2):
private void center_Click(object sender, EventArgs e)
{
this.Hide();
Form1.region = "center";
Form1.show();
}
Try creating an instance of Form2 and calling ShowDialog() method to show it
Form2 form2= new Form2();
form2.ShowDialog();
When creating Form2, just pass Form1 as a parameter and edit the textbox value in your click event.
On form1:
private void click_on_region(object sender, EventArgs e)
{
this.Hide();
Form2 frm2 = new Form2(this);
Form2.Show();
}
on form2:
Form1 _frm1;
public Form_Main(Form1 frm)
{
InitializeComponent();
_frm1 = frm;
}
private void center_Click(object sender, EventArgs e)
{
this.Hide();
_frm1.textBox_region.Text = whateverobject.Text;
_frm1.Show();
}
This might not be the prettiest but It'll do for starters.
Form 1 Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
textBox_Region.Text = objForm2.RegionName;
}
}
And Form 2 Code
public partial class Form2 : Form
{
public string RegionName
{
get
{
return textBox_Form2_Region.Text.ToString();
}
set { }
}
public Form2()
{
InitializeComponent();
}
}
On Form 2
private void center_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textbox_region = whateverobject.text;
this.hide();
frm1.show();
}
this will bring up a form1 with the region text on it.
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.
I have two forms, Form1 and Form2. I use ShowDialog() on Form2 from Form1. How can I run Form1Load() from Form2? Specifically, I want to refresh Form1 from Form2.
Firstly, make sure you assigning the Owner property on Form2 before showing it. This allows you to access the current instance of Form1.
class Form1 : Form
{
public void Method()
{
var form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
}
}
From Form2 you can this use this.Owner to access the instance of Form1 and call any public methods or access any public properties. (Make sure the load event handler is public)
class Form2 : Form
{
public void Method()
{
this.Owner.form1_Load(null,null); //assuming you don't use these params.
}
}
In your form1_Load() I would recommend putting in a this.Refresh() to "refresh" the form. The refresh doesn't update some things that have data stored, it only repaints the form, so in the load event you will have to manually "refresh" things.
Create an instance of Form1 and then use the Refresh method or your Form1_Load method with that instance.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ShowMessage(string message)
{
MessageLabel.Text = message;
}
private void ShowForm2(object sender, EventArgs e)
{
Form2 Form2Copy = new Form2(this);
Form2Copy.ShowDialog();
}
}
and
public partial class Form2 : Form
{
Form1 Form1Copy;
public Form2(Form1 Parent)
{
InitializeComponent();
Form1Copy = Parent;
}
public void Button_Click(Object sender, EventArgs e)
{
Form1Copy.ShowMessage("Hello from Form2!");
}
}
Pass in Form1 to the ShowDialog() method of your Form2 instance:
private void Form1_Load(object sender, EventArgs e)
{
this.LoadEventCode();
}
public void LoadEventCode()
{
this.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass in Form1
}
Now over in Form2, cast the .Owner property to Form1 and do what you need:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
f1.LoadEventCode();
}
I have 2 Form and i want run Button1.PerformClick() in form1 with button2 in form2, when two forms are opened !!!
I dont want make a instance Form1.
Thanks friends
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonShowForm2_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
//Run my method...
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void ClickButton1Form1_Click(object sender, EventArgs e)
{
Form1 //Run my method... |Form1.Button1.PerformClick()
}
}
Form2 needs a reference to an instance of Form1 and needs to invoke the functionality on that instance.
Create a method on Form1 which invokes this functionality:
public void PerformButton1Click()
{
Button1.PerformClick();
}
Require an instance of Form1 on the constructor for Form2:
private Form1 Form1Instance { get; set; }
public Form2(Form1 form1Instance)
{
InitializeComponent();
Form1Instance = form1Instance;
}
Pass a reference to the existing instance when constructing an instance of Form2:
Form2 F2 = new Form2(this);
F2.ShowDialog();
Then you can invoke that functionality on that instance:
private void ClickButton1Form1_Click(object sender, EventArgs e)
{
Form1Instance.PerformButton1Click();
}
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.