• Check out world news on HeadLinesList.com
  • Forum registration is now open for public.
Hello There, Guest! Login


Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a value to Visual Basic Listbox Item?
#
In a VB form I have two list boxes, one which is populated from a text file. These are the different items in the text file list box:

Small
Medium
Large

I want to attach a value to each of these items. For example, if I was to select Small from the first list box and then another item from the other list box the program would know Small is 1000 and therefore it would add the first list box item (Small) to the second one.

Basically, what I need is a way to add a value to each of these items. When Small is clicked the program needs to know that Small is equal to 1000. Then when I enter in the code to add two selected list box items it will add 1000 (if small is selected) and whatever the other value is.
Is there any way of doing this with just one list box where you select small, medium or large and it automatically knows the values? I don't want to have two list boxes which the user has to select.

Ads

#
The following code will assign any value shown in list box 2 to any size shown in list box 1.

I have entered the 3 sizes in list box 1 and 3 values in list box 2. However, you can enter as many values to list box 2 as you desire, without changing any code shown below.

When you assign values to the 3 sizes (by first clicking on a size in list box 1 then clicking on a value in list box 2), you create an interger variable and variable value, which can be used in your code to add the values of several of the variable values, either with a button or in the code you write to do this.

You will be guided by the text in the label on the form as you set values for the 3 sizes.

You have not given enough information for any additional code to be developed by me.

There is a button, a label and 2 list boxes on the form. The button is for enabling you to set the values of the 3 sizes.


Public Class Form1
Public smallValue, mediumValue, largeValue As Integer
Public Flag1, Flag2, Flag3, Flag4 As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ListBox1.Enabled = True
ListBox2.Enabled = True
Flag1 = 0
Flag2 = 0
Flag3 = 0
Flag4 = 0
smallValue = 0
mediumValue = 0
largeValue = 0
Label1.Text = "You can now assign new values to the sizes in list box 1"
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex = 0 And Flag1 = 0 Then
Label1.Text = "Assign a value to this size by clicking the value in list box 2"
ElseIf ListBox1.SelectedIndex = 1 And Flag2 = 0 Then
Label1.Text = "Assign a value to this size by clicking the value in list box 2"
ElseIf ListBox1.SelectedIndex = 2 And Flag3 = 0 Then
Label1.Text = "Assign a value to this size by clicking the value in list box 2"

End If

End Sub

Private Sub ListBox2_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
If ListBox1.SelectedIndex = 0 And Flag1 = 0 Then
smallValue = Val(ListBox2.SelectedItem)
Label1.Text = "You have assigned a value of " & Val(ListBox2.SelectedItem) & " to the Small size"
Flag1 = 1
ElseIf ListBox1.SelectedIndex = 1 And Flag2 = 0 Then
mediumValue = Val(ListBox2.SelectedItem)
Label1.Text = "You have assigned a value of " & Val(ListBox2.SelectedItem) & " to the Medium size"
Flag2 = 1
ElseIf ListBox1.SelectedIndex = 2 And Flag3 = 0 Then
largeValue = Val(ListBox2.SelectedItem)
Label1.Text = "You have assigned a value of " & Val(ListBox2.SelectedItem) & " to the Large size"
Flag3 = 1
End If
ListBox1.SelectedIndex = -1
ListBox2.SelectedIndex = -1
If Flag1 = 1 And Flag2 = 1 And Flag3 = 1 Then
Flag4 = 1
Label1.Text = "All sizes have been assigned a value."
ListBox1.Enabled = False
ListBox2.Enabled = False
End If
End Sub

End Class

Edit
The interger variable and value for the 3 sizes are smallValue, mediumValue and largeValue. These can be used in the addition code for these values.


EDIT
In response to your latest question. I gave you 2 methods to use just one list box, in your same question 6 days ago.

EDIT 3
You can also simply assign values to those 3 sizes when you click them in the list box.


TexMav

Ads

  


Forum Jump:


1 Guest(s)