Question Checkbox to Listbox

hellboy2828

New member
Joined
Apr 5, 2009
Messages
3
Programming Experience
3-5
Good morning. I'm a new member to this forum, so I would like to say Hi to all the members. The forums seem to be very well organized and pleasing to the eyes.

--

Anyway, I need some help on checkboxes and listboxes. So here goes:

I need to make the text value of a checkbox appear on a listbox when I check it and when make it disappear when I uncheck it.

Simple, right?

However, I need to do this with 100 checkboxes, and doing it per checkbox, (I'll assume you guys will give me a code that handles only one checkbox) is quite tiring. So in short,

I need a code that for ANY checkbox checked and unchecked, the text value will be listed or cleared on a listbox.

--

I hope you guys understand what I'm getting at.

Thanks.
 
You can simply use the one method to handle the CheckedChanged event for all 100 CheckBoxes. In an event handler the 'sender' parameter refers to the object that raised the event, so that's your CheckBox:
VB.NET:
Dim cbx As CheckBox = DirectCast(sender, CheckBox)

If cbx.Checked Then
    Me.ListBox1.Items.Add(cbx.Text)
Else
    Me.ListBox1.Items.Remove(cbx.Text)
End If
To create the event handler you can just select all 100 CheckBoxes, open the Properties window, click the Events button and double-click the CheckedChanged event.

Having said all that, have you considered just using a CheckedListBox?
 
You can simply use the one method to handle the CheckedChanged event for all 100 CheckBoxes. In an event handler the 'sender' parameter refers to the object that raised the event, so that's your CheckBox:
VB.NET:
Dim cbx As CheckBox = DirectCast(sender, CheckBox)

If cbox.Checked Then
    Me.ListBox1.Items.Add(cbox.Text)
Else
    Me.ListBox1.Items.Remove(cbox.Text)
End If
To create the event handler you can just select all 100 CheckBoxes, open the Properties window, click the Events button and double-click the CheckedChanged event.

Having said all that, have you considered just using a CheckedListBox?

--

Okay so what I did was declare cbox as a universal variable as such,

Dim cbox As CheckBox = DirectCast(CheckBox1, CheckBox)

then selected 6 checkboxes and double clicked the checkchanged, so this is what I made:


Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged, CheckBox6.CheckedChanged, CheckBox5.CheckedChanged, CheckBox4.CheckedChanged, CheckBox3.CheckedChanged, CheckBox2.CheckedChanged, CheckBox1.CheckedChanged

If cbox.Checked Then
orderlist.Items.Add(cbox.Text)
Else
orderlist.Items.Remove(cbox.Text)
End If


End Sub


but I ended up with a nasty error

" Object reference not set to an instance of an object. "

Did I miss anything?
--

Sorry if I did something wrong, thanks for the help though. I'm learning.
 
You missed the whole point of my post, which was the 'sender' parameter in the event handler. There's no member variable involved. The code I provided was supposed to all be placed inside the event handler.
 
You missed the whole point of my post, which was the 'sender' parameter in the event handler. There's no member variable involved. The code I provided was supposed to all be placed inside the event handler.

--

Sheer brilliance.

I put everything inside the event handler like you said, and changed Checkbox1 to sender.

It worked.

I guess I need to "read" more attentively next time. Thanks jm. :)

--

Btw, I didn't use a checkedlistbox cause our professor forbid. Stupid woman told us to do something like this, but didn't even teach this to us, not even a hint on how to. I'm probably the only one able to do this in our class. lol. Anyways, thanks again.
 
Back
Top