0

I have the below code

    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++;
    }

Now, here is the leave event handler:

    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--;
        }
    }

Now, the problem is like this: If the user leave the textbox blank, the row will be deleted. The strange problem is: If press tab, it will execute the leave event handler twice. It means it will try to delete the same textbox twice and this will create problem. Can any one help me how to avoid this double calling?

Thanks

I want to add more: here is exactly what is happening:

Now, I will give exactly how it is executed:

    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]);

After that, it calls the same function

    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--;
        }            
    }

Then it continues here

            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--;
        }            
    }

So, why it executes once it get to the remove

Servy
  • 202,030
  • 26
  • 332
  • 449
user2103335
  • 63
  • 4
  • 13
  • Did you put a breakpoint inside dynamic_text_item_code_Leave to check how many time it was called and who was the sender? – Serge Jun 07 '13 at 11:57
  • Dear, Yes I put that and I found that it was called twice. I think that the sender is the same textbox since it is the only one how can send the event. Can you tell me how can I check the sender if I put the break point. Thanks – user2103335 Jun 07 '13 at 11:59
  • @user2103335 can you tell us how to reproduce your problem? because it is hard to solve any problem until we get face it. – Never Quit Jun 07 '13 at 12:29
  • Dear bro, You can reproduce the problem if you just copy and paste the code and try to run it – user2103335 Jun 07 '13 at 13:03
  • @user2103335 You can hit breakpoint in dynamic_text_item_code_Leave and with mouse cursor can check textbox properties. Or just make explicit casting (TextBox) and show in console/messageBox e.g Text – Saint Jun 07 '13 at 15:10

1 Answers1

0

Are you sure, you don't use this line twice in another part of code?

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

If not I suppose you hooks dynamic_text_item_code_Leave to one another control.

EDIT

You can also to protect by adding this code:

if (textbox_item_array[0].Leave != null)
    textbox_item_array[0].Leave += new EventHandler(dynamic_text_item_code_Leave);
Saint
  • 5,397
  • 22
  • 63
  • 107