PDA

View Full Version : ComboBox - Please help!



Luke L
09-11-2012, 11:29 PM
Ok so i'm taking my first programming class in college, CMIS 142, programming in visual basic.

We have only had two labs , and all we've really learned how to do is code for click button events, check boses, and how to display output into text boxes.

Just for fun I've decided to build my own little equipment builder. It will have no real use besides to make it for fun. Here is what it looks like:
http://i45.tinypic.com/2ntbtk5.png

What I want it to do is, have like maybe two different choices in each drop down box. When I select the choice, I'm going to have a picture box for each item, that is over the main picture box (that you can see). It will be set to "not visible" to start off with, then when the item is selected from the combo box, I want it to then " = helmofnetiznotPictureBox.visible".

That way when it is selected, the picture will become visible and look as if it is "equipped".

Later on I'll be playing with the math to get the stats displayed in the display only text box under that main picturebox, but for now i'm stuck.

I got those things in the combo box list just by hitting "edit items" in the combo box little menu. But how do I code for the specific things that are in the combo box drop down box?

Like I said, I want to select one and have it make the picture visible.

I know for like a checkbox it would be "HelmofneitiznotCheckbox.checked = HelmofneitiznotPicturebox.visible"

But I want to have more then one option available, hence the combobox and not the checkbox. No experience with combo boxes, please help!

edit: also hopefully if someone can help me with that first, what method do I use to "clear" my combo boxes? I want my "Resetbutton" to return them to their blank default, but not sure how..

Runaway
09-11-2012, 11:38 PM
I don't know much about vb... but hopefully an analogy can help you out :p If you can check the "caption" (not entirely sure what it would be called in vb) then you could do something like:


// Assuming your ComboBoxes are stored in an array
if (ComboBoxes[0].caption = Lowercase('helm of neitiznot')) then
HelmofneitiznotPicturebox.visible;


And to clear it would be something alone the lines of:


for i := 0 to 10 do
ComboBoxes[i].caption := '';


Of course, this is all assuming that caption refers to the current value :pinch:

Luke L
09-11-2012, 11:44 PM
I don't know much about vb... but hopefully an analogy can help you out :p If you can check the "caption" (not entirely sure what it would be called in vb) then you could do something like:


// Assuming your ComboBoxes are stored in an array
if (ComboBoxes[0].caption = Lowercase('helm of neitiznot')) then
HelmofneitiznotPicturebox.visible;


And to clear it would be something alone the lines of:


for i := 0 to 10 do
ComboBoxes[i].caption := '';


Of course, this is all assuming that caption refers to the current value :pinch:

That was wayyy over my head :p. VisualBasic is very... basic :p.

It's really prebuilt. All I really know how to do is when I want to code for something, i.e. the reset button, I double click on it. Then it brings me to the code, and it shows the code for if that click event is fired. Then I just type my remarks and type things simply. I have really never done any "if then" or any other "statements"

The only "code" I have done for that whole thing is..

Public Class Form1

Private Sub ResetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetButton.Click
'Reset's the combo boxes for all equipment
'(make sure resetting all combo boxes to default also resets the pictures associated with them to not visible too)
StatsTextBox.Clear()

End Sub
End Class


^The top part is put there from me double clicking (or "firing") the click even, the reset button. Then the green of obviously my remarks, and the only "coding" I've done was to clear the textbox when I hit the reset button, which is the .Clear method I used. I'll have to include whatever methods to clear the combo boxes in that sub, but Idk how :p

euphemism
09-12-2012, 12:28 AM
I know when making a form in Simba, there's an "ItemIndex" property, which returns the current selected item in your combobox, so if you had two items, it would return 0 or 1 depending on which item is currently selected.

Luke L
09-12-2012, 12:47 AM
I know when making a form in Simba, there's an "ItemIndex" property, which returns the current selected item in your combobox, so if you had two items, it would return 0 or 1 depending on which item is currently selected.

Ok I'll poke around a bit and see what I can find, thanks!

edit: I did find a property of the Combobox named "Items", and then you click the "collection" and that has the list of stuff I added the other way, so I can add it that way. I'm just not sure how to refer to an item in my combo box, when trying to code for it.

Hopefully Kyle takes a look at this, I think i remember reading he was good at VB.

euphemism
09-12-2012, 02:17 AM
ComboBox.SelectedIndex returns the index of the currently selected item.

Brandon
09-12-2012, 03:31 AM
If ComboBox.SelectedValue IsNot Nothing Then
PictureBox.Image = ImageFromFile("C:/Brandon/Desktop/" & ComboBox.SelectedItem.ToString & ".bmp")


I think? I haven't done VB in basically years lol.

Or you can try iterating with a for loop and checking if the index is the same as the index of a picture. Load that picture into the thing I guess.

Luke L
09-12-2012, 09:55 PM
I figured it out!



Public Class Form1

Private Sub NameComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NameComboBox.SelectedIndexChanged
If HelmetComboBox.SelectedItem = "Verac's Helm" Then
VeracsHelmPictureBox.Visible = True
Else
VeracsHelmPictureBox.Visible = False
End If
End Sub
End Class


That works so that when an item is selected in my combobox drop down it makes that item visible, but if another item is selected then it will go back to not visible, hence giving it the "unequip" effect.