C#如何自动将数据填充到第3个文本框 [英] C# how to auto populate data to a 3rd textbox

查看:116
本文介绍了C#如何自动将数据填充到第3个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过网但却无法找到一个简单问题的答案。我有3个文本框

数学TB1 12 * TB2 12 = TB3(144)。当我在文本1和2中键入数字时,我希望它在没有按钮的textbox3中显示。



任何链接都会有用

< br $>
谢谢



我的尝试:



  int  res =  0 ; 
尝试
{
res = Convert.ToInt32(tbLength.Text)* Convert.ToInt32(tbWidth.Text);
textBox2.Text = res.ToString();
}
catch (例外){}

解决方案

只需处理文本更改事件。在里面做你的计算并在另一个文本框中显示。


正如其他人已经说过的那样,你可以使用文本框的TextChanged事件。

这是一个(WinForms) )如何做到这一点的例子:

  public  Form1()
{
InitializeComponent();

tbWidth.TextChanged + = tbWidthLength_TextChanged;
tbLength.TextChanged + = tbWidthLength_TextChanged;
}

private void tbWidthLength_TextChanged( object sender,EventArgs e)
{
int width,length;

int .TryParse(tbWidth.Text, out width);
int .TryParse(tbLength.Text, out length);

textBox2.Text =(width * length).ToString();
}

Peter


尝试

{

十进制长度= = Convert.ToDecimal( tbLength.Text);

十进制宽度= = Convert.ToDecimal(tbWidth.Text);



十进制res =长度*宽度;

textBox2.Text = res.ToString();

}

catch(例外)

{}

I have look over the net but cant find an answer to a simple question. I have 3 textboxs
for math TB1 12 * TB2 12 = TB3 (144). when I type the numbers into text 1 and 2 I want it to show in textbox3 without Buttons.

any links would be helpful

Thanks

What I have tried:

int res = 0;
           try
           {
               res = Convert.ToInt32(tbLength.Text) * Convert.ToInt32(tbWidth.Text);
               textBox2.Text = res.ToString();
           }
           catch (Exception ) { }

解决方案

Just handle the text change event. Inside that do your calculations and show in another textbox.


As the others already stated, you can use the TextChanged event of the textboxes.
Here's an (WinForms) example of how to do that:

public Form1()
{
    InitializeComponent();

    tbWidth.TextChanged += tbWidthLength_TextChanged;
    tbLength.TextChanged += tbWidthLength_TextChanged;
}

private void tbWidthLength_TextChanged(object sender, EventArgs e)
{
    int width, length;

    int.TryParse(tbWidth.Text, out width);
    int.TryParse(tbLength.Text, out length);

    textBox2.Text = (width * length).ToString();
}

Peter


try
{
decimal Length= = Convert.ToDecimal(tbLength.Text);
decimal Width= = Convert.ToDecimal(tbWidth.Text);

decimal res = Length * Width;
textBox2.Text = res.ToString();
}
catch (Exception )
{ }


这篇关于C#如何自动将数据填充到第3个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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