如何在运行时将两个文本框的值乘以第三个文本框 [英] how to multiply value of two textboxs into the third text box at runtime

查看:72
本文介绍了如何在运行时将两个文本框的值乘以第三个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时将两个文本框的值乘以第三个文本框

推荐答案

将其命名为WinForms:例如,对于十进制值,请使用 Decimal.Parse方法 [^ ].例如:
Taken this is WinForms: For example with decimal values use Decimal.Parse Method[^]. For example:
textBox3.Text = (Decimal.Parse(textBox1.Text) * Decimal.Parse(textBox2.Text)).ToString();


当然,您需要添加检查,使用try catch,还可以使用TryParse等.

加法:
您可以将代码放在您喜欢的任何位置.例如,您可以使用文本框Validating event,KeyUp,KeyDown等可行的方法.


Of course you need to add checks, use try catch, possibly use TryParse instead and so on.

Addition:
You can put the code where anywhere you like. For example you can use the text boxes Validating event, KeyUp, KeyDown etc, whatever is feasible.


要执行此操作而无需回发,您应该使用javascript--
To do this without postback you should use javascript--
function updateValue()
{
  var val1=document.getElementByID("<%=textbox1.ClientID%>").value;
  var val2=document.getElementByID("<%=textbox2.ClientID%>").value;
  document.getElementByID("<%=textbox3.ClientID%>").value=parseFloat(val1)*parseFloat(val2);
}



现在,您可以在需要的任何javascript事件中调用此函数,而我会在blur--
处调用此函数
写在pageload上



now you can call this at any javascript event of your need i am calling this at blur-

write this on pageload

textbox1.Attribute.Add("onblur","updateValue()");
textbox2.Attribute.Add("onblur","updateValue()");




--Pankaj




--Pankaj


要在用户更改其他文本框时更新文本框,只需复制Mika提供的代码并将其粘贴到TextChanged事件处理程序中:

To update the textbox when the user changes the other ones, you just need to copy the code provided by Mika and paste it into a TextChanged event handler:

public YourForm()
{
   InitializeComponent();
   //add the handler for the two input boxes
   textBox1.TextChanged += textbox_TextChanged;
   textBox2.TextChanged += textbox_TextChanged;
}

private void textBox_TextChanged(object sender, EventArgs e)
{
   try
   {
       textBox3.Text = (Decimal.Parse(textBox1.Text) * Decimal.Parse(textBox2.Text)).ToString();
   }
   catch
   {
       //an error occured...
   }
}


这篇关于如何在运行时将两个文本框的值乘以第三个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆