Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Iam using Visual studio 2012,

i would like to change text box values automatically based on fixed combo box values,

meaning i have combobox with 4 fixed values & i want to change another text box values based on these changeable values.

so anyone know simple method to apply that will be great

thanks..,

[Edit - OP code from comment]
the previous solution didn't work, i need to make condition like:

VB
If fy.Text = 2400 Then
    µmax.Text = 8.56 / 100000 * fcu.Text

ElseIf fy.Text = 2800 Then
    µmax.Text = 7 / 100000 * fcu.Text

ElseIf fy.Text = 3600 Then
    µmax.Text = 5 / 100000 * fcu.Text

ElseIf fy.Text = 4000 Then
    µmax.Text = 4.31 / 100000 * fcu.Text

End If


(fy is the combobox name) ( µmax is the textbox name)

' i tried this also, but it didn't work

VB
Private Sub fy_SelectedIndexChanged(sender As Object, e As EventArgs) Handles fy.SelectedIndexChanged


        If fy.SelectedIndex = 2400 Then
            µmax.Text = 8.56 / 100000 * fcu.Text

        
        ElseIf fy.SelectedIndex = 3600 Then
            µmax.Text = 7 / 100000 * fcu.Text

        End If

    End Sub
Posted
Updated 1-Jan-14 11:43am
v5
Comments
ZurdoDev 1-Jan-14 16:02pm    
The simple way is to do text1.Text = "whatever you want."

Where are you stuck?
amadofirst 1-Jan-14 16:06pm    
i want to change the text box value automatically based on the changeable combo box value,
meaning,
if i chose (x1) value from combo box i need the text box to automatically change to (y1) &
if i chose (x2) value from combo box i need the text box to automatically change to (y2), etc..

all of this automatically without pushing any buttons
CHill60 1-Jan-14 16:15pm    
I think you mean that you need to put some code behind the _SelectedIndexChanged event on the comboBox. How you look up y1 from x1 is up to you but you may find the SelectedIndex property of the ComboBox useful - remember it will start at 0 not 1
Sergey Alexandrovich Kryukov 1-Jan-14 16:24pm    
What have you tried so far?
—SA
amadofirst 1-Jan-14 16:27pm    
the previous solution didn't work, i need to make condition like:

If fy.Text = 2400 Then
µmax.Text = 8.56 / 100000 * fcu.Text

ElseIf fy.Text = 2800 Then
µmax.Text = 7 / 100000 * fcu.Text

ElseIf fy.Text = 3600 Then
µmax.Text = 5 / 100000 * fcu.Text

ElseIf fy.Text = 4000 Then
µmax.Text = 4.31 / 100000 * fcu.Text

End If

(fy is the combobox name) ( µmax is the textbox name)

' i tried this also, but it didn't work

Private Sub fy_SelectedIndexChanged(sender As Object, e As EventArgs) Handles fy.SelectedIndexChanged


If fy.SelectedIndex = 2400 Then
µmax.Text = 8.56 / 100000 * fcu.Text


ElseIf fy.SelectedIndex = 3600 Then
µmax.Text = 7 / 100000 * fcu.Text



End If

End Sub

1 solution

You have several issues with your attempts. Most importantly you are trying to mix numeric data and string data. Sometimes you will get an implicit conversion done "for you" but you should never rely on this.

In your second attempt you are confusing SelectedIndex (the rank of the selected item within the list) and SelectedItem (the actual item selected). - I somehow doubt that you have 3600 items in the comboBox fy.

Instead of having multiple if statements consider using some form of lookup (have a look through sergey's solutions[^] as I know he's written several good solutions on this subject)

I've tested this code and it works
VB
Dim factors As Double() = {8.56, 7, 5, 4.31} 'etc
Const multiplier As Double = 10000.0

Dim fcuAsDouble As Double
If Not Double.TryParse(fcu.Text, fcuAsDouble) Then
    'raise an error not a valid number
End If

Dim result As Double = (factors(fy.SelectedIndex) / multiplier) * fcuAsDouble

umax.Text = result.ToString()

factors is my lookup for your values that you are using in your multiple if statements. I've used an array for simplicity but there are other (and better) options.

I've also defined multiplier as a constant because I don't like "magic numbers"

Next I look to the fcu textbox and convert it's text into a double so I can use it in the calculation. Note the use of Double.TryParse() - it will convert text into a Double value if in the right format otherwise it will return False. Double.Parse will error if fcu contained (for example) "three"

In the next line I use fy.SelectedIndex to look up the appropriate factor from my array, and work out the result.

Finally when I assign the result into the umax text box I convert it to text using ToString()

All of the above I put into the fy_SelectedIndexChanged event as per my earlier comments
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jan-14 21:02pm    
Good points, a 5.
—SA
CHill60 2-Jan-14 9:02am    
Thank you ... a virtual 5 back for including a link to your solutions - I knew I should have bookmarked the one's I remember but can't find when I want to refer to them like this :-)
amadofirst 2-Jan-14 5:55am    
thanks alot for your explanation & your solution it's working.
CHill60 2-Jan-14 9:01am    
Good - I'm pleased. I hope the explanation made sense

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900