Results 1 to 4 of 4

Thread: Editing controls on different forms (C# 2003)

  1. #1

    Thread Starter
    Member nci's Avatar
    Join Date
    Aug 2003
    Posts
    43

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. using (Form2 f2 = new Form2())
    2. {
    3.     // Set the initial text.
    4.     f2.TextBoxText = "initial value";
    5.  
    6.     if (f2.ShowDialog() == DialogResult.OK)
    7.     {
    8.         // Get the final value.
    9.         MessageBox.Show(f2.TextBoxText);
    10.     }
    11. }
    The TextBoxText property would be defined like this in Form2:
    C# Code:
    1. public string TextBoxText
    2. {
    3.     get
    4.     {
    5.         return this.textBox1.Text;
    6.     }
    7.     set
    8.     {
    9.         this.textBox1.Text = value;
    10.     }
    11. }
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member nci's Avatar
    Join Date
    Aug 2003
    Posts
    43

    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

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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
  •  



Click Here to Expand Forum to Full Width