繁体   English   中英

c#设置32个文本框值更改时的事件

[英]c# set event when 32 textbox value change

我有这个功能

public void calculateTotalFructiferous() { 
            totalFructiferous.Text = ....;
        }

我有32个文本框 每当32个值中的每个值更改时,我都希望触发该函数。 我在Google上搜索,发现我必须使用事件下downkey and upkey但是我不确定到底是哪一个。 再加上我想是否有一种方法可以在不同于 Windows窗体线程的线程中进行此调用。

对所有TextBox使用TextChanged事件:

    public Form1()
    {
        InitializeComponent();

        textBox1.TextChanged += TextChanged;
        textBox2.TextChanged += TextChanged;
    }


    private void TextChanged(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)sender;
        string text = tb.Text;

        calculateTotalFructiferous(text);
    }

    public void calculateTotalFructiferous(string text) 
    { 
        totalFructiferous.Text = ....;
    }
}

当您进行CPU密集型计算时,可以使用以下方法:

public delegate void CalculateTotalFructiferousDelegate(string text);

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        textBox1.TextChanged += TextChanged;
        textBox2.TextChanged += TextChanged;
    }


    private void TextChanged(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)sender;
        string text = tb.Text;

        //If it is a CPU intensive calculation
        Task.Factory.StartNew(() =>
        {
            //Do sometihing with text
            text = text.ToUpper();

            if (InvokeRequired)
                Invoke(new CalculateTotalFructiferousDelegate(calculateTotalFructiferous), text);
        });
    }

    public void calculateTotalFructiferous(string text)
    {
        totalFructiferous.Text = text;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM