Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 删除时调用两次休假事件_C#_Winforms_Textbox - Fatal编程技术网

C# 删除时调用两次休假事件

C# 删除时调用两次休假事件,c#,winforms,textbox,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

我有下面的代码

    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++;
    }
现在,这里是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]);
            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--;
        }            
    }

那么,为什么它在到达remove后执行呢?你确定在代码的另一部分中没有两次使用这一行吗

textbox\u item\u数组[0]。Leave+=新事件处理程序(动态\u text\u item\u code\u Leave)

如果没有,我想您将
dynamic\u text\u item\u code\u Leave
钩住另一个控件

编辑

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

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

您是否在dynamic_text_item_code_Leave中设置了断点以检查调用了多少次以及发送者是谁?亲爱的,是的,我设置了断点,发现调用了两次。我认为发送者是同一个文本框,因为它是唯一一个可以发送事件的文本框。你能告诉我,如果我设置了断点,如何检查寄件人吗。Thanks@user2103335你能告诉我们如何重现你的问题吗?因为在我们面对任何问题之前都很难解决。亲爱的兄弟,如果你只是复制粘贴代码并尝试运行,你就可以重现问题it@user2103335您可以点击动态文本项目代码离开中的断点,并用鼠标光标检查文本框属性。或者直接进行显式转换(文本框)并在控制台/消息框中显示,例如文本