i've two winforms form1 and form2 as the following:
public partial class Form1 : Form
{
Form2 frm2;
int count = 0;
public bool fromForm2;
public Form1(bool fromForm2 = false)
{
InitializeComponent();
this.fromForm2 = fromForm2;
MessageBox.Show(fromForm2.ToString());
if (fromForm2 == true) {
test();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (frm2 == null)
{
frm2 = new Form2(); //Create form if not created
frm2.FormClosed += frm2_FormClosed; //Add eventhandler to cleanup after form closes
}
frm2.Show(this); //Show Form assigning this form as the forms owner
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
frm2 = null; //If form is closed make sure reference is set to null
Show();
}
public void test ()
{
textBox1.Text = "ABCDFGHIJKLM";
MessageBox.Show(textBox1.Text);
}
}
Form 2
public partial class Form2 : Form
{
Form1 f1;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objj = new Form1(true);
objj = (Form1)Application.OpenForms["Form1"];
objj.fromForm2 = true;
objj = null;
}
}
i want click the button in form2, will run test(), but i've found that, if i use if (fromForm2 == true) { } the if statement, the test() function will not update (redraw?) the text value to display to the textBox1, anyone know what is the reason of this?
Related
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.
This is the first form( it contains an OK button and a textbox)
namespace Testt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int dimx;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
this.Show();
dimx = int.Parse(textBox1.Text);
MessageBox.Show(dimx.ToString());
}
}
}
This is the second form (it contains an OK button + a messageBox when OK is pressed)
namespace Testt
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f=new Form1();
MessageBox.Show(f.dimx.ToString());
}
}
}
I want to write the value of 6 in the textbox press OK, then form2 pops up and when i press OK on the second form it should display 6 not 0..what am i doing wrong?
You could make it so that your form takes dimx as a variable, so it would look like this
public partial class Form2 : Form
{
private int dimX;
public Form2(int dimx)
{
InitializeComponent();
dimX = dimx;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dimX.ToString());
}
}
alternatively you could pass the form itself, changing
public Form2(int dimx)
into
public Form2(Form1 f1)
You would then also have to replace
private int dimX;
//and
dimX = dimx;
//and
MessageBox.Show(dimX.ToString());
with
private Form1 f;
//and
f = f1;
//and
MessageBox.Show(f.dimx.ToString());
You are creating a NEW form object on every onclick event and that is not the way it works...
private void button1_Click(object sender, EventArgs e)
{
Form1 f=new Form1(); // no that way!!!
MessageBox.Show(f.dimx.ToString());
}
use instead a callback, delegate
So im using this now
Form1:
namespace Testt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int dimx;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
this.Hide();
f2.ShowDialog();
this.Show();
dimx = int.Parse(textBox1.Text);
//MessageBox.Show(dimx.ToString());
}
}
}
and Form2:
namespace Testt
{
public partial class Form2 : Form
{
private Form1 f;
public Form2(Form1 f1)
{
InitializeComponent();
f=f1;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(f.dimx.ToString());
}
}
}
I'm new in C# programming. I have a beginner level question:
How do I change the text property of the textbox1 in my form 2 object using a button in my form1?
Here's my code in form1:
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
This is in form2:
namespace DoubleForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textBox1.Text = "Test";
}
}
}
When you add a text box or any control for that matter to a Winform using the controls toolbox the control gets added as private so it can't be accessed outside of the class it's created in. Easy enough to fix though just added a public property that lets you get and set the text box value as such
namespace DoubleForms
{
public partial class Form1 : Form
{
// NEW CODE
public string TextBoxText
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
Then from Form2 you can just call form1.TextBoxText = "blah blah" to set the value.
Code is creating new Form1 every-time you click the button, which is not you want I believe.
What you need to do is create an event in Form2 and then subscribe to that event in Form1, that way you can listen changes from Form2 and update Form1.
namespace DoubleForms
{
public partial class Form2 : Form
{
public event EventHandler Updated; // define an event handler
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(Updated != null)
{
Updated(sender, new EventArgs()); //Raise a change.
}
}
}
}
Now in Form1 subscribe to Form2 event.
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Updated += (se,ev)=> textBox1.Text = "Test"; // update textbox
frm2.Show();
}
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
I am new to c#. I have the following in my project in windows forms:
Form1 with button and textbox.
User control with a buttton.
Form2 with button and textBox.
As shown in the screenshot: In form1, I click "Show User Control1" User Control1 pops up. Then in User Control1 I click Show Form2 form2 pops up.
In Form2 I enter values in textBox and when click "Send to textbox in form1" I want this text to be inserted into the textbox in Form1.
My question is: How can I send text from form2 to textbox in form1 via user control1?
I just need to know some steps to follow or some code if it is possible to achieve this.
Please help me. Thank you
Form1:
public partial class Form1 : Form
{
UserControl1 UC1 = new UserControl1();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Controls.Add(UC1); //add a userControl
UC1.Visible = true;
}
}
User Control1:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// I want to send text to form1 when this button is clicked
}
}
You can do it by trigger event and event handler.
In Form2,
public delegate void SendTextF2(string YourStringFromTextBox);
public partial class Form2 : Form
{
public event SendTextF2 UISendTextHandlerF2;
public Form2(TextBox s)
{/*unchange*/}
private void button1_Click(object sender, EventArgs e)
{
if(UISendTextHandlerF2!=null)
UISendTextHandlerF2(textBox1.Text);
}
}
In UserControl1,
//New
public delegate void SendTextUC(string YourStringInTextBox);
public partial class UserControl1 : UserControl
{
//New
public event SendTextUC UISendTextHandlerUC;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
//Add event handler
frm2.UISendTextHandlerF2 += SendText123;
}
//Event Handler for the event trigger in Form2
void SendText123(string YourStringFromTextBox)
{
//Trigger Event
if(UISendTextHandlerUC!=null)
UISendTextHandlerUC(YourStringFromTextBox);
}
}
In Form1,
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
//Add event handler
UC1.UISendTextHandlerUC += FinallyWeGetTheString;
}
Controls.Add(UC1);
UC1.Visible = true;
}
//New
void FinallyWeGetTheString(string YourStringFromTextBox)
{
textBox1.Text = YouStringFromTextBox;
}
}
Add those line to your code:
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
}
Controls.Add(UC1);
UC1.Visible = true;
}
}
User Control:
public partial class UserControl1 : UserControl
{
TextBox r;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
}
}
And Form2:
public partial class Form2 : Form
{
TextBox s;
public Form2(TextBox s)
{
InitializeComponent();
this.s = s;
}
private void button1_Click(object sender, EventArgs e)
{
String str = textBox1.Text;
s.Text = str;
}
}
This question is the followup to the following question:
C# Text don't display on another form after double clicking an item in listbox
Now I have typed my value in the textbox of form3. How am I going to pass back the value to form1 to show it in the listbox10 after pressing "OK" in form3? Below is my form3 coding but it don't work:
private void button1_Click(object sender, EventArgs e)
{
//This is the coding for "OK" button.
int selectedIndex = listBox10.SelectedIndex;
listBox10.Items.Insert(selectedIndex, textBox1.Text);
}
You can put public property on form3:
public partial class form3 : Form
{
public String SomeName
{
get
{
return textbox1.Text;
}
}
...
private void buttonOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
In form1, where you are open form3, after ShowDialog, you will write:
if (form3.ShowDialog() == DialogResult.OK)
{
int selectedIndex = listBox10.SelectedIndex;
if (selectedIndex == -1) //listbox does not have items
listbox10.Add(form3.SomeValue);
else
listBox10.Items.Insert(selectedIndex, form3.SomeName);
}
do do something like that:
//form1:
public void add(int num)
{
//add num to the list box.
}
now, form3 should get an instance of form1 in the constructor, and save it:
//in form3:
private form form1_i
public form3(form i_form1)
{
.
.
.
form1_i = i_form1;
}
and on button click in form3, call the fumction add in form1.
It should go like this, this is the safest way to do it, in fact if you are working on Windows Mobile this is the only way that won't crash the application. In desktop versions it can crash in debug versions.
public partial class Form1 : Form
{
public string name = "something";
public Form1()
{
InitializeComponent();
}
public delegate void nameChanger(string nme);
public void ChangeName(string nme)
{
this.name = nme;
}
public void SafeNameChange(string nme)
{
this.Invoke(new nameChanger(ChangeName), new object[] { nme });
}
private void button2_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3(this);
f3.Show();
}
}
public partial class Form2 : Form
{
Form1 ff;
public Form2(Form1 firstForm)
{
InitializeComponent();
ff = firstForm;
}
private void button2_Click(object sender, EventArgs e)
{
ff.SafeNameChange("something different from the Form1");
this.Close();
}
}