I have several drop lists on my form, with either numbers or names on them. I want to change it so each item on the drop list has a certain "value" to it. For example, if a was "1" on my first drop list and hello was "23", wen I select "a" and "hello" from the list and I click calculate, it will do the mathematical equation of 1*23. Is there any way?

Recommended Answers

All 11 Replies

Do you mean the listindex property (for a listbox)?

Dim a As Integer
Dim b As Integer

Private Sub Command1_Click()
If List1.ListIndex = -1 Then
MsgBox "nothing selected"
ElseIf List1.ListIndex = 0 Then
a = List1.ListIndex + 1
MsgBox "The value is " & a
a = 0
b = 0
Exit Sub
ElseIf List1.ListIndex = 1 Then
b = List1.ListIndex + 1
MsgBox "The value is " & b
a = 0
b = 0
Exit Sub
End If
End Sub

Private Sub Form_Load()
List1.AddItem "Cat"
List1.AddItem "Dog"
End Sub

Try to use the Listindex of the item

else

try to use ItemData property of the control.

The code is already provided to you.

Maybe try this? Amazing things you can do with the Listbox control Or, check out the zip file. Good luck.

use this simple code.

Private Sub Command1_Click()
MsgBox Combo1.ListIndex + 1 & "*" & Combo2.ListIndex + 1
End Sub

Private Sub Form_Load()
With Combo1
.AddItem "a"
.AddItem "b"
.AddItem "c"
End With
With Combo2
.AddItem "welcome"
.AddItem "to"
.AddItem "vb"
.AddItem "programming"
End With
End Sub

can you further explain how the code works? What does each part mean? That would be really helpful.

Now its really too much. The code is so simple ,very basic level . How do u expect all the spoon feeding after being provided with alll the sample code on a free forum like this.

Thanks Debashis. I appreciate your reply. But I think I can explain this too. Hope you will recognize that.

n louislam 123 here are explanations. go through it......

the code in the form_load event simply adds some elements in the two combo boxes.three elements in combo1 and four elements in combo2.

now the code in command1_click event :-
whenver you select an item from both the combo boxes it will trap the index value for the selected item and add 1 to it,so that it can be looked as real numbers.hope u know that the combo box is a sort of array and the elements stored in an array is identified by its position no.,also known as index or subscript value and this index value always starts from 0.so,when you select the first item in the combo1 the event will trap its index as 0 and add 1 to it.so,it will become 0+1=1.same thing is for combo2 also.after this the code will just concate the two resulted index values and put an asterix between them.

hope the above explanation is enough for you to understand the logic behind this coding.

regards
Shouvik

ok thanks. I get it now. But now I came up with another question. How do i make it subtract and divide the datavalues selected on the droplists, then display them in a label? Is it also possible to make the answers Round up? (not down)

well to perform the math you have to convert the droplist values into numbers.because they(droplist values) always come in string format.use the VAL function to do the conversion like :-

dim a,b as integer
a=val(trim(combo1.text))
b=val(trim(combo2.text))

after this you can perform any mathematical operations like :-

dim subs as integer,divd as double
subs=a-b
divd=round(a/b)

use the ROUND() to round up any values.
then u can display the output like :-

label1.caption="Substraction :" & subs
label2.caption="Division :" & divd

hope it'll help.
regards
Shouvik

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.