Greetings,

I'm a bit new at Visual Basic 2010, and i have one main problem. I want to creat a Calculator for a game, however i have issues getting it to work.

When i select a setting from my main Combobox i want it to add different settings for the other comboboxes, example:

I pick from my main Combobox - "Wizard"
On one of my other comboboxes i want these two settings to be pickable -
"Main-Hand + Source" and "Main-Hand + Shield"

Now if i instead of Wizard select "Barbarian"
i want my other combobox to remove the "Main-Hand + Source" and "Main-Hand + Shield" and instead add
"Main-Hand + Off-Hand" and "Main-Hand + Shield"

This should be automatically done using a timer. Here is my code so far:

Public Class Form1

Private Sub EnableTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnableTimer.Tick
    If ClassCombo.Text = "Wizard" Then
        WizardTimer.Enabled = True
    Else : WizardTimer.Enabled = False
    End If

    If ClassCombo.Text = "Barbarian" Then
        BarbTimer.Enabled = True
    Else : BarbTimer.Enabled = False
    End If

End Sub

Private Sub WizardTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WizardTimer.Tick
    BuildCombo.Items.Add("Main-Hand + Source")
    BuildCombo.Items.Add("Main-Hand + Shield")

    MainCombo.Items.Add("Spear")
    MainCombo.Items.Add("Sword")
    MainCombo.Items.Add("Wand")
    MainCombo.Items.Add("Mace")
    MainCombo.Items.Add("Axe")

    WizardTimer.Stop()
End Sub

Private Sub BarbTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BarbTimer.Tick
    BuildCombo.Items.Add("Main-Hand + Shield")
    BuildCombo.Items.Add("Main-Hand + Off-Hand")

    MainCombo.Items.Add("Spear")
    MainCombo.Items.Add("Sword")
    MainCombo.Items.Add("Mighty Weapon")
    MainCombo.Items.Add("Mace")
    MainCombo.Items.Add("Axe")

    BarbTimer.Stop()
End Sub

End Class

But if i do this, my comboboxes are just mass spammed with Spear, Spear, Spear, Spear, Sword, Sword, Sword, etc, etc, etc and so are my main-hand + off-hand etc.

Anyway to make this only add the items 1 time, then stop and if i switch it will remove those items and add the new 1 time?

hmm - now i can't select a thing from the combo ? it just re-markers it instant.

You don't want to use a timer for this. Instead, you should modify the contents of the second combobox when the user picks a new selection from the first combobox. Also, instead of hardcoding separate Adds for each selection, store the contents in a dictionary. That way the code doesn't have to be modified if you add people or weapons. In the following code I have still hardcoded the values in the Form Load, but they could easily be moved to a file and read in at run time.

Private weapons As New Dictionary(Of String, String())

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

    weapons.Add("Wizard", {"Main-Hand + Source", "Main-Hand + Shield"})
    weapons.Add("Barbarian", {"Main-Hand + Off-Hand", "Main-Hand + Shield"})

End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    Dim cbo As ComboBox = sender

    ComboBox2.Items.Clear()

    For Each weapon As String In weapons(cbo.Text)
        ComboBox2.Items.Add(weapon)
    Next

End Sub

Note that the dictionary (combo) uses the user selection as the key. The value is an array of weapons. The number of items in the array can vary. As the user selects a new person (Wizard, etc), the contents of the second combobox change to reflect the appropriate range of weapons.

Edited 4 Years Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.