Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically update textbox values from combobox changes

Status
Not open for further replies.

smchoudhury

Programmer
Feb 18, 2009
10
GB
Hi Guys,
Can someone direct me on how can i automatically update my value when a user changes the combobox item. At the moment i have a button click when the user changes the combobox item and press the button the values get updated, everytime the combobox item get changed i have to repeat it click the button to update the value. I want something where automitically as soon as the combobox item selected the total value in the text box to change without clicking the button. So if a user changes the item slected from the combobox the values within the text box should changes accoding to combobox changes without a button event handler:

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
 

So what do you get when you multiply

[tt]3.5 * ComboBox2.SelectedItem [/tt] ???

What does your [tt]TextBox1.Text[/tt] show?

Have fun.

---- Andy
 
the combobox items are 1-10, which ever one i selected it does times it by 3.5 but for the answer to appear inside the textbox i have to press the button handler. What i want is to put htis code inside the combobox event handler and everytime a user changes the item (1-10) the calulation should be done atomatically without the need for pressing the button. I need a code that will perform that function....
 

How about:
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
[blue]
        For i As Integer = 1 To 10
            ComboBox1.Items.Add(i)
        Next
[/blue]
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
[blue]
        TextBox1.Text = 3.5 * ComboBox1.SelectedItem
[/blue]
    End Sub

Have fun.

---- Andy
 
I'm using Windows FORM not normal web format vb.net. I do not have Form1_Load instead its the classical
Public Class Form1
End class
I cannot put the code inside there. it throws an error, if i put the code provided inside a button event handler then this code work perfectly but again i dont want a button to control things instead the value should be updated automatically.

Thanks
 
Sorry it works actualyy...tgis is what i did with your code:

Public Class Form1

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

For i As Integer = 1 To 10
ComboBox1.Items.Add(i)
Next

End Sub

and it seem to produce the function i need.

Thanks
 
Using this code you provided with the amendment i made now my form design is not showing up. this is the error i recive:

at Microsoft.VisualStudio.Shell.Design.Serialization.CodeDom.CodeDomEventBindingService.ValidateMethodName(String methodName)
at System.ComponentModel.Design.EventBindingService.EventPropertyDescriptor.SetValue(Object component, Object value)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAttachEventStatement(IDesignerSerializationManager manager, CodeAttachEventStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)


any idea how i could go about fixing this problem.....
 

I don't know what to tell you...

I just started a new VB.NET Project in VS 2008. That gives me one empty Windows Form named Form1. I placed a TextBox1 and a ComboBox1, and then pasted this code:
Code:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 10
            ComboBox1.Items.Add(i)
        Next
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        TextBox1.Text = 3.5 * ComboBox1.SelectedItem
    End Sub
End Class
It works and runs just fine....

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top