Reputation: 113
C# textbox sending text value. Situation, i got 2 forms. Form1 and Form2. Form1 got a textbox and form2 got a textbox and a button, i will put a text value on form2 textbox and when i click the form2 button the value of form2 textbox will be sent and change the form1 textbox value....Need help..
This is what ive done..im just gonna summarize it
Form1 got no codes just textbox1
This is the code in form2 button
private void change_Click(object sender, EventArgs e)
{
form1 frm1 = new form();
string test = textbox2.text
frm1.textbox.text = test;
}
ive try some poping message box to check if the value pass...and so far the value was really pass but no changes in the UI
Upvotes: 1
Views: 12479
Reputation: 11581
The reason your form1.textbox1 was not updated because you initialized a new instance of form1
form1 frm1 = new form();
So to update the form1 you have on screen, you need to get its instance injected into the form2. For instance, when you show form2, you set.
form2.Form1 = currentForm1Instance;
Hope this helps.
Upvotes: 0
Reputation: 337
you could use the .Tag property (look at my question here the simple way to do it is like this: you said that the textBox.text in form2 would replace the texBox.text in form1 right? do this in the form2
try
{
private void change_Click(object sender, EventArgs e)
{
form2 frm2 = new form();
frm2.Tag = this.textbox2.text;
frm2.ShowDialog();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
then write this when you load your form1
string myText = (string)this.Tag;
this.textbox1.text = myText;
Upvotes: 0
Reputation: 54532
You could rely on knowledge of Form1 in Form2 by making the TextBox public. But in my opinion the proper way to do it would to create a custom event handler, subscribe to it in Form2 and pass the text as a eventarg. Code adapted from this MSDN Article
Form1
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm2 = new Form2();
frm2.RaiseCustomEvent+=new CustomEventHandler(frm2_RaiseCustomEvent);
frm2.Show(this);
}
void frm2_RaiseCustomEvent(object sender, CustomEventArgs a)
{
textBox1.Text = a.Message;
}
}
}
Form2
namespace WindowsFormsApplication1
{
public delegate void CustomEventHandler(object sender, CustomEventArgs a);
public partial class Form2 : Form
{
public event CustomEventHandler RaiseCustomEvent;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
RaiseCustomEvent(this, new CustomEventArgs(textBox1.Text));
}
}
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(string s)
{
msg = s;
}
private string msg;
public string Message
{
get { return msg; }
}
}
}
Upvotes: 0
Reputation: 21191
Assuming you create Form2
as a child of Form1
(from within Form1
, do something like Form2 from = new Form2();
, you can access any public property of the child form from within the parent. So, just make sure to set the accessibility of the TextBox
to public
, and do something like this:
var form = new Form2();
form.ShowDialog();
this.TextBox1.Text = form.TextBox1.Text;
Upvotes: 2
Reputation: 18534
You could use events for this:
Define an interface:
public interface ITextChange
{
event EventHandler SomeTextChanged;
}
Then let you form with button implement this interface and fire the event on button click passing the value from the textbox as the first parameter:
public partial class Form1 : Form, ITextChange
{
public event EventHandler SomeTextChanged = delegate { };
public Form1 () {}
private void button1_Click(object sender, EventArgs e)
{
SomeTextChanged(textBox1.Text, null);
}
}
Pass an instance of this form to your second form like this:
public partial class Form2 : Form
{
public Form2(ITextChange f)
{
InitializeComponent();
f.SomeTextChanged += new EventHandler(f_SomeTextChanged);
}
void f_SomeTextChanged(object sender, EventArgs e)
{
textBox1.Text = sender.ToString();
}
}
So, when you create your Form2, you need to pass an instanse of Form1:
Form2 f = new Form2(form1);
As soon as you press the button, the textbox on the second form will automatically get the value.
P.S.: for more info, please, see Events Tutorial
Upvotes: 1
Reputation: 2708
You can declare the textbox in Form1 to be public, then you can access it from form2 by going form1.textBoxName.propertyName
Upvotes: 1