I want to call a method in my class form1.cs after closing form2.cs
form1:
private void button1_Click(object sender, EventArgs e)
{
var form2= new form2();
form2.Show();
}
public void Form1Refresh()
{
//some code
}
form2:
private void button1_Click(object sender, EventArgs e)
{
...
Close();
//call refresh from form1
}
How can i used the method Refresh() after closing the form ? I try it with new form1 and call the function but this used a new object and doesnt work.
Should i pass the object of the form1 to form2 and use it or is there another solution ?
When you create and show your Form2 handle its FormClosed event.
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.FormClosed += form2_FormClosed; // This Line
form2.Show();
}
void form2_FormClosed(object sender, FormClosedEventArgs e)
{
Debug.Print("Form 2 has been closed. Call the Refresh Action");
}
This question is probably a duplicate.
You could hook the FormClosed event when you're constructing Form2 on the button press in Form1
var form2 = new Form2();
form2.Show();
form2.FormClosed += (sender, eventArgs) => { Refresh(); };
Windows Forms already contain a method called Refresh, did you intend to hide the method from the baseclass? I'd suggest giving it a different name or at least call the base.Refresh(); from your method.
Try calling the MainWindow and parse it to Form1.
Window window = Application.Current.MainWindow;
((form1)window).Refresh();
try this code
FORM1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
public void Form1Refresh()
{
///////
}
FORM2:
public Form1 f1;
public Form2(Form1 m)
{
this.f1 = m;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
f1.Form1Refresh();
}
Related
I have three WinForms .. Form1 Form2 and Form3
// Form1 Button
private void btF1_Click(object sender, EventArgs e)
{
new Form3(this).ShowDialog();
}
// Form2 Button
private void btF21_Click(object sender, EventArgs e)
{
new Form3(this).ShowDialog();
}
// Form3
public partial class AjoutDemandeur : Form
{
Form1 _owner;
Form2 _owner2;
public Form3(Form1 owner, Form2 owner2)
{
InitializeComponent();
_owner = owner;
_owner2 = owner2;
}
private void button1_Click(object sender, EventArgs e)
{
_owner.methodForm1(); //call a method from Form1
}
private void button2_Click(object sender, EventArgs e)
{
_owner2.methodForm2(); // call a method from Form2
}
I want to call a method from Form1 and Form2 into the Form3
But the problem is in the two buttons btF1 and btF2
=> there is no argument given that corresponds to the required formal parameter 'owner2' of 'Form3.Form3(Form1, Form2)'
So any solutions !
Create events and their handlers in Form1 and Form2. Now fire those events from Form3.
Solved !
Just I need to pass a null parameter in the calling methods
//Form1 Button
private void btF1_Click(object sender, EventArgs e)
{
new Form3(this,null).ShowDialog();
}
//Form2 Button
private void btF21_Click(object sender, EventArgs e)
{
new Form3(null,this).ShowDialog();
}
This is how I open a new form
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//Sets the Add New Button to Color WhiteSmoke
this.button1.BackColor = Color.WhiteSmoke;
//Hides current form (Form 1)
this.Hide();
//Displays Form 2
f2.ShowDialog();
}
This is how I close my forms. What am I doing wrong?
private void Form1_Close(object sender, FormClosingEventArgs e)
{
foreach (Form form in Application.OpenForms)
{
Environment.Exit(0);
}
}
Instead of calling Environment.Exit(0), try calling Close():
foreach (Form form in Application.OpenForms)
{
form.Close();
}
Another option:
Application.Exit();
Try putting your code in the Form1_FormClosing() Method:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (Form form in Application.OpenForms)
{
// Make sure form is visible before closing //
form.Show();
form.Close();
}
Environment.Exit(0);
}
Form1's close event is not called if the form is hidden. So don't hide the Form. In fact - never hide the form :)
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//Sets the Add New Button to Color WhiteSmoke
this.button1.BackColor = Color.WhiteSmoke;
//Hides current form (Form 1)
this.Hide(); //<-- delete this
//Displays Form 2
f2.ShowDialog();
f2.Dispose(); //<-- necessary when using ShowDialog()
}
And delete all of this:
private void Form1_Close(object sender, FormClosingEventArgs e)
{
foreach (Form form in Application.OpenForms)
{
Environment.Exit(0);
}
}
That should make the process close then you close Form1
Here, simple solution is that pass your Form1 object reference in Form2 contructor and call Form1.Close in Form2_Closing Event.
public partial class Form1 : Window
{
public Form1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Form2 f2 = new Form2(this);
this.Hide();
f2.Show();
}
}
Call Form1 close event when Form2 is closed.
public partial class Form2 : Window
{
Form1 frm;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 frm)
{
InitializeComponent();
this.frm = frm;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
frm.Close();
}
}
I want to add an item to my combobox in form1 from form2. But if I press the button, form2 closes but the item is not added in form1. I hope you can help me! I found no solution for this problem in the internet.
Form 2:
public void button5_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
main.AddItem("Item");
this.Close();
}
Form 1:
public void AddItem(object item)
{
comboBox1.Items.Add(item);
}
First of all: You have to change comboBox access modifier to public. Then:
Form 2:
public void button5_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
main.AddItem("Item");
this.Hide(); // This will hide Form2 ("this." is redundant)
main.ShowDialog(); // This will show Form1
}
Reference the name property of your 'Form1' don't create a new instance. Then reference that forms combobox control.
Your original code creates a NEW instance. Meaning its creating a new version of Form1 and not accessing the original!
The code below should help
public void button5_Click(object sender, EventArgs e)
{
Form1 myForm = whateverYourFormsNamePropertyIs;
myForm.Combobox.AddItem("Item");
this.Close();
}
In form2...
Form1 f;
public Form2(Form1 parent)
{
InitializeComponent();
f = parent;
}
private void Add_Click(object sender, EventArgs e)
{
f.comboBox1.Items.Add("item");
}
In form1
public void AddItem(object item)
{
comboBox1.Items.Add(item);
Form2 f = new Form2(this);
f.Show();
}
orginal form
private void FormPeople_Load(object sender, EventArgs e)
{
populateComboBoxTitles();
}
public void populateComboBoxTitles()
{
comboBox2.Items.Clear();
comboBox2.Items.Add("mr");
comboBox2.Items.Add("miss");
}
private void button5_Click(object sender, EventArgs e)
{
FormAddTitle formAddTitle = new FormAddTitle(this);
formAddTitle.Show();
}
secondary form
FormPeople formPeople;
public FormAddTitle(FormPeople formPeople)
{
InitializeComponent();
this.formPeople = formPeople;
}
private void button1_Click(object sender, EventArgs e)
{
if (formPeople != null)
formPeople.populateComboBoxTitles();
}
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
I would like to control Form1 from Form2
I'm a newbie to C# and I can't find the answer I'm looking for in google, so I'm hoping someone here could help me. I'm only practicing to transfer data (or pass, call it however you want) from a form to another.
Here's what I have:
I have 2 forms - Form1 and Form2.
Form1 contains a textbox (named txtForm1) and a button (named btnForm1).
Form2 contains a textbox (named txtForm2) and a button (named btnForm2).
After running the application, by clicking the button btnForm1, the user opens Form2. The text that the user writes in the textbox (txtForm2) should be transfered to the textbox (txtForm1, which button is disabled) in Form1.
How can I do this transfer?
Edited:
Okay i need to be clear that this is all the code i have:
Form1 (button which opens Form2):
private void btnForm1_Click(object sender, EventArgs e)
{
new Form2().Show();
}
Form2 (button which closes Form2):
private void btnForm2_Click(object sender, EventArgs e)
{
this.Close();
}
I have NOTHING ELSE. (I'm a total newbie)
Make a public variable and pass it the value from your text box and then onto your second form.
public static string myVar;
myVar = txtForm2.Text;
and when you return to the first form:
txtForm1.Text = Form2.myVar;
In your Form2 you should have some like:
private void btnForm2_Click(object sender, EventArgs e)
{
this.Hide();
}
public String GettxtForm2()
{
return txtForm2.Text;
}
Now in form1 you can acces that txtForm2 with something like:
Form2 form2 = new Form2();
//on click btnForm1 show that form2 where you can edit the txtForm2
private void btnForm1_Click(object sender, EventArgs e)
{
form2.Show();
}
//after you save the txtForm2 when you will focus back to form1 the txtForm1 will get the value from txtForm2
private void Form1_Enter(object sender, EventArgs e)
{
txtForm1.Text = Form2.GettxtForm2();
}
You can easy modify the events where all this logic can occur...
in Form1:
public void SetTextboxText(String text)
{
txtForm1.Text = text;
}
private void btnForm1_Click(object sender, EventArgs e)
{
var frm = new Form2(this); // pass parent form (this) in constructor
frm.Show();
}
in Form2:
Form _parentForm;
public Form2(Form form)
{
_parentForm = form;
}
private void txtForm2_TextChanged(object sender, EventArgs e)
{
_parentForm.SetTextboxText(txtForm2.Text); // change Form1.txtForm1.Text
}
Try this ;)
On Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(textBox1.Text);
frm2.Show();
this.Hide();
}
On form2:
public partial class Form2 : Form
{
public string textBoxValue;
public Form2()
{
InitializeComponent();
}
public Form2(string textBoxValue)
{
InitializeComponent();
this.textBoxValue = textBoxValue;
}
private void Form2_Load(object sender, EventArgs e)
{
textBox2.Text = textBoxValue;
}