|
-
Jul 30th, 2007, 07:47 PM
#1
Thread Starter
Member
Editing controls on different forms (C# 2003)
hi, i'm new to C# and was wondering how to do something.
I created a text box and a button on Form1. When I click the button, Form2 pops up and also has a text box and a button. I want the text i type in the textbox on Form2, to show up in the textbox of Form1 when I click a button.
I cant seem to be able to access the Form1 controls from Form2. Does anybody have a simple solution to this?
I can do it in any other language, i just cant figure it out in C#... lol
thanks for any help
-
Jul 30th, 2007, 09:06 PM
#2
Re: Editing controls on different forms (C# 2003)
What you ask for is quite possible but generally the wrong way to go about it. Firstly, forms should not be accessing controls on other forms directly. When you add a control to a form it is private by default and it should stay that way. You can then provide public properties or methods to allow access to specific aspects of the control(s). In your case you might do this on Form1:
C# Code:
using (Form2 f2 = new Form2())
{
// Set the initial text.
f2.TextBoxText = "initial value";
if (f2.ShowDialog() == DialogResult.OK)
{
// Get the final value.
MessageBox.Show(f2.TextBoxText);
}
}
The TextBoxText property would be defined like this in Form2:
C# Code:
public string TextBoxText
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value;
}
}
So Form1 will create a new instance of Form2 and set its TextBoxText property. In Form2 that property value is passed on and displayed in the TextBox. Form1 then displays the Form2 instance as a modal dialogue. If the user presses the OK button Form1 will then retrieve the TextBoxText property value from Form2 and display it. In Form2 that property value is retrieved from the TextBox. In that way Form1 is able to set and retrieve the Text of the TextBox on Form2 without ever knowing the TextBox itself exists.
-
Jul 31st, 2007, 07:59 AM
#3
Thread Starter
Member
Re: Editing controls on different forms (C# 2003)
ok, i tried the public string TextBoxText property, but the textbox.text value is not modified. I threw in a messagebox to test it, and the message box shows what is supposed to go in the text box, but for some reason, the textbox ignores the value... ?
Code:
public string TextBoxText
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value; // value is not changed!?
MessageBox.Show(value;) //---------shows the right value
}
}
I must be missing something. thanks for any help
-
Jul 31st, 2007, 09:35 AM
#4
Re: Editing controls on different forms (C# 2003)
Are you actually setting the TextBox text on your Form1 to the text in the TextBox on Form2 like you want to? You just need to replace the MessageBox line in jmc's code above.
Code:
using (Form2 f2 = new Form2())
{
// Set the initial text.
f2.TextBoxText = "initial value";
if (f2.ShowDialog() == DialogResult.OK)
{
// Get the final value.
this.textBox1.Text = f2.TextBoxText;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|