-2

i make user control from 3 text boxes but i don not how to declare read only property to it i tried many things but it do not work here is my code to make the control

i want to make it read only when needed like if i add checkbox i want if checkbox.check=true make my control readonly

 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; }
        }

need to know how to make read only property available here plz

dark knight
  • 11
  • 1
  • 6
  • possible duplicate of [How to access properties of a user control using C#](http://stackoverflow.com/questions/16787239/how-to-access-properties-of-a-user-control-using-c-sharp) – Cody Gray - on strike May 28 '13 at 08:46

3 Answers3

1

just remove the set { } part of the property

Example:

public string Day
{
   get { return txtDay.Text; }
}
Ted
  • 3,985
  • 1
  • 20
  • 33
  • ted u miss understand me i want to make it read only in some cases only ex : add check box on the form & if checkbox.checked = true i want my control to be aread only control – dark knight May 28 '13 at 09:01
0

I dont know the correlation of where your "txtDay", "txtMonth", "txtYear" come from, but you could do something like

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
}
DRapp
  • 47,638
  • 12
  • 72
  • 142
-1

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.

PPPP
  • 1