English is not my native language so please bear with me.
What I meant by my question is that how would one make a form (form 2, for example) display what is being typed from form 1, like in real time. I assume the
TextChanged event would be used but what's the next step?
To keep it simple and OOP-friendly, I would make a property in Form2 and update it in the Form1.TextChanged event:
Form1.cs
public partial class Form1 : Form
{
private Form2 _form2;
public Form1()
{
InitializeComponent();
this.textBox1.TextChanged += (sender, e) =>
{
_form2.TextBoxValue = textBox1.Text;
};
_form2 = new Form2();
_form2.Show();
}
}
Form2.cs
public partial class Form2 : Form
{
public string TextBoxValue
{
get => textBox1.Text;
set => textBox1.Text = value;
}
public Form2()
{
InitializeComponent();
}
}
Note that you would need to change the Form1 and Form2 names to the actual form class names in your application (as well as the textBox1 name).
Edit:
As requested in the comments, if you want changes to either textbox to be updated in both forms, you need to add the TextBoxValue property to both forms, and make the event handlers in both forms change that property. I've also cleaned up the code a little.
Form1.cs
public partial class Form1 : Form
{
private Form2 _form2;
public string TextBoxValue
{
get => textBox1.Text;
set => textBox1.Text = value;
}
public Form1()
{
InitializeComponent();
this.textBox1.TextChanged += (sender, e) =>
{
_form2.TextBoxValue = textBox1.Text;
};
_form2 = new Form2();
_form2.Show();
}
}
Form2.cs
public partial class Form2 : Form
{
private Form1 _form1;
public string TextBoxValue
{
get => textBox1.Text;
set => textBox1.Text = value;
}
public Form2()
{
InitializeComponent();
this.textBox1.TextChanged += (sender, e) =>
{
_form1.TextBoxValue = textBox1.Text;
};
this.Shown += (sender, e) =>
{
_form1 = (Form1)Application.OpenForms["Form1"];
};
}
}
Throw an event on form2 and call it from form1
On Form1:
private Form2 frm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var text = Textbox1.Text;
frm2.TextChanged.Invoke(text , new EventArgs());
}
private void Form1_Load(object sender, EventArgs e)
{
frm2 = new Form2();
frm2.Show();
}
and on form2:
public EventHandler TextChanged;
public Form2()
{
InitializeComponent();
TextChanged += HandleTextChanged;
}
private void HandleTextChanged(object sender, EventArgs e)
{
label1.Text = (string) sender;
}
I prefer not to let the two forms know each other.
You can instead do something like this:
static class Program
{
private static Form _form1;
private static Form _form2;
public static void Main()
{
_form1 = new MyForm();
_form2 = new MyForm();
_form1.MyTextbox.OnTextChanged += Form1_MyTextBox_TextChanged;
}
private void Form1_MyTextBox_TextChanged(object sender, EventArgs e)
{
_form2.MyTextBox.Text = _form1.MyTextbox.Text;
}
}
}
public class MyForm : Form
{
public TextBox MyTextBox {get;set;}
}
Related
Im trying to change the text in Form1 when pushing the button on Form2
Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.textCh = "Text has been changed";
}
}
Form 1:
public partial class Form1 : Form
{
public string textCh {
get
{
return this.textCh;
}
set
{
this.label1.Text = value;
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
When I'm pushing button nothing happens, the text remain the same.
Another method of passing Form1 to Form2 via the Show() method and the .Owner property:
// In Form1
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass Form1 via "this"
Then, in Form2, you CAST .Owner to type Form1:
// In Form2
Form1 f1 = this.Owner as Form1;
if (f1!=null && !f1.IsDisposed)
{
f1.textCh = "Text has been changed";
}
There are several way to do this here 2 examples
This example use references whitout any use of events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2(this);
frm.Show();
}
}
here the Form2 is created passing the Form1 as parameter
public partial class Form2 : Form
{
Form1 _parent;
public Form2(Form1 parent)
{
_parent = parent;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var lbl = (Label)_parent.Controls.Find("label1", false).First();
lbl.Text = "new text";
_parent.Update();
}
}
Than the Form2 use that for set the value wanted.
This example use events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2();
frm.UpdateLabelEvent += Frm_UpdateLabelEvent;
frm.Show();
}
private void Frm_UpdateLabelEvent(string str)
{
label1.Text = str;
}
}
and here the code of Form2
public partial class Form2 : Form
{
public event Action<string> UpdateLabelEvent;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UpdateLabelEvent("new string value");
}
}
Nothing changes in your Form1 because you are creating a new Form1 first and change the text there. The new Form1 is never shown, so you see no changes.
Solution
private void button1_Click(object sender, EventArgs e)
{
//Form1 f1 = new Form1(); GET rid of this line
f1.textCh = "Text has been changed";
}
you need to make sure off course that f1 is known in form2, if you dont know how here is a simple way to do that
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.f1 = this;
f2.ShowDialog();
}
public Form2()
{
public Form1 f1 { get; set; }
...
These are my 2 Forms.
These are the codes for Form 1-->
namespace Passing_Values
{
public partial class Form1 : Form
{
string a="preset value";
public Form1()
{
InitializeComponent();
}
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2().Show();
}
public void set(string p)
{
MessageBox.Show("This is Entered text in Form 2 " + p);
a = p;
MessageBox.Show("a=p done! and P is: " + p + "---and a is: " + a);
textBox1.Text = "Test 1";
textBox2.Text = a;
textBox3.Text = p;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(a);
}
}
}
These are the codes for Form 2-->
namespace Passing_Values
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
Form1 j = new Form1();
j.set(g);
}
}
}
See the picture.You can understand the design.
This is what I want to do. 1st I open Form2 using button in Form1. Then I enter a text and click the button("Display in Form1 Textbox"). When it's clicked that value should be seen in the 3 Textboxes in Form1.I used Message Boxes to see if the values are passing or not. Values get passed from Form2 to Form1. But those values are not displays in those 3 Textboxes but the passed values are displayed in Message Boxes. Reason for the 3 Text Boxes can be understood by looking at the code. So what's the error?
Actually I have an object to pass. So I did this
in form1-->
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}
in form2-->
public partial class Form2 : Form
{
Form1 a;
public Form2(Form1 b)
{
a = b;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
a.set(g);
this.Close();
}
}
I would simply pass it in the constructor.
So, the code for form2, will be:
public partial class Form2 : Form
{
string _input;
public Form2()
{
InitializeComponent();
}
public Form2(string input)
{
_input = input;
InitializeComponent();
this.label1.Text = _input;
}
}
And the call in Form1 will be:
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2(this.textBox1.Text.ToString());
fm2.Show();
}
public partial class Form1 : Form
{
Form2 fm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2();
fm2.Show();
fm2.button1.Click += new EventHandler(fm2button1_Click);
}
private void fm2button1_Click(object sender, EventArgs e)
{
textBox1.Text = fm2.textBox1.Text;
}
}
And code in form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
set modifier property of textbox1 and button1 to public
Place a static string in your Form2
public static string s = string.Empty;
and, in your Display in Form1 Textbox button click event, get the value from the textbox in your string s:
s = textBox1.Text;
Form1 f1 = new Form1();
f1.Show();
once, the Form1 is showed up again, then in the Form1_Load event, just pass your Form2's text value to your Form1's textboxes, the value of which was gotten by the variable s:
foreach (Control text in Controls)
{
if (text is TextBox)
{
((TextBox)text).Text = Form2.s;
}
}
Sorry, I know this question, or similar has been asked frequently, but as I've gone through different threads, I just don't know how to apply it to my program.
Here's my situation:
In Form 1, I have a label. There's a button that opens Form 2, which has radiobuttons and a button. The button in Form 2 should send a string value from the radio button, to the label.Text in form 1. How can I go around in doing so?
So, below is what opened form 2.
private void selectkeyButton_Click(object sender, EventArgs e)
{
selectKeyboard sk = new selectKeyboard();
sk.ShowDialog();
}
And in Form 2, here's what i have so far:
public Form1 otherForm = new Form1();
string hotkey = "";
public void hotkeyChanged(object sender, EventArgs e)
{
RadioButton rr = (RadioButton)sender;
switch (rr.Name)
{
case ("buttonF1"):
hotkey = "F1 ";
break;
}
}
public void buttonConfirmKey_Click(object sender, EventArgs e)
{
hotkey = otherForm.keyLabel.Text;
this.Close();
}
Where I have public Form1 otherForm = new Form1();
and hotkey = otherForm.keyLabel.Text; I found it here.
And it doesn't seem to be working, as when I press the button on form2, the form closes but the label in form1 doesn't change...
any ideas?
thanks
There are different approaches to do this. You could go like this :
Solution one:
(Don't forget to set the modifier for you label1 in this case to Public. You can set this in the designer options > under Properties > design)
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
}
Form 2:
public partial class Form2 : Form
{
private readonly Form1 _parent;
public Form2(Form1 parent)
{
InitializeComponent();
_parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
_parent.label1.Text = textBox1.Text;
Close();
}
}
Solution 2
Instead of setting label1 to public, leave it on private (as default) but set the DialogResult property of button1 on form 2 to "DialogResult OK" (under Properties > Behavior)
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
DialogResult res = frm.ShowDialog();
if (res == DialogResult.OK)
{
label1.Text = frm.MyNewText;
}
}
}
Form 2:
public partial class Form2 : Form
{
public string MyNewText;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyNewText = textBox1.Text;
Close();
}
}
in the constructor of the forms you can get the values like this :
in form2 you should add a constructor like this :
public partial class Form2: Form
{
public string _newvalue
public Form2(string value)
{
InitializeComponent();
_newvalue=value
}
//you should assign the value to the label .
}
in form1 you should do this :
form2 new=form2("sampletext");
new.showdialog();
Solution 1:
In Form1:
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.ShowDialog();
}
Codes in Form2:
Form frm_;
public Form2(Form frm)
{
InitializeComponent();
frm_ = frm;
}
private void btnInForm2_Click(object sender, EventArgs e)
{
Label lbl = (Label)frm_.Controls.Find("lblInForm1", true)[0];
string PassVal="What you want";
lbl.Text = PassVal;
}
Solution 2:
in Form 1:
Form2 f = new Form2();
if (f.ShowDialog() == DialogResult.OK)
{
lblInForm1.Text = f.PassVal;
}
in Form 2:
internal string PassVal = "";
PassVal is a Field.
Basically when a user enters data into a textfield on Form2, I want that data to be stored into a variable then when users selects the button enter, Form2 will hide and Form1 will appear. I then want Form1 to display the data entered in the textfield from Form2 in a new textfield.
This is my attempt, but it doesn't work
On Form 2...
public string Player1 {get; set;}
private void pvpPlay_Click(object sender, EventArgs e)
{
Player1 = txtPlayer1.Text;
Form1 op = new Form1();
op.Show();
this.Hide();
}
Then on Form1 to call this I put...
Form2 f = new Form2();
txtTest.Text = f.Player1;
But it doesn't work. Hopefully someone knows the answer.
I would prefer the using of a simple Callback function like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public String SomeProperty { get; set; }
private void OpenFormButton_Click(object sender, EventArgs e)
{
var secondForm = new Form2()
{
GetSomeProperty = () => { return SomeProperty ;};
};
this.Hide(); //The best way to hide!
secondForm.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
SomeProperty = "HELLO WORLD";
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Func<string> GetSomeProperty
{
get;
set;
}
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show(GetSomeProperty.Invoke());
}
}
Everytime you call GetSomeProperty.Invoke(); the Func will call the get accessor and return it from the first Form
What you can do is overload the Form1 constructor.
public Form1(string s)
{
txtTest.Text=s;
}
When calling from Form2
Form1 op = new Form1(Player1);
I have two forms named form1 and form2:
form1 is made of a label and a button.
form2 is made of a textBox and a button
When I click the form1 button, this will show up form2. Any inputs in textBox should be written back to form1.label once I hit the button in form2.
I have the code below but it doesn't work.
// Code from Form 1
public partial class Form1 : Form
{
public void PassValue(string strValue)
{
label1.Text = strValue;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);
this.Close();
}
}
And a screenshot:
How can I realize that?
You don't access your form1, from which you created form2. In form2 button1_Click you create new instance of Form1, which is not the same as initial. You may pass your form1 instance to form2 constructor like that:
// Code from Form 1
public partial class Form1 : Form
{
public void PassValue(string strValue)
{
label1.Text = strValue;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2(this);
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
Form1 ownerForm = null;
public Form2(Form1 ownerForm)
{
InitializeComponent();
this.ownerForm = ownerForm;
}
private void button1_Click(object sender, EventArgs e)
{
this.ownerForm.PassValue(textBox1.Text);
this.Close();
}
}
Like mentioned in other posts, you won't be able to reference the original Form1 by creating a new instance of Form1. You can pass Form1 into Form2's constructor or expose Form2's text as a public property, but I usually prefer using delegates for this to maintain loose coupling.
// Code from Form 1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.PassValue += new PassValueHandler(objForm2_PassValue);
objForm2.Show();
}
public void objForm2_PassValue(string strValue)
{
label1.Text = strValue;
}
}
// Code From Form 2
public delegate void PassValueHandler(string strValue);
public partial class Form2 : Form
{
public event PassValueHandler PassValue;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (PassValue != null)
{
PassValue(textBox1.Text);
}
this.Close();
}
}
When you are doing:
Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);
... you are creating a new Form1 and calling the PassValue method on the wrong Form1 object. Instead, you could do:
public partial class Form1 : Form
{
// This is the text that will be entered in form2
public String form2text;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Show form2
Form2 objForm2 = new Form2(this);
objForm2.ShowDialog();
// When form2 is closed, update the label text on form1
label1.Text = form2text;
}
}
public partial class Form2 : Form
{
// This is the instance of Form1 that called form2
private Form1 form1caller;
public Form2(Form1 form1caller)
{
InitializeComponent();
this.form1caller = form1caller;
}
private void button1_Click(object sender, EventArgs e)
{
// Pass the textBox value to form1 before closing form2
form1caller.form2text = textBox1.Text;
this.Close();
}
}
I just tried this code and it works, sure it will help you.
in the first form (Form1) type below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(textBox1.Text);
f.ShowDialog();
}
}
in the second form (Form2) use below codes:
public partial class Form2 : Form
{
public Form2( string st)
{
InitializeComponent();
textBox1.Text = st;
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
You could do this:
class Form2
{
public string ReturnedText = "";
private void button1_Click(object sender, EventArgs e)
{
ReturnedText = textbox1.Text;
Close();
}
}
and in form1
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
string ret = objForm2.ReturnedText;
You should pass reference on form1 to form2 instead of creating new instance in this code:
private void button1_Click(object sender, EventArgs e)
{
Form1 objForm1 = new Form1(); // ← this is another form1, not that you see
objForm1.PassValue(textBox1.Text);
this.Close();
}
The way that I normally approach this requirement is as follows:
I place a public property on the Form2 class:
public string ValueFromForm1 { get; set; }
//In the constructor, or other relevant method, I use the value
public Form2()
{
form2LabelToDisplayForm1Value.Text = ValueFromForm1;
}
In order to return something to Form1, you need to add a public property to the Form1 class to receive the value, and then send a reference to the form to Form2, so that Form2 can set the value:
//Add reference property to Form2 class
public Form1 CallingForm { get; set; }
//Form2 can access the value on Form1 as follows:
private someMethod()
{
this.CallingForm.ValueFromForm2 = "Info coming from form 2";
}
then
//Add public property to Form1 class
public string ValueFromForm2 { get; set; }
//When Form2 is created, set the reference property
Form2 objForm2 = new Form2();
objForm2.CallingForm = this;
objForm2.Show();
Since Form2 now has a reference to the Form1 that created, there is no need to call new Form1() anywhere in Form2. All Form2 has to do is set the value on the reference, and then close itself.
This is what you are going to do:
// Code from Form 1
public partial class Form1 : Form
{
public string MyValue { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.textBox1.Text = MyValue;
objForm2.MainForm = this;
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
public Form1 MainForm { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MainForm.MyValue = textBox1.Text;
MainForm.Show();
this.Close();
}
}
Form 1 code...:-
namespace Passing_values_from_one_form_to_other
{
public partial class Form1 : Form
{
string str;
private String value1;//taking values from form no _of_test_cases
public string value
{
get { return value1; }
set { value1 = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
str = f2.passvalue;
}
}
}
Form 2 code....:-
namespace Passing_values_from_one_form_to_other
{
public partial class Form2 : Form
{
private string str;
public string passvalue
{
get { return str; }
set { str = value; }
}
public Form2()
{
InitializeComponent();
}
private void Btn_Ok1_Click(object sender, EventArgs e)
{
passvalue = textBox1.Text;
this.Close();
}
}
}
directly execute it u will get the clear picture....same way u can pass values from one form to other...
post your comments if you face any issues...
hope this will help...
or else you can refer this video...
http://www.youtube.com/watch?v=PI3ad-TebP0