Search code examples
c#winformstablelayoutpanel

retrieve label text from tablelayoutpanel


i have just begun experimenting on using table layout panel to keep things organised. I have added a label control with some text into a cell. how do I get this text back into a string variable at a later time? or even modify the text in the label in the cell?


Solution

  • When you add the control, just store a reference to it in the form.

    Add this to the main form body (not inside a method):

    Label myLabel;
    

    in your code that adds the label:

    myLabel = new Label();
    myLabel.Text = "Some Text";
    // Pseudo-code
    myPanel.Cells.Add(myLabel);
    

    Then later, just access myLabel anywhere in the form code.