-2

我从 3 个文本框制作用户控件,但我不知道如何向它声明只读属性我尝试了很多东西,但它在这里不起作用是我制作控件的代码

我想让它只在需要时只读,比如如果我添加我想要的复选框,如果 checkbox.check=true 使我的控件只读

 public partial class dateIN : UserControl
    {
        Dates datess = new Dates();
        public dateIN()
        {
            InitializeComponent();
        }

        private void dateIN_Leave(object sender, EventArgs e)
        {
            if (txtDay.Text != "" || txtMonth.Text != "" || txtYear.Text != "")
            {
                if (!datess.IsHijri(txtDay.Text.Trim() + "/" + txtMonth.Text.Trim() + "/" + txtYear.Text.Trim()))
                {
                    txtDay.Focus();
                }
            }
        }
        public string Day
        {
            set { txtDay.Text = value; }
            get { return txtDay.Text; }
        }
        public string Month
        {
            set { txtMonth.Text = value; }
            get { return txtMonth.Text; }
        }
        public string Year
        {
            set { txtYear.Text = value; }
            get { return txtYear.Text; }
        }

需要知道如何在此处提供只读属性

4

3 回答 3

1

只需删除set { }属性的一部分

例子:

public string Day
{
   get { return txtDay.Text; }
}
于 2013-05-28T08:45:11.613 回答
0

我不知道你的“txtDay”、“txtMonth”、“txtYear”来自哪里的相关性,但你可以做类似的事情

public partial class dateIN : UserControl
{
   ...
   ...

   private bool AllowEditing()
   { return SomeCondition when SHOULD be allowed...; }


   public string Day
   {
      // only allow the set to apply the change if the "AllowEditing" condition
      // is true, otherwise, ignore the attempt to assign.
      set { if( AllowEditing() )
               txtDay.Text = value; }
      get { return txtDay.Text; }
   }

   // same concept for month and year too
}
于 2013-05-28T11:52:37.650 回答
-2

so may you add some flag to your set when it is true then you set a value. also you can work with textbox property called ReadOnly.

于 2013-05-28T10:20:54.367 回答