Automotically Update Value

choudhmh

Member
Joined
Jul 18, 2006
Messages
12
Programming Experience
Beginner
Hi Guys,
Can someone direct me on how can i automatically update my value when a user changs the listbox item. At the moment i have a button click when the user changes the list box item and press the button the values get updated, everytime the listbox item get changed i have to repeat it click the button to update the value. I want something where automitically as soon as the listbox item selected the total value in the text box to change without clicking the button:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.SelectedItem = "Seafood" Then
TextBox1.Text = 3.5 * ComboBox2.SelectedItem
End If

End Sub

Thanks,
choudhmh
 
Take a look at handling SelectedIndexChanged.

VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles MyBase.Load

		Dim lbItems() As String = {"One", "Two", "Three", "Four", "Five"}
		Me.ListBox1.Items.AddRange(lbItems)

	End Sub

	Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles ListBox1.SelectedIndexChanged

		Me.TextBox1.Text = Me.ListBox1.SelectedItem

	End Sub
 
Sorry but i'm not developing the application in vb.net web format i'm using windows form format. I have combobox which in web format is equal to list box.
How will i amend the code so this could take place in windows form application usind combobox as selecteditems..
 
choudhmh said:
Hi Guys,
Can someone direct me on how can i automatically update my value when a user changs the listbox item.

choudhmh said:
Sorry but i'm not developing the application in vb.net web format i'm using windows form format. I have combobox which in web format is equal to list box.
How will i amend the code so this could take place in windows form application usind combobox as selecteditems..

Um...a Web App's equivalent of ComboBox would be Drop Down List.

You said ListBox in your description so that's the example code I gave you. To change it for a ComboBox change where it says ListBox1 to whatever you've named your ComboBox.

VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	 Handles MyBase.Load

		Dim lbItems() As String = {"1", "2", "3", "4", "5"}
		Me.ComboBox1.Items.AddRange(lbItems)

	End Sub

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

		Me.TextBox1.Text = (3.5 * CInt(Me.ComboBox1.SelectedItem)).ToString()

	End Sub
 
Under FORM application the form is not private its public:

if i put the first code in the Me is not recognised it becomes a syntax error.
Public Class Form1

Dim lbItems() As String = {"1", "2", "3", "4", "5"}
Me.ComboBox1.Items.AddRange(lbItems)
End Class

i tried executing witihin the combobox:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim lbItems() As String = {"1", "2", "3", "4", "5"}
Me.ComboBox1.Items.AddRange(lbItems)
Me.TextBox1.Text = (3.5 * CInt(Me.ComboBox1.SelectedItem)).ToString()

End Sub

but nothing happens the drop down itms are not added.
 
Under FORM application the form is not private its public:

if i put the first code in the Me is not recognised it becomes a syntax error.

Full code for the example I gave you.

VB.NET:
Public Class Form1

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

		Dim lbItems() As String = {"1", "2", "3", "4", "5"}
		Me.ComboBox1.Items.AddRange(lbItems)

	End Sub

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

		Me.TextBox1.Text = (3.5 * CInt(Me.ComboBox1.SelectedItem)).ToString()

	End Sub

End Class
 
i get this error, where could it come from

Error 1 'Private Sub Form1_Load(sender As Object, e As System.EventArgs)' has multiple definitions with identical signatures.

If i put the whole code inside a button action and run the button the combobox is populated with the strings
 
Back
Top