Question:
In Visual Basic 08 how do you add the text next to a check box using a button?
Jack
2009-05-12 05:23:54 UTC
The layout of my form:
- I've got three radio buttons that correspond to three list boxes (when a radio button is selected the corresponding list box changes colour, to let the user know which list box is selected).
- I've also got a groupbox that only contains 20 checkboxes (these checkboxes represent items that I want to be able to individually add to a selected list box) and I 've also got a button "add item to list".

Names of items/objects in my program:
- lbxOven1, lbxOven2 and lbxOven3 (the list boxes)
- rdbOven1, rdbOven2 and rdbOven3 (the radio buttons)
- gbxbakeitemstimes (the groupbox)
- btnAddtooven (the add selected item button)
- and my checkboxes start with the prefix chkbx

What I want to do:
- I want to be able to select any check box and/or multiple check boxes and click the "add item to list" button, to add that item/those items (the text next to the checkboxes) to the list box selected by the corresponding radio button.

I'm also bad at programming, so please take that into account.
Thanks in advance.
Three answers:
anonymous
2009-05-12 05:35:19 UTC
Here is the basics:



if (chkbx1.Checked) then

if (rdbOven1.checked)

lbxOven1.Items.Add(chkbx1.Caption)

else if (rdbOven1.checked)

lbxOven2.Items.Add(chkbx1.Caption)

else

lbxOven3.Items.Add(chkbx1.Caption)

endif

end if





This would all go in the add button item handler. You can add a handler by double clicking the button in design mode for the form.



You don't really care about the group box in this implementation as it is just for visual grouping purposes
?
2009-05-12 05:35:49 UTC
In each forms properties there is a controls collection which contains a list of every object added to a form.



You can use a

"For each Obj as Object in MyForm.Controls" to loop through each object

.

As each object is read determine if it is a check box

if it is a check box by comparing the obj to a checkbox variable



if the obj is a checkbox then assign the obj to the checkbox variable so that you can expose the method and properties



using the now copied checkbox look at its checked property and if its checked you run code that will add its text to the appropriate listbox.
anonymous
2009-05-12 05:32:46 UTC
checkBox1.Caption = "new item"

or

checkBox1.Caption = sItem1



if it hasn't been changed. (The Caption property is the text you see.)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...