如何从下拉列表中减去给文本框的值 [英] how to subtract a value given to a textbox from a dropdownlist
本文介绍了如何从下拉列表中减去给文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
首先,将文本框值转换为数字。这很容易,但由于这是一个文本框,你应该这样做:
int myValue;
if (!int.TryParse(myTextBox.Text, out myValue))
{
// 告诉用户他没有输入数字!
...
return ;
}
// 一切正常 - 继续......
然后,为您的下拉列表执行相同的操作:因为您控制了值,所以更容易:
int myDDLValue = int .Parse(myDropDownList.Text);
然后就这样做吧!
myOutputTextBox.Text =(myDDLValue - myValue).ToString();
这是在开始编写基于表单的应用程序之前应该真正理解的基本编程。请参阅 http://www.codeproject.com/Messages/4799340/Re-Csharp.aspx [ ^ ]虽然不准确,但原则是一样的。
for example i have a dropdownlist having values 1,2,3 and a textbox having value 10,how do i subtract these values ie if choosen 1 then output should be 9.output could be shown in another textbox
解决方案
First, convert the textbox value to a number. That's easy, but since this is a textbox, you shoudl do it like this:
int myValue; if (!int.TryParse(myTextBox.Text, out myValue)) { // Tell the user he didn't enter a number! ... return; } // All ok - move on...
Then, do the same for your drop down list: that's even easier because you control the values:
int myDDLValue = int.Parse(myDropDownList.Text);
Then just do it!
myOutputTextBox.Text = (myDDLValue - myValue).ToString();
This is basic programming that you should really understand before you start writing form based applications. See http://www.codeproject.com/Messages/4799340/Re-Csharp.aspx[^], while not exact, the principle is the same.
这篇关于如何从下拉列表中减去给文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文