如果删除,则会调用两次事件

时间:2013-06-07 11:50:56

标签: c# winforms textbox

我有以下代码

    public void panel_item_collections_Click(object sender, EventArgs e)
    {
        TextBox[] textbox_item_array = new TextBox[5];
        item_textbox.Add(textbox_item_array);
        textbox_item_array[0] = new TextBox();
        textbox_item_array[0].Width = label_item_code.Width;
        textbox_item_array[0].Height = 26;
        textbox_item_array[0].Font = print_font_default;
        textbox_item_array[0].Location = new Point(label_item_code.Location.X, 45 + (20 * row_count));
        textbox_item_array[0].Name = string.Concat("item_code", row_count.ToString());
        panel_item_collections.Controls.Add(textbox_item_array[0]);
        textbox_item_array[0].Leave += new EventHandler(dynamic_text_item_code_Leave);
        textbox_item_array[1] = new TextBox();
        textbox_item_array[1].Width = label_item_descrition.Width;
        textbox_item_array[1].Font = textbox_item_array[0].Font;
        textbox_item_array[1].Location = new Point(label_item_descrition.Location.X, textbox_item_array[0].Location.Y);
        textbox_item_array[1].Name = string.Concat("item_description", row_count.ToString());
        panel_item_collections.Controls.Add(textbox_item_array[1]);
        textbox_item_array[2] = new TextBox();
        textbox_item_array[2].Width = label_item_price.Width;
        textbox_item_array[2].Font = textbox_item_array[0].Font;
        textbox_item_array[2].Location = new Point(label_item_price.Location.X, textbox_item_array[0].Location.Y);
        textbox_item_array[2].Name = string.Concat("item_price", row_count.ToString());
        panel_item_collections.Controls.Add(textbox_item_array[2]);
        textbox_item_array[3] = new TextBox();
        textbox_item_array[3].Width = label_item_quantity.Width;
        textbox_item_array[3].Font = textbox_item_array[0].Font;
        textbox_item_array[3].Location = new Point(label_item_quantity.Location.X, textbox_item_array[0].Location.Y);
        textbox_item_array[3].Name = string.Concat("item_quantity", row_count.ToString());
        panel_item_collections.Controls.Add(textbox_item_array[3]);
        textbox_item_array[4] = new TextBox();
        textbox_item_array[4].Width = label_item_total.Width;
        textbox_item_array[4].Font = textbox_item_array[0].Font;
        textbox_item_array[4].Location = new Point(label_item_total.Location.X, textbox_item_array[0].Location.Y);
        textbox_item_array[4].Name = string.Concat("item_total", row_count.ToString());
        panel_item_collections.Controls.Add(textbox_item_array[4]);
        row_count++;
    }

现在,这是离开事件处理程序:

    void dynamic_text_item_code_Leave(object sender, EventArgs e)
    {
        //MessageBox.Show(((Control)sender).Name.Substring(((Control)sender).Name.Length - 1, 1));
        int i;
        string name_textbox = ((Control)sender).Name;
        i = System.Convert.ToInt32(name_textbox.Substring(name_textbox.Length - 1, 1));
        //MessageBox.Show(i.ToString());
        //i--;
        TextBox[] textbox_item_array = new TextBox[5];
        textbox_item_array = (TextBox[])(item_textbox[i]);
        double item_total;
        Item item = new Item();
        if (long.TryParse(textbox_item_array[0].Text, out item.item_code) == true)
        {
            if (item.get_item() == 0)
            {
                textbox_item_array[1].Text = item.item_details;
                textbox_item_array[2].Text = item.sell_price.ToString();
                textbox_item_array[3].Text = "1";
                item_total = System.Convert.ToInt32(textbox_item_array[3].Text) * item.sell_price;
                textbox_item_array[4].Text = item_total.ToString();
            }
        }
        else
        {
            //TextBox[] textbox_item_array = new TextBox[5];
            textbox_item_array = (TextBox[])(item_textbox[item_textbox.Count - 1]);
            panel_item_collections.Controls.Remove(textbox_item_array[0]);
            panel_item_collections.Controls.Remove(textbox_item_array[1]);
            panel_item_collections.Controls.Remove(textbox_item_array[2]);
            panel_item_collections.Controls.Remove(textbox_item_array[3]);
            panel_item_collections.Controls.Remove(textbox_item_array[4]);
            item_textbox.RemoveAt((item_textbox.Count - 1));
            row_count--;
        }
    }

现在,问题是这样的:  如果用户将文本框留空,则该行将被删除。奇怪的问题是:  如果按Tab键,它将执行两次Leave事件处理程序。这意味着它将尝试两次删除相同的文本框,这将产生问题。谁能帮助我如何避免这种双重呼唤?

谢谢

我想补充更多:这正是发生的事情:

现在,我将准确地说明它是如何执行的:

    void dynamic_text_item_code_Leave(object sender, EventArgs e)
    {
        //MessageBox.Show(((Control)sender).Name.Substring(((Control)sender).Name.Length - 1, 1));
        int i;
        string name_textbox = ((Control)sender).Name;
        i = System.Convert.ToInt32(name_textbox.Substring(name_textbox.Length - 1, 1));
        //MessageBox.Show(i.ToString());
        //i--;
        TextBox[] textbox_item_array = new TextBox[5];
        textbox_item_array = (TextBox[])(item_textbox[i]);
        double item_total;
        Item item = new Item();
        if (long.TryParse(textbox_item_array[0].Text, out item.item_code) == true)
        {
            if (item.get_item() == 0)
            {
                textbox_item_array[1].Text = item.item_details;
                textbox_item_array[2].Text = item.sell_price.ToString();
                textbox_item_array[3].Text = "1";
                item_total = System.Convert.ToInt32(textbox_item_array[3].Text) * item.sell_price;
                textbox_item_array[4].Text = item_total.ToString();
            }
        }
        else
        {
            //TextBox[] textbox_item_array = new TextBox[5];
            textbox_item_array = (TextBox[])(item_textbox[item_textbox.Count - 1]);
            panel_item_collections.Controls.Remove(textbox_item_array[0]);

之后,它调用相同的函数

    void dynamic_text_item_code_Leave(object sender, EventArgs e)
    {
        //MessageBox.Show(((Control)sender).Name.Substring(((Control)sender).Name.Length - 1, 1));
        int i;
        string name_textbox = ((Control)sender).Name;
        i = System.Convert.ToInt32(name_textbox.Substring(name_textbox.Length - 1, 1));
        //MessageBox.Show(i.ToString());
        //i--;
        TextBox[] textbox_item_array = new TextBox[5];
        textbox_item_array = (TextBox[])(item_textbox[i]);
        double item_total;
        Item item = new Item();
        if (long.TryParse(textbox_item_array[0].Text, out item.item_code) == true)
        {
            if (item.get_item() == 0)
            {
                textbox_item_array[1].Text = item.item_details;
                textbox_item_array[2].Text = item.sell_price.ToString();
                textbox_item_array[3].Text = "1";
                item_total = System.Convert.ToInt32(textbox_item_array[3].Text) * item.sell_price;
                textbox_item_array[4].Text = item_total.ToString();
            }
        }
        else
        {
            //TextBox[] textbox_item_array = new TextBox[5];
            textbox_item_array = (TextBox[])(item_textbox[item_textbox.Count - 1]);
            panel_item_collections.Controls.Remove(textbox_item_array[0]);
            panel_item_collections.Controls.Remove(textbox_item_array[1]);
            panel_item_collections.Controls.Remove(textbox_item_array[2]);
            panel_item_collections.Controls.Remove(textbox_item_array[3]);
            panel_item_collections.Controls.Remove(textbox_item_array[4]);
            item_textbox.RemoveAt((item_textbox.Count - 1));
            row_count--;
        }            
    }

然后它继续在这里

            panel_item_collections.Controls.Remove(textbox_item_array[1]);
            panel_item_collections.Controls.Remove(textbox_item_array[2]);
            panel_item_collections.Controls.Remove(textbox_item_array[3]);
            panel_item_collections.Controls.Remove(textbox_item_array[4]);
            item_textbox.RemoveAt((item_textbox.Count - 1));
            row_count--;
        }            
    }

那么,为什么它一旦进入删除

就会执行

1 个答案:

答案 0 :(得分:0)

您确定,您不会在另一部分代码中使用此行两次吗?

textbox_item_array[0].Leave += new EventHandler(dynamic_text_item_code_Leave);

如果不是,我想你将dynamic_text_item_code_Leave挂钩到另一个控件。

修改

您还可以通过添加以下代码来保护:

if (textbox_item_array[0].Leave != null)
    textbox_item_array[0].Leave += new EventHandler(dynamic_text_item_code_Leave);