Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Changing User Control Properties during program run

$
0
0

Hi there,

I have a User Control that should change it's "Power" property at certain times, and this change makes the User Control's appearance also change. I read that you are not supposed to simply change the property with a line such as:

ClimatePower.Power = 1;

I don't really understand why that is, but oh well. This is the code behind my property:

private ulong toggled = 0;
public ulong Power
        {
            get
            {
                if (toggled == 1) { LED.BackColor = Color.FromArgb(0, 255, 255); }
                else { LED.BackColor = Color.FromArgb(64, 64, 64); }
                return (ulong)toggled;
            }
            set
            {
                toggled = (ulong)value;
                if (toggled == 1) { LED.BackColor = Color.FromArgb(0,255,255);} 
                else { LED.BackColor = Color.FromArgb(64,64,64);}
            }
        }

It's fairly simple. What happens is when the power property is changed, a label on the User Control changes color in order to show the "button" (User Control) is powered on.

I know that UI changes are supposed to be handled as such:

        private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);

        public static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object[] { control, propertyName, propertyValue });
            }
            else
            {
                control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
            

However, when I try to apply this method to my User Control (calling SetControlPropertyThreadSafe(ClimatePower, "Power", 1);) I get a Missing Method Exception. As you can see, the Power property clearly has both a get, and a set method. Can anyone provide some insight as to why this is happening?


Viewing all articles
Browse latest Browse all 12583

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>