且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从下拉列表中减去给文本框的值

更新时间:2023-02-11 20:12:51

首先,将文本框值转换为数字。这很容易,但由于这是一个文本框,你应该这样做:

  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.