How to have a control redraw the Windows form?

ThisHandleNotInUse picture ThisHandleNotInUse · May 28, 2014 · Viewed 16k times · Source

I'm not exactly sure a "redraw" is what I'm looking for... I'm new to designing Windows forms by hand. I've created a class that will use a "TableLayoutPanel" as a passed variable and do its own designing within that table layout panel so the control can be reused and adjust its parameters to fit the data it contains.

I have an event that will redraw the control upon resize of the frame, which works fine. However, when I first .Show() the form, it won't show any of the child controls from the class. If I manually invoke the "resize" method which is called from the Resize event it won't redraw itself either.

All I get is a blank "TableLayoutPanel" until I manually resize the window which invokes the "Resize" event on the parent TableLayoutPanel.

Here's a trunchated version of my class with the methods removed as they're not really relevant:

    public class DataTableFrame : Form
    {

        TableLayoutPanel MyFrame;
        Size ParentSize;
        int Row = 1;
        int Col = 1;
        int LabelWidth = 75;
        int TextWidth = 150;            
        List<DataObject> MyData = new List<DataObject>();


        public class DataObject
        {...
        }

        public DataTableFrame() { }

        public DataTableFrame(TableLayoutPanel Parent)
        {
            MyFrame = Parent;
            MyFrame.AutoScroll = true;
            ParentSize = MyFrame.Size;
            MyFrame.Layout += new LayoutEventHandler(MyFrame_Layout);
        }

        void MyFrame_Layout(object sender, LayoutEventArgs e)...

        public void AddData(string Label, string Data)...

        public void EvaluateRowCol()...

        public void RowsColums(int Rows, int Cols)...

        public void PopulateControls()...

        public void Refresh()
        {
           // What do I put here to force a redraw???
        }

    }

Answer

Jade picture Jade · May 28, 2014

try

this.Invalidate(); //Refreshes or invoke the control to redraw

or

this.Refresh();

Note: Refresh() is already in Form object property you don't have to declare it.