none
Set Form TextBox value From User control RRS feed

  • Question

  • Hello,

    Can anyone tell me how can I set the value of textbox present on form from user control

    • Moved by CoolDadTx Friday, March 11, 2016 3:32 PM Winforms related
    Friday, March 11, 2016 10:39 AM

Answers

  • Hello,

    solved my problem, Below code is for others to refer

     TextBox myform1textbox1 = (ParentForm.Controls["textbox1"] as TextBox);
     MessageBox.Show(myform1textbox1.Text);

    Saturday, March 12, 2016 6:17 AM
  • I asume you are using WinForms. Also the textbox is contained in a usercontrol. And you want to set the textbox from outside said usercontrol. There are two basic ways, both involve modifying the user control:

    1. Expose the usercontrol directly via a public property to anyone holding a reference on the user control. Maybe use a readonly property to protect it from outside damage (don't want any other code to set it).

    2. Add a public function that takes a string and writes it to the Textbox to the user control.

    In the end it is the same question as writing one Form from another Form, except you are much more likely to already have a reference to a usercontrol.
    All the encapsualtion problems apply.

    Friday, March 11, 2016 1:26 PM
  • From your code, it looks like you are setting the Form's textbox1 from the UserControl. While what you've done will work, it assumes that the UserControl will only ever be dropped on a Form where you're interested in a TextBox named "textbox1". IOW, it's not reusable on any Form that doesn't have a "textbox1".

    Now, that may be OK for your purposes, but in order to make your UserControl more flexible, I recommend using Intefaces. IOW, this UserControl should only be used on a Form that implements a specific interface. Here's an example of what I mean:

    // Interface
    public interface IMyTextBox
    {
        TextBox MyTextBox { get; set; }
    }
    
    // Form
    public partial class Form1 : Form, IMyTextBox
    {
    ...
        public TextBox MyTextBox { get; set; }
    ...
        public Form1()
        {
            InitializeComponent();
            this.MyTextBox = this.txtWhatever; // doesn't have to be textbox1
        }
    }
    
    // UserControl:
    
    // You might want to also check that the ParentForm does indeed implement the interface
    // before trying to cast it
    TextBox myformtextbox = ((IMyTextBox)ParentForm).MyTextBox;
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, March 13, 2016 4:56 PM

All replies

  • I asume you are using WinForms. Also the textbox is contained in a usercontrol. And you want to set the textbox from outside said usercontrol. There are two basic ways, both involve modifying the user control:

    1. Expose the usercontrol directly via a public property to anyone holding a reference on the user control. Maybe use a readonly property to protect it from outside damage (don't want any other code to set it).

    2. Add a public function that takes a string and writes it to the Textbox to the user control.

    In the end it is the same question as writing one Form from another Form, except you are much more likely to already have a reference to a usercontrol.
    All the encapsualtion problems apply.

    Friday, March 11, 2016 1:26 PM
  • Hello,

    solved my problem, Below code is for others to refer

     TextBox myform1textbox1 = (ParentForm.Controls["textbox1"] as TextBox);
     MessageBox.Show(myform1textbox1.Text);

    Saturday, March 12, 2016 6:17 AM
  • From your code, it looks like you are setting the Form's textbox1 from the UserControl. While what you've done will work, it assumes that the UserControl will only ever be dropped on a Form where you're interested in a TextBox named "textbox1". IOW, it's not reusable on any Form that doesn't have a "textbox1".

    Now, that may be OK for your purposes, but in order to make your UserControl more flexible, I recommend using Intefaces. IOW, this UserControl should only be used on a Form that implements a specific interface. Here's an example of what I mean:

    // Interface
    public interface IMyTextBox
    {
        TextBox MyTextBox { get; set; }
    }
    
    // Form
    public partial class Form1 : Form, IMyTextBox
    {
    ...
        public TextBox MyTextBox { get; set; }
    ...
        public Form1()
        {
            InitializeComponent();
            this.MyTextBox = this.txtWhatever; // doesn't have to be textbox1
        }
    }
    
    // UserControl:
    
    // You might want to also check that the ParentForm does indeed implement the interface
    // before trying to cast it
    TextBox myformtextbox = ((IMyTextBox)ParentForm).MyTextBox;
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, March 13, 2016 4:56 PM