0

I have a WinForms program which pulls data from a .csv file and lists it in a table with one entry being listed in a textbox to allow changes. I add the textbox through the following code which works correctly.

 for (int i = 0; i < listLength; i++)
 {


    Label itemCodeData = new Label();
    itemCodeData.Text = itemCode[i];
    dynamicTableLayoutPanel.Controls.Add(itemCodeData, 0, i+1);
    Label itemDescData = new Label();
    itemDescData.Text = itemDescription[i];
    dynamicTableLayoutPanel.Controls.Add(itemDescData, 1, i + 1);
    TextBox currentCountData = new TextBox();
    currentCountData.Text = currentCount[i];
    dynamicTableLayoutPanel.Controls.Add(currentCountData, 2, i + 1);
    Label onOrderData = new Label();
    onOrderData.Text = onOrder[i];
    dynamicTableLayoutPanel.Controls.Add(onOrderData, 3, i + 1);
}

What im trying to do is make it so that when the text in the textbox is changed it will change the background color to green, but if its left empty it will change it to red. Ive tried adding eventhandlers but it wont run as the object isnt created until its loaded so it cant find it.

Is there anyway to change this?

  • _"Ive tried adding eventhandlers but it wont run as the object isnt created until its loaded so it cant find it"_ -- how did you try adding handlers? In the Designer? Then of course you can't. But there's no reason you can't add the handlers programmatically after you create the control. Did you try that? Unfortunately, the code you posted has both too much code and not enough, simultaneously. There's not enough, because it's not _complete_, but there's too much, because sure you can explain your question with just one text box control. See [mcve]. – Peter Duniho Aug 04 '19 at 01:00
  • Based on the code you did post, it seems you are simply trying to add a handler programmatically. See marked duplicates for information on how to do that. – Peter Duniho Aug 04 '19 at 01:06

0 Answers0