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();
}
Related
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();
}
I have two forms. The main form contains a treeview. After I show the second form, the treeview loses focus. That's okay, but I want to activate the treeview when the second form closes.
Form1.cs
namespace ex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
form2.StartPosition = FormStartPosition.CenterParent;
form2.ShowDialog();
}
}
internal void example()
{
treeView1.SelectedNode = treeView1.Nodes[1];
}
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.cs
namespace ex
{
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();
//not working
form1.treeView1.Focus();
form1.treeView1.Select();
}
}
}
Form2 really shouldn't get so intimate with Form1. Try turning your code around like this:
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
if (form2.ShowDialog(this) == DialogResult.OK) {
treeView1.Select();
example();
}
}
}
If Form2 is supposed to supply any information to add to your TreeView control, you would set up a property on Form2 and access it from within this same code block.
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();
}
My problem is very simple: i have a combobox in form1, i have a button that open form2 to write into a textbox the new item to add. Here my code:
Form1:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
}
Form2:
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
combobox.Items.Add(new_item);
this.Close();
}
But the new item is not added to my comobobox.
I tried to refresh th combobox but i have the same result.
Thank you.
You need to add the item to your ComboBox after closing Form2:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
comboBox1.Items.Add(new_item); //this is missing in your code
}
But a better way would be creating a public property in Form2 to pass the string back:
public string Value { get; set; }
private void btn1_Click(object sender, EventArgs e)
{
this.Value = textBox1.Text; //pass the TextBox value to the property
this.DialogResult = DialogResult.OK; // Cancel would mean you closed/canceled the
// form without pressing OK-Button (btn1)
this.Close();
}
Than in Form1 you can access the property and add the new item:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if(f2.ShowDialog() == DialogResult.OK) //check the result
{
comboBox1.Items.Add(f2.Value);//Add the new item
}
}
Assuming combo's name is combobox.
Form1:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if (f2.ShowDialog() == DialogResult.OK)
combobox.Items.Add(f2.ItemValue);
}
Form2:
public string ItemValue {get {return textBox1.Text;} };
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
this.DialogResult = DialogResult.OK;
}
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;
}