[RESOLVED] [3.0/LINQ] Get Value from Other Form
Hi, I've tried a lot of manipulations to get the current data in a textbox from other form but it always return an empty. How to get the recent value? below is part of the code i hv started:
Code:
--code in Form2
Form1 frm1 = new Form1();
frm2txtbox.text=frm1.frm1txtbox.text;
i keep changing the text value of frm1txtbox at run time, but still frm2txtbox.text still empty. i've already set the modifier of the frm1txtbox into Public. need ur advise..
Re: [3.0/LINQ] Get Value from Other Form
Hey,
By default, TextBox are set to private in C#, so in order to access the control on another form, you either have to change the protection level of the form, or create a public property that will set and get the text.
The code that you have above will always return null, since you are creating a new instance of Form2, which will always reset the Text Property of the form. However, I am going to assume that above it not all of the code.
Here is one way of doing it, but bear in mind there are other ways:
Main Form Code:
Code:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SecondForm secondForm = new SecondForm();
secondForm.Owner = this;
secondForm.Show();
}
public string TextBoxText
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value;
}
}
}
Second Form Code:
Code:
public partial class SecondForm : Form
{
public SecondForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MainForm mainForm = (MainForm)this.Owner;
mainForm.TextBoxText = this.textBox1.Text;
}
}
Here I have used the Owner Property to expose a reference to the first form from the second one.
That make sense?
Gary
Re: [3.0/LINQ] Get Value from Other Form
Thank u Gray.By your help i solved my problem
Re: [3.0/LINQ] Get Value from Other Form
Hey,
Glad to hear it.
If you question has been answered, remember to mark your thread as resolved. If you are unsure how to do that, there is a link in my signature explaining how.
Gary
Re: [3.0/LINQ] Get Value from Other Form
Re: [RESOLVED] [3.0/LINQ] Get Value from Other Form
Ha ha, when tuhin responded, I just assumed that they were the original poster of the thread :) Glad it is working for you as well jbatman.
Gary