Changing state of label depending upon two checkboxs?

qadeer37

Well-known member
Joined
May 1, 2010
Messages
63
Location
Hyderabad,pakistan
Programming Experience
1-3
I have added two checkbox controls chkBox1 and chkBox2.
What I want to do is that when both the chkbox are same meaning checked or unchecked the label should be On and if both are not same label should be Off.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'and plz tell why this method is not working?
If chkBox1.Equals(chkBox2) Then

lblState.Text = "On"
Else
lblState.Text = "Off"

End If
End Sub
:(
 
Hi this code is working but I think there must be some other better way of doing this.


Private Sub chkBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBox1.CheckedChanged, chkBox2.CheckedChanged

If chkBox1.CheckState = CheckState.Checked And chkBox2.CheckState = CheckState.Checked Or _
chkBox1.CheckState = CheckState.Unchecked And chkBox2.CheckState = CheckState.Unchecked Then

lblState.Text = "On"

Else
lblState.Text = "Off"

End If
End Sub
 
VB.NET:
If chkBox1.Checked = chkBox2.Checked Then
    lblState.Text = If(chkBox1.Checked, "On", "Off")
End If
 
Try This one very easy and simple

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
Label1.Text = (CheckBox1.Checked = CheckBox2.Checked)
End Sub
 
The two previous replies had omitted something or another. The first one by mdusara works if you add ToString after the Boolean statement, but it only outputs True or False.

The second one by JohnH left out the = CheckBox2.Checked.

Here is the corrected code. In the Checked_Changed event, you can do either of these two:

VB.NET:
		If (CheckBox1.Checked = CheckBox2.Checked) Then
			Label1.Text = "On"
		Else
			Label1.Text = "Off"
		End If

or

VB.NET:
Label1.Text = If(CheckBox1.Checked = CheckBox2.Checked, "On", "Off")
 
No, the requirement is to display "on" if both checkboxes are checked and "off" if none is checked. Only my code solves this :)
 
No, JohnH. The op requirements were as follow:

What I want to do is that when both the chkbox are same meaning checked or unchecked the label should be On and if both are not same label should be Off.

Your code does not even address the option of when one checkbox is on and the other off. It should be as follows:

Both boxes checked -- ON
Both boxes unchecked -- ON
One box checked and one unchecked -- OFF

This is the exact opposite of using Xor between the two boxes, which would be coded as follows:
Label1.Text = If(CheckBox1.Checked <> CheckBox2.Checked, "On", "Off")
 
You could be right, but that is not how I read it. Anyway, OP does have some options now.
 
Solotaire had it exactly right. I think this is what you want:

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged

Label1.Text = CStr(IIf(Not (CheckBox1.Checked Xor CheckBox2.Checked), "ON", "OFF"))

End Sub

Of course you need to set Label1.Text to whatever you want the initial value to be.
 
i think that this is the code you are looking for :

VB.NET:
Label1.Text = IIf(CheckBox1.Checked And CheckBox2.Checked, "ON", "OFF")

Solotaire had it exactly right. I think this is what you want:

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged

Label1.Text = CStr(IIf(Not (CheckBox1.Checked Xor CheckBox2.Checked), "ON", "OFF"))

End Sub

Of course you need to set Label1.Text to whatever you want the initial value to be.
Why are you two using IIf()? You should be using just If(). Also tmostad, you wouldn't have to convert a string to a string if you use If() instead of IIf():
VB.NET:
Label1.Text = If(CheckBox1.Checked = CheckBox2.Checked, "ON", "OFF")
 
Doesn't Solitaire's code do it backwards? According to your first post and post #12, you want what I showed in post 11, not Solitaire's
 
I think this code is working fine.
If (CheckBox1.Checked = CheckBox2.Checked) Then
Label1.Text = "On"
Else
Label1.Text = "Off"
End If
correct me if I am wrong.
Isn't this better:
Solitaire said:
Label1.Text = If(CheckBox1.Checked = CheckBox2.Checked, "ON", "OFF")
That has same result with less typing and less code, without loosing clarity.
 
Back
Top