Calling 2 methods from 2 different forms - c#

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();
}

Related

Access to SelectedPath in Form2

I have two form, FolderBrowserDialog in Form1 and I want to use SelectedPath in Form 2.
I don't know why but when I click on Form1 Button to show Form2 ,This Error will be appear :
Invalid URI: The URI is empty
I set Form Control Modifiers: Public and FolderBrowseDialog Seletedpath not be transferred to Form2
Form 1 :
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog MyFolderBrowse = new FolderBrowserDialog();
if(MyFolderBrowse.ShowDialog()==DialogResult.OK)
{
txtpath.Text = MyFolderBrowse.SelectedPath;
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
Frm2.ShowDialog();
}
Form 2 :
private void Form2_Load(object sender, EventArgs e)
{
Form1 Frm1 = new Form1();
webBrowser1.Url = new Uri(Frm1.txtpath.Text);
}
any solution...?
Welcome to StackOverflow!
The problem is, if I understand it correctly from the code you post it, is that you create a new instance of Form2 and then in the Loadevent of the Form2 you create a new instance of Form1 and the information you need is the instance of Form1 you already have.
You already have an instance of Form1. I would do it by two one of the 2 options:
Create a public property in Form2 and assign it when creating the Form2 instance
public class Form2: Form
{
//{...}
public string SelectedPath { get; set;}
//{...}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(this.SelectedPath);
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
Frm2.SelectedPath = txtpath.Text;
Frm2.ShowDialog();
}
Create an argument in the Form2 constructor and pass the selected path from Form1
public class Form2: Form
{
//{...}
private string _selectedPath;
public Form2(string selectedPath)
{
_selectedPath = selectedPath;
}
//{...}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(_selectedPath);
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2(txtpath.Text);
Frm2.ShowDialog();
}

Use method after close form in C#

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();
}

Add combobox item from another form won't work

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();
}

Transferring data from Form2 (textbox2) to Form1 (textbox1)? [duplicate]

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;
}

go back to the previous form (c#)

I know how to go to another form in modal mode just like what I did below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 myNewForm = new Form2();
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
myNewForm.ShowDialog();
}
}
This is my second form, how do I go back to the previous form?
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
// what should i put here to show form1 again
}
}
When you call ShowDialog on a form, it runs until the form is closed, the form's DialogResult property is set to something other than None, or a child button with a DialogResult property other than None is clicked. So you could do something like
public partial class Form1
{
...
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
newform.ShowDialog();
// We get here when newform's DialogResult gets set
this.Show();
}
}
public partial class Form2
{
...
private void button1_Click(object sender, EventArgs e)
{
// This hides the form, and causes ShowDialog() to return in your Form1
this.DialogResult = DialogResult.OK;
}
}
Although if you're not doing anything but returning from the form when you click the button, you could just set the DialogResult property on Form2.button1 in the form designer, and you wouldn't need an event handler in Form2 at all.
I use a static Form value Current in all my multiple form apps.
public static Form1 Current;
public Form1()
{
Current = this;
// ... rest of constructor
}
Then in Form2
public static Form2 Current;
public Form2()
{
Current = this;
// ... rest of constructor
}
Then you can from your button click do,
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
// what should i put here to show form1 again
Form1.Current.ShowDialog(); // <-- this
}

Categories

Resources