How to access properties of a user control using C

2019-09-27 00:35发布

问题:

This question already has an answer here:

  • How to access properties of a usercontrol in C# 5 answers

I've made a C# user control with one textbox.

How can I access the property of read only from outside the user control. thanks

回答1:

expose your textbox as a public property in the userControl and access it wherever you have used the usercontrol.

for example:

public class MyUserControl : UserControl
    {
           public TextBox MyTextBox 
           {
               get
               {
                  return txtBox1;
               }
               set
               {
                   txtBox1 = value;
               } 
           }
    }

and to make it readonly do this:

myUserControl.MyTextBox.ReadOnly=true; //where myUserControl is instance of 
                                       //MyUserControl, you have used somewhere