How to have a control redraw the Windows form?
Asked Answered
E

1

7

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

    }
Elohist answered 28/5, 2014 at 11:43 Comment(1)
see my updated anser belowMalayopolynesian
M
15

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.

Malayopolynesian answered 28/5, 2014 at 11:53 Comment(6)
I tried this - both invalidate() and refresh() separately and together. I even tried invoking it directly on the "Form" class itself (the parent of the TableLayout that is passed to this class object). It didn't work and I'm not sure why. That was the answer I came across when I was looking online. The redefinition of "refresh" is simply so I don't have to access the table control via DataTableFrame.MyFrame.Refresh(). Since I'm routing control of the "TableLayout" through this class object - I was just giving it its own "refresh" definition.Elohist
Like I said, the problem is that none of the child controls that this class object adds to the passed "TableLayoutPanel" will show up until I do something that invokes the .Layout event. Is there a way to programmatically invoke this event after data is added to the table? It works exactly as intended when after a "Layout" event is called.Elohist
You've got it backwards: ParentSize = MyFrame.Size doesn't resize the frame. It just stores the current size in a variable so the assigned Layout event method can check whether or not the "Layout" event actually has resized the frame. Otherwise it won't reprocess the child components into columns fitting the width of the frame. It's there to avoid horizontal scrollbars.Elohist
what if you use the Resize Event or SizeChanged event of the tableLayoutPanelMalayopolynesian
Re: Jade I suppose I could do that. I was going to use the resize event rather than the layout event but the .NET documentation advised against using that and recommended using "Layout" instead (msdn.microsoft.com/en-us/library/…). The method checks for a resize. The problem is still just that when the form is loaded and data is added, it doesn't show up until a "Layout" event is called on the form. I'm brand new to hand coding form design so I don't know what events are raised precisely when so I chose a broader scope.Elohist
Oh, I feel stupid - it was my fault. I had put the evaluation logic for the child controls within the "check if object has been resized" event. So it wasn't actually creating any child controls until the window had been resized. I'm sorry to stack overflow for wasting your time. I'll try to be more awake when I ask questions while writing code in the future.Elohist

© 2022 - 2024 — McMap. All rights reserved.