6 Replies - 468 Views - Last Post: 05 February 2013 - 09:21 AM Rate Topic: -----

#1 triangulator  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-February 13

User control can't set the enabled property of its elements

Posted 05 February 2013 - 08:22 AM

I have a user control which is esentially a combination of a checkbox and a textbox. I want it so that if the checkbox is clicked, the textbox be active and otherwise don't. I have this code for the clicked property of the user control:
        [Category("Appearance")]
        [Description("Gets or sets the state of the checkbox.")]
        public bool Checked
        {
            get { return checkbx.Checked; }
            set { 
                checkbx.Checked = value;
                if (checkbx.Checked == false) textbx.Enabled = false;
                else textbx.Enabled = true;
            }
        }


In the designer I got six instances of this control. Four of them are checked, two aren't - in the designer everything is fine. But as soon as I click the start debugging button, the two disabled textboxes become enabled and while running clicking the checkboxes has no effect whatsover - the corresponding textboxes are always active. Why so?
I've builded the project multiple times.

Is This A Good Question/Topic? 0
  • +

Replies To: User control can't set the enabled property of its elements

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: User control can't set the enabled property of its elements

Posted 05 February 2013 - 08:26 AM

The first thing I would do is not to call the function Checked

public bool Checked
{

Was This Post Helpful? 0
  • +
  • -

#3 triangulator  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-February 13

Re: User control can't set the enabled property of its elements

Posted 05 February 2013 - 08:36 AM

Is 'public bool Checked' a function? I thought it's a property.
I've changed its name and there isn't any change in the behaviour of the program.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: User control can't set the enabled property of its elements

Posted 05 February 2013 - 08:43 AM

View Posttriangulator, on 05 February 2013 - 08:36 AM, said:

Is 'public bool Checked' a function? I thought it's a property.
I've changed its name and there isn't any change in the behaviour of the program.

Yes, for your current code, it is a Property.

Actually, perhaps it needs to be named Checked, in order to replicate the standard Checked property for a CheckBox(?). Not sure; I don't think you've implemented this correctly for a custom control. Hopefully someone else might contribute :)
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: User control can't set the enabled property of its elements

Posted 05 February 2013 - 08:49 AM

No, I believe we do need to change the name:

// expose the Checked Property of a checkbox (read/write)
  public bool CheckBoxProperty
  {
    get { return checkBox.Checked; }
    set { checkBox.Checked = value; }
  }

but, again, I think there is a little more required - to attach a CheckedChanged event to your custom control, that then uses your new property.
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: User control can't set the enabled property of its elements

Posted 05 February 2013 - 08:57 AM

Something like:

YourNewCheckbox.CheckedChanged += new EventHandler(this.CheckedChange);

protected void CheckedChange(object sender, EventArgs e)
{
    CheckBox x = (CheckBox)sender;
    // change your Checked property
}

.. waiting for someone else to confirm or deny :)/>

BUT I don't think your Checked property (or whatever you rename it to) should be connected to another controls. It should remain independent. I think it should be the event that changes the two properties.

This post has been edited by andrewsw: 05 February 2013 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 triangulator  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-February 13

Re: User control can't set the enabled property of its elements

Posted 05 February 2013 - 09:21 AM

Why do you think so? Anyway, I found the answer - the code in OP runs fine, changing the Enabled property. It was just me thinking that simply clicking the checkbox will call the code in OP. Quite naive from my side.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1