C# Reload/Redraw form upon settings update in dialog box

前端 未结 3 1335
北恋
北恋 2021-01-22 08:00

I have a C# Win Forms application where I dynamically draw buttons in a panel based on 2 properties in the class. Rows and Columns.

I also have a dialog box that opens,

相关标签:
3条回答
  • 2021-01-22 08:18

    You should not be doing any drawing outside of the Paint event of the form. This is why the form is not redrawing correctly. Move your custom drawing there and the redrawing should behave normally.

    0 讨论(0)
  • 2021-01-22 08:22

    Panel.Refresh()

    Will force redrawing of all child controls.

    0 讨论(0)
  • 2021-01-22 08:32

    You have basically three ways to force the control to redraw itself, Refresh(), Update() and Invalidate(). As Adam Robinson points out, the easiest way to enable custom painting is to override the Paint event. Put all painting logic here. Use the Graphics object provided by the PaintEventArgs parameter.

    So what's the difference between the above calls?

    Invalidate marks the control (region, or rect) as in need of repainting, but doesn't immediately repaint (the repaint is triggered when everything else has been taken care of and the app becomes idle).

    Update causes the control to immediately repaint if any portions have been invalidated.

    Refresh causes the control to invalidate, and then update (i.e. immediately repaint itself).

    I'd say it's a good habit to use Invalidate() unless you have specific needs to cater for. In most cases it will make your program more efficient. If you do this, you won't even need to have paint logic in your load event. Quite possibly this is being overwritten and invalidated before you even get your form visible, depending on what else you do in the Load event.

    0 讨论(0)
提交回复
热议问题