简体   繁体   English

GroupBox中的TextBox事件处理程序

[英]TextBox event handler within GroupBox

I have 12 textboxes and 12 labels inside a GroupBox. 我在GroupBox中有12个文本框和12个标签。

When a price is entered into any of the textboxes, I want the tax to be calculated and then shown in label next to this textbox. 当价格输入到任何文本框中时,我希望计算税额,然后在此文本框旁边的标签中显示。

I have written code to calculate the tax, but it's visible only in the first label labelTax01 . 我已经编写了计算税收的代码,但是它仅在第一个标签labelTax01

My code listing is as follows: 我的代码清单如下:

public void Form1_Load(object sender, EventArgs e)        
{
    foreach (Control ctrl in groupBoxPrice.Controls)              
    {
        if (ctrl is TextBox)
        {
            TextBox price= (TextBox)ctrl;
            price.TextChanged += new EventHandler(groupBoxPrice_TextChanged);
         }
     }
}       

void groupBoxPrice_TextChanged(object sender, EventArgs e)       
{
    double output = 0;
    TextBox price= (TextBox)sender;

    if (!double.TryParse(price.Text, out output))
    {
        MessageBox.Show("Some Error");
        return;
    }
    else
    {
        Tax tax = new Tax(price.Text);              // tax object
        tax.countTax(price.Text);                   // count tax
        labelTax01.Text = (tax.Tax);                // ***help*** ///
    }
}

Name your labels (for example LabelForPrice001, LabelForPrice002, etc...), then insert this name in the Tag property of each price textbox at desing time. 命名标签(例如LabelForPrice001,LabelForPrice002等),然后在设计时将此名称插入每个价格文本框的Tag属性。

At this point finding the textbox means also finding the associated label with a simple search in the Controls collection of the groupBox.... 此时,查找文本框还意味着通过在groupBox的Controls集合中进行简单的搜索来找到关联的标签。

By the way, you will find very useful, to simplify your loops, the extension OfType 顺便说一下,您会发现非常有用的扩展OfType可以简化循环,

public void Form1_Load(object sender, EventArgs e)
{
    foreach (TextBox price in groupBoxPrice.Controls.OfType<TextBox>())              
    {
        price.TextChanged += new EventHandler(groupBoxPrice_TextChanged);
    }
}

void groupBoxPrice_TextChanged(object sender, EventArgs e)       
{
    double output = 0;
    TextBox price= (TextBox)sender;

    if(!double.TryParse(price.Text, out output))
    {
        MessageBox.Show("Some Error");
        return;
    }
    else
    {
        Tax tax = new Tax(price.Text);              
        tax.countTax(price.Text);                   

        // retrieve the name of the associated label...
        string labelName = price.Tag.ToString()

        // Search the Controls collection for a control of type Label
        // whose name matches the Tag property set at design time on
        // each textbox for the price input
        Label l = groupBoxPrice.Controls
                               .OfType<Label>()
                               .FirstOrDefault(x => x.Name == labelName);
        if(l != null)
           l.Text = (tax.Tax);                
    }
}

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

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