Visual Studio 2012 - 自动更改文本框 [英] Visual studio 2012 - change text box automatically
问题描述
我使用Visual Studio 2012,
i想根据固定的组合框值自动更改文本框值,
意味着我有4个固定值的组合框和&我想根据这些可变值更改另一个文本框值。
所以任何人都知道应用的简单方法会很棒
谢谢..,
以前的解决方案不起作用,我需要做出如下条件:
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..,
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是组合框名称)(μmax是文本框名称)
'我也试过这个,但是没用了
(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
推荐答案
您的尝试有几个问题。最重要的是,您正在尝试混合数字数据和字符串数据。有时你会得到一个为你的隐式转换,但你永远不要依赖它。
在你的第二次尝试中你会感到困惑SelectedIndex
(列表中所选项目的排名)和SelectedItem
(选择的实际项目)。 - 我怀疑你在comboBox中有3600个项目。
而不是多个如果
语句考虑使用某种形式的查找(浏览 sergey的解决方案 [ ^ ]我知道他已经写了几个很好的解决方案主题)
我已经测试了这段代码并且它可以工作
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 confusingSelectedIndex
(the rank of the selected item within the list) andSelectedItem
(the actual item selected). - I somehow doubt that you have 3600 items in the comboBox fy.
Instead of having multipleif
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
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()
因素
是我对y的查找我们在多个if语句中使用的值。我使用了一个简单的数组,但还有其他(和更好的)选项。
我还定义了乘数
作为常数,因为我不喜欢魔术数字
接下来我查看 fcu
文本框并将其文本转换为双,以便我可以在计算中使用它。注意使用 Double.TryParse()
- 如果格式正确,它会将文本转换为Double值,否则返回False。如果包含fcu,Double.Parse会出错(例如)三
在下一行中我使用 fy.Selected 索引
从我的数组中查找相应的因子,并计算结果。
最后当我将结果分配到umax文本框我使用将其转换为文本ToString()
以上所有我放入根据我之前的评论 fy_SelectedIndexChanged
事件
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
这篇关于Visual Studio 2012 - 自动更改文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!