.netwinformscheckboxoxygene

CheckBox is not accepting state change in the code


On a winform I have a three different checkboxes. They all won't accept any changes to their state programmatically but only by a mouse click. Why?

enter image description here

Here is how I am setting their state in my code:

 if i=1 then
  ClientChk.Checked := true
 else
  ClientChk.Checked := false;

 if i = 2 then
  HostChk.Checked := true
 else
  HostChk.Checked := false;

Solution

  • You've posted code with no context, and expect us to try and psychically debug it for you. I'll give it a shot, but you really need to learn to make your questions more complete.

    My guess is that somewhere in the code you didn't post, you're enabling/disabling ClientChk and HostChk based on whether the Enable Remote Mode box is checked, and when it's not checked (as in your image), the ClientChk and HostChk buttons are disabled. A quick trip through your code (or using the debugger) would tell in very quickly if that's the problem.

    You can improve your code somewhat, BTW (and handle enabling/disabling them based on the value of EnableRemoteChk:

    ClientChk.Enabled := EnableRemoteChk.Checked;
    HostChk.Enabled := ClientChk.Enabled;
    if ClientChk.Enabled then
    begin
      ClientChk.Checked := (i = 1);
      HostChk.Checked := (i = 2);
    end;