如何使用 C# 访问用户控件的属性 [英] How to access properties of a user control using C#

查看:37
本文介绍了如何使用 C# 访问用户控件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个 textbox 制作了一个 C# 用户控件.

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

推荐答案

将您的文本框作为 userControl 中的公共属性公开,并在您使用过 usercontrol 的任何地方访问它.

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

例如:

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

这篇关于如何使用 C# 访问用户控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆