I have all the same list for each ComboBox. I have 10 ComboBoxs. I want it to set variables (10 again), depending on the item selected for each ComboBox. Is there a simpler way to do this? Like a function?
Printable View
I have all the same list for each ComboBox. I have 10 ComboBoxs. I want it to set variables (10 again), depending on the item selected for each ComboBox. Is there a simpler way to do this? Like a function?
give some more information pls, if u tried any code pls post it too.
I think using a control array for Combo boxes is what you want.... And another array of strings for storing the selected text in Comboboxes... See the below code... I used a control array for Combo1 (3 nos)... :wave:
Code:Option Explicit
Dim strComboValues(2) As String '~~> Array for storing the selected items in Comboboxes
Private Sub Combo1_Click(Index As Integer) '~~~> On selecting values in any of the Combobox
strComboValues(Index) = Combo1(Index).Text '~~~> Saving the text into array
End Sub
Private Sub Form_Load() '~~> Initialize the Combos with some text, for testing...
Dim i As Integer
For i = 0 To 2 '~~> For 3 combobox control arrays
Combo1(i).AddItem "aa" '~~> Dummy text for testing
Combo1(i).AddItem "ab" '~~> Dummy text for testing
Combo1(i).AddItem "ac" '~~> Dummy text for testing
strComboValues(i) = ""
Next i
End Sub
No thats not what i mean. I have this code:
The variables i have are the combo name (A, B, C, and so on). I want to get the name of the key changed to the GetAsyncKey number.Code:Private Sub Form_Load()
ComboA.AddItem "A"
ComboA.AddItem "B"
ComboA.AddItem "C"
ComboA.AddItem "D"
ComboA.AddItem "E"
ComboA.AddItem "F"
ComboA.AddItem "G"
ComboA.AddItem "H"
ComboA.AddItem "I"
ComboA.AddItem "J"
ComboA.AddItem "K"
ComboA.AddItem "L"
ComboA.AddItem "M"
ComboA.AddItem "N"
ComboA.AddItem "O"
ComboA.AddItem "P"
ComboA.AddItem "Q"
ComboA.AddItem "R"
ComboA.AddItem "S"
ComboA.AddItem "T"
ComboA.AddItem "U"
ComboA.AddItem "V"
ComboA.AddItem "W"
ComboA.AddItem "X"
ComboA.AddItem "Y"
ComboA.AddItem "Z"
ComboA.AddItem "0"
ComboA.AddItem "1"
ComboA.AddItem "2"
ComboA.AddItem "3"
ComboA.AddItem "4"
ComboA.AddItem "5"
ComboA.AddItem "6"
ComboA.AddItem "7"
ComboA.AddItem "8"
ComboA.AddItem "9"
ComboA.AddItem "SpaceBar"
ComboA.AddItem "Shift"
ComboA.AddItem "Enter"
ComboA.AddItem "Tab"
ComboA.AddItem "NumPad0"
ComboA.AddItem "NumPad1"
ComboA.AddItem "NumPad2"
ComboA.AddItem "NumPad3"
ComboA.AddItem "NumPad4"
ComboA.AddItem "NumPad5"
ComboA.AddItem "NumPad6"
ComboA.AddItem "NumPad7"
ComboA.AddItem "NumPad8"
ComboA.AddItem "NumPad9"
ComboA.AddItem "UpArrow"
ComboA.AddItem "DownArrow"
ComboA.AddItem "RightArrow"
ComboA.AddItem "LeftArrow"
ComboB.AddItem "A"
ComboB.AddItem "B"
ComboB.AddItem "C"
ComboB.AddItem "D"
ComboB.AddItem "E"
ComboB.AddItem "F"
ComboB.AddItem "G"
ComboB.AddItem "H"
ComboB.AddItem "I"
ComboB.AddItem "J"
ComboB.AddItem "K"
ComboB.AddItem "L"
ComboB.AddItem "M"
ComboB.AddItem "N"
ComboB.AddItem "O"
ComboB.AddItem "P"
ComboB.AddItem "Q"
ComboB.AddItem "R"
ComboB.AddItem "S"
ComboB.AddItem "T"
ComboB.AddItem "U"
ComboB.AddItem "V"
ComboB.AddItem "W"
ComboB.AddItem "X"
ComboB.AddItem "Y"
ComboB.AddItem "Z"
ComboB.AddItem "0"
ComboB.AddItem "1"
ComboB.AddItem "2"
ComboB.AddItem "3"
ComboB.AddItem "4"
ComboB.AddItem "5"
ComboB.AddItem "6"
ComboB.AddItem "7"
ComboB.AddItem "8"
ComboB.AddItem "9"
ComboB.AddItem "SpaceBar"
ComboB.AddItem "Shift"
ComboB.AddItem "Enter"
ComboB.AddItem "Tab"
ComboB.AddItem "NumPad0"
ComboB.AddItem "NumPad1"
ComboB.AddItem "NumPad2"
ComboB.AddItem "NumPad3"
ComboB.AddItem "NumPad4"
ComboB.AddItem "NumPad5"
ComboB.AddItem "NumPad6"
ComboB.AddItem "NumPad7"
ComboB.AddItem "NumPad8"
ComboB.AddItem "NumPad9"
ComboB.AddItem "UpArrow"
ComboB.AddItem "DownArrow"
ComboB.AddItem "RightArrow"
ComboB.AddItem "LeftArrow"
ComboC.AddItem "A"
ComboC.AddItem "B"
ComboC.AddItem "C"
ComboC.AddItem "D"
ComboC.AddItem "E"
ComboC.AddItem "F"
ComboC.AddItem "G"
ComboC.AddItem "H"
ComboC.AddItem "I"
ComboC.AddItem "J"
ComboC.AddItem "K"
ComboC.AddItem "L"
ComboC.AddItem "M"
ComboC.AddItem "N"
ComboC.AddItem "O"
ComboC.AddItem "P"
ComboC.AddItem "Q"
ComboC.AddItem "R"
ComboC.AddItem "S"
ComboC.AddItem "T"
ComboC.AddItem "U"
ComboC.AddItem "V"
ComboC.AddItem "W"
ComboC.AddItem "X"
ComboC.AddItem "Y"
ComboC.AddItem "Z"
ComboC.AddItem "0"
ComboC.AddItem "1"
ComboC.AddItem "2"
ComboC.AddItem "3"
ComboC.AddItem "4"
ComboC.AddItem "5"
ComboC.AddItem "6"
ComboC.AddItem "7"
ComboC.AddItem "8"
ComboC.AddItem "9"
ComboC.AddItem "SpaceBar"
ComboC.AddItem "Shift"
ComboC.AddItem "Enter"
ComboC.AddItem "Tab"
ComboC.AddItem "NumPad0"
ComboC.AddItem "NumPad1"
ComboC.AddItem "NumPad2"
ComboC.AddItem "NumPad3"
ComboC.AddItem "NumPad4"
ComboC.AddItem "NumPad5"
ComboC.AddItem "NumPad6"
ComboC.AddItem "NumPad7"
ComboC.AddItem "NumPad8"
ComboC.AddItem "NumPad9"
ComboC.AddItem "UpArrow"
ComboC.AddItem "DownArrow"
ComboC.AddItem "RightArrow"
ComboC.AddItem "LeftArrow"
' I do this for the other combos (Combo X, Y, Z, U, D, L, R)
End Sub
Create a function with that code inside, maybe something like this....
Now just call your sub like thisCode:Private Sub InitializeCombo(cboTarget As ComboBox)
cboTarget.AddItem "A"
cboTarget.AddItem "B"
cboTarget.AddItem "C"
cboTarget.AddItem "D"
' etc copy and paste your other ComboA lines & change ComboA to cboTarget
End Sub
Now you may want to use indexed comboboxes so this becomes much simpler. By indexed, I mean each combobox has the same name and its Index property is 0 thru 9 for the 10 comboboxes. Doing it this way will greatly reduce your overall coding since you won't have 10 different sets of events for each combobox, but one set of events with an Index parameter. Let's say that one name was ComboKeysCode:InitializeCombo ComboA
InitializeCombo ComboB
' etc
Instead of 10 ComboA_Click(), ComboB_Click() etc
You'd just have one: ComboKeys_Click(Index As Integer)
Now your original code could look like this:
Code:Dim I As Integer
For I = 0 To 9
ComboKeys(I).AddItem "A"
ComboKeys(I).AddItem "B"
ComboKeys(I).AddItem "C"
ComboKeys(I).AddItem "D"
ComboKeys(I).AddItem "E"
ComboKeys(I).AddItem "F"
ComboKeys(I).AddItem "G"
ComboKeys(I).AddItem "H"
ComboKeys(I).AddItem "I"
ComboKeys(I).AddItem "J"
ComboKeys(I).AddItem "K"
ComboKeys(I).AddItem "L"
ComboKeys(I).AddItem "M"
ComboKeys(I).AddItem "N"
ComboKeys(I).AddItem "O"
ComboKeys(I).AddItem "P"
ComboKeys(I).AddItem "Q"
ComboKeys(I).AddItem "R"
ComboKeys(I).AddItem "S"
ComboKeys(I).AddItem "T"
ComboKeys(I).AddItem "U"
ComboKeys(I).AddItem "V"
ComboKeys(I).AddItem "W"
ComboKeys(I).AddItem "X"
ComboKeys(I).AddItem "Y"
ComboKeys(I).AddItem "Z"
ComboKeys(I).AddItem "0"
ComboKeys(I).AddItem "1"
ComboKeys(I).AddItem "2"
ComboKeys(I).AddItem "3"
ComboKeys(I).AddItem "4"
ComboKeys(I).AddItem "5"
ComboKeys(I).AddItem "6"
ComboKeys(I).AddItem "7"
ComboKeys(I).AddItem "8"
ComboKeys(I).AddItem "9"
ComboKeys(I).AddItem "SpaceBar"
ComboKeys(I).AddItem "Shift"
ComboKeys(I).AddItem "Enter"
ComboKeys(I).AddItem "Tab"
ComboKeys(I).AddItem "NumPad0"
ComboKeys(I).AddItem "NumPad1"
ComboKeys(I).AddItem "NumPad2"
ComboKeys(I).AddItem "NumPad3"
ComboKeys(I).AddItem "NumPad4"
ComboKeys(I).AddItem "NumPad5"
ComboKeys(I).AddItem "NumPad6"
ComboKeys(I).AddItem "NumPad7"
ComboKeys(I).AddItem "NumPad8"
ComboKeys(I).AddItem "NumPad9"
ComboKeys(I).AddItem "UpArrow"
ComboKeys(I).AddItem "DownArrow"
ComboKeys(I).AddItem "RightArrow"
ComboKeys(I).AddItem "LeftArrow"
Next
No. Thats not what i mean! The code is fine for the add item. I already have done that. What i need is to know if there is a different way to code the way to tell what the combobox.text is and with the key that is stated, make a number and have a variable = that number. like
I need to know if there is a simpler way of codeing that above.Code:If ComboA.Text = "A" then
a = 'whatever the number for the key a in the GetAsyncKey thing is
Elseif ' i would do that for the rest of the keys in each ComboBox
End If
Use the combobox's .ItemData property when you add items. You can store numbers there. This way when you click a combobox item, you can get its "value" like so:
myValue = ComobA.ItemData(ComboA.ListIndex)
Code:ComboA.AddItem "A"
ComboA.ItemData(ComboA.NewIndex) = vbKeyA
is vbKeyA equal to
Is it equal to the value 65?Code:If GetAsyncKeyState(65) <> 0 then
'whatever
End If
Test it yourself, add this line and execute in your Immediate/Debug Window. Open that window with Ctrl+G.
Print vbKeyA
Want to view the values of the various vbKeyxxx constants?
1. Open Object Browser (F2)
2. Type in the search combobox: vbKeyA
3. Search
4. Look at the right lower window pane, there are the constants
5. Click on one and look at the bottom of the window and you will see its value in decimal and hex.
so wait, where do i put that line of code?
You must first add the value to the .ItemData property as I showed in post #7 above.
Here is how you can use it in your sample statement you provided in post #8:
Code:If GetAsyncKeyState(ComboA.ItemData(ComboA.ListIndex)) <> 0 then
...
Ok.... But im going to have a huge project that i would have to type in that whole thing in the () of the getAsyncKeyState..... Is there a way to make a variable = ComboA.ItemData(ComboA.ListIndex)) ?
Yes, use a form-level or module-level variable.
Form-Level if only used in that form
Or module-level placed in a code module and made Public so it can be used by more than one formCode:' in your form's declaration section
Dim theKey As Integer
Then you simply set theKey = ComboA.ItemData(ComboA.ListIndex)) and use theKey in your calls.Code:Public theKey As Integer
Ah. Ok. Could i put A= then what you have insted of theKey?
I think I am losing your train of thought. If you want to declare a variable as A, you can. Test your ideas out and if you have problems, post them or resolve the thread as appropriate.
here is my train of thoght. I am making a Button configuration for games. I want them to pick from the buttons from a list. Then, useing a variable like A for the A button in the getAsyncKeyState(A) for the code instead of setting only certain keys they can use.
can i do the Select Case with a combobox?
Yes
Code:Select Case ComboA.ListIndex
Case 0 ' first item selected
Case 1 ' second item selected
' etc
End Select
An idea, if I'm on the right track. Your 10 "buttons" are for commands, like Jump, Run, Fire, etc, etc. Correct? If so, maybe this can be of use.
I'm not a game engineer and others may have a better or more efficient idea. The above was provided as food for thought.Code:' in declarations section
Dim colShortCuts As Collection
Private Sub Form_Load()
Set colShortCuts = New Collection
colShortCuts.Add "Jump", CStr(vbKeyJ) ' default J for command: Jump
colShortCuts.Add "Run", CStr(vbKeyR) ' default R for command: Run
' add your other 7 command names and default keys
' and last one we will say is for Fire
colShortCuts.Add "Fire", CStr(vbKeyF) ' default F for command: Fire
' no 2 shortcuts can have the same default
End Sub
' if ComboA was for the Jump command
Private Sub ComboA_Click()
' Jump is the 1st item in the collection. We cannot simply change the collection object, we must delete it then re-add it
On Error Resume Next
colShortCuts.Add "Jump", CStr(ComboA.ItemData(ComboA.ListIndex)), 1
If Err Then
MsgBox "You cannot use that key, it is used for another command"
Else
colShortCuts.Remove 2 ' remove the old 1st item, which is now the 2nd item
End If
End Sub
' if ComboB was for the Run command
Private Sub ComboB_Click()
' Run is the 2nd item in the collection. We cannot simply change the collection, we must delete it then re-add it
On Error Resume Next
colShortCuts.Add "Run", CStr(ComboB.ItemData(ComboB.ListIndex)), 2
If Err Then
MsgBox "You cannot use that key, it is used for another command"
Else
colShortCuts.Remove 3 ' remove the old 2nd item, which is now the 3rd item
End If
End Sub
' See the pattern above? And for the last combobox...
Private Sub ComboJ_Click()
' Fire is the last item in the collection. We cannot simply change the collection, we must delete it then re-add it
On Error Resume Next
colShortCuts.Add "Fire", CStr(ComboJ.ItemData(ComboJ.ListIndex)), 10
If Err Then
MsgBox "You cannot use that key, it is used for another command"
Else
colShortCuts.Remove 11 ' remove the old 10th item, which is now the 11th item
End IF
End Sub
' ok, now how to use this collection, let's say your Picturebox has keyboard focus during gameplay
Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim sCommand As String
On Error Resume Next
sCommand = colShortCuts.Item(CStr(KeyCode))
If Err Then ' did not select shortcut command, pressed another key for the game
Err.Clear
On Error GoTo 0
' process that other key
Else ' pressed a command
On Error GoTo 0
Select Case sCommand
Case "Jump"
Case "Run"
' etc, etc, etc
Case "Fire"
End Select
End If
End Sub
Nah. Ill stick with my idea.