Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the below code

C#
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[0].KeyDown +=

new KeyEventHandler(bill_new_Form_KeyPress);

            textbox_item_array[0].DoubleClick +=

new EventHandler(bill_new_Form_DoubleClick);

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

        }



C#
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();
    //textbox_item_array[0].Leave -= dynamic_text_item_code_Leave;
    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]);
        //textbox_item_array[0].Leave -= dynamic_text_item_code_Leave;
        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));
        //MessageBox.Show("Deleted");
        //textbox_item_array[0].Leave -= dynamic_text_item_code_Leave;
        //MessageBox.Show(item_textbox.Count.ToString());
        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
Posted
Updated 19-Nov-17 20:55pm
Comments
ZurdoDev 7-Jun-13 8:55am    
What does the call stack look like? Put a breakpoint and see if the call stack will indicate why it was called?
Member 9646334 7-Jun-13 9:04am    
I put break point but it was not mentioned. Please help how can I do that
Sergey Alexandrovich Kryukov 7-Jun-13 10:15am    
Look the Debug Window "Call Stack".
—SA
Member 9646334 7-Jun-13 9:15am    
I tried to put the break point and it was just the event before it.
Alexander Dymshyts 7-Jun-13 9:18am    
Did you try to put e.Handled = true at the end of your event?

I have found with certain event handlers that they may be called recursively (e.g, an onchange handler that changes the item that called the event will call itself again).

I declare something like:

static bool beenHere = false;

I use it as follows

eventHandler() {

static bool beenHere = false;

 if(beenHere) {
   beenHere = false;
   return;
 }

 beenHere=true;
 /* event handler code */
 /* event handler code */
 /* event handler code */
 /* event handler code */
 return;

} // eventHandler()
 
Share this answer
 
This is not a good solution, but might prevent duplicate same events to be called.
C#
bool leaveEventIsAlreadyCalled = false;

private void SomeMethod()
{
    if (!leaveEventIsAlreadyCalled)
    {
        leaveEventIsAlreadyCalled = true; 
        // do something
    }
    else
    {
        // do nothing
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900