Accessing objects
-
Heyas all, Really trying my hardest to take all this information in. I come from a unix C environment and I'm finding the OO paradigm very aawkward so far, but I'm gonna keep it. My question is, If say I have a label on a form, and on a second form, I have a button that when clicked will populate the label with the text in a textbox from the first form. basically how do go about making the objects on a seperate form available from within another form. That sounds really confusing. I hope someone understands what I trying to say. P.S. - Yes, English is my first language :-) kha0s "There are 10 types of people in this world; Those that know binary and those that do not."
-
Heyas all, Really trying my hardest to take all this information in. I come from a unix C environment and I'm finding the OO paradigm very aawkward so far, but I'm gonna keep it. My question is, If say I have a label on a form, and on a second form, I have a button that when clicked will populate the label with the text in a textbox from the first form. basically how do go about making the objects on a seperate form available from within another form. That sounds really confusing. I hope someone understands what I trying to say. P.S. - Yes, English is my first language :-) kha0s "There are 10 types of people in this world; Those that know binary and those that do not."
Each form you create in .net is in itself a class so if you instantiate an object of one form within the code behind for the other form you will be able to access it's components ie: Within the Form2 code write Form1 myForm1 = new Form1(); Then also within the Form2 code write textboxname.DataSource = myForm1.Text.ToString(); and it should put the data in the approppriate place Please not what I have written is very basic there is a large amount of help for questions like yours in places like this website and msdn so don't be shy dig right in an get an early understanding of OOP with .net
-
Each form you create in .net is in itself a class so if you instantiate an object of one form within the code behind for the other form you will be able to access it's components ie: Within the Form2 code write Form1 myForm1 = new Form1(); Then also within the Form2 code write textboxname.DataSource = myForm1.Text.ToString(); and it should put the data in the approppriate place Please not what I have written is very basic there is a large amount of help for questions like yours in places like this website and msdn so don't be shy dig right in an get an early understanding of OOP with .net
Why are you calling
Text.ToString()
? TheText
property is already aString
. CallingToString()
is a waste.Microsoft MVP, Visual C# My Articles
-
Heyas all, Really trying my hardest to take all this information in. I come from a unix C environment and I'm finding the OO paradigm very aawkward so far, but I'm gonna keep it. My question is, If say I have a label on a form, and on a second form, I have a button that when clicked will populate the label with the text in a textbox from the first form. basically how do go about making the objects on a seperate form available from within another form. That sounds really confusing. I hope someone understands what I trying to say. P.S. - Yes, English is my first language :-) kha0s "There are 10 types of people in this world; Those that know binary and those that do not."
You need to pass an instance of the first form to your second form. For example:
public class Form1 : Form
{
internal Label label1;
private Button button1;
public Form1()
{
label = new Label();
button1 = new Button();
// Initialize...
button1.Click += new EventHandler(button1_Click);
}
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form = new Form2(this))
form.ShowDialog(this);
}
}
public class Form2 : Form
{
private TextBox textBox1;
private Button button1;
private Form1 form;
public Form2(Form1 form)
{
this.form = form;
textBox1 = new TextBox();
button1 = new Button();
// Initialize....
button1.Click += new EventHandler(button1_Click);
}
private void button1_Click(object sender, EventArgs e)
{
if (form != null) form.Label.Text = textBox1.Text;
}
}Microsoft MVP, Visual C# My Articles