tags:

views:

232

answers:

3

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, which sets those properties using 2 textboxes.

I have a button on that dialog box called "save" which upon pressing, updates the properties (rows, columns) in the main class to whatever values are set.

I want the main form to redraw the dynamically drawn buttons, based on the new settings applied (rows and columns). How can I do this?

edit:

Refresh is not working.

Another possibly important note: my dynamic drawing of buttons occurs in the "Form1_Load" method.

A: 

Panel.Refresh()

Will force redrawing of all child controls.

Aequitarum Custos
this is not working...
Sev
A: 

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.

Adam Robinson
When I try that, it gives me an error stating that I have no method called Form1_Load
Sev
I don't understand what you're saying. Don't do your drawing in the `Load` event, do it in the `Paint` event. Again, **do not do any drawing in `Form1_Load`**. This is the source of your problem.
Adam Robinson
If you deleted the Form1_Load method and it's complaining, it's probably because it's referenced in Form1.Designer.cs or something. Delete it from there if necessary.
Andy West
visual studio seems to create too many files and complicates things for a simple task. i added a button, i don't even see the code for the button being shown.
Sev
@Sev: I'm not able to reconcile how the fact that you don't see code means Visual Studio is creating too many files and complicating things...I don't intend to sound condescending, but this is fairly rudimentary stuff. You should spend some time with a good WinForms programming book and learn some of the concepts.
Adam Robinson
well, you do sound condescending. The winforms book i used in the GUI class i took didn't create a designer.cs file, nor a resx file. In any case, I was looking for a quick answer, and I got it, thanks anyways
Sev
@Sev: It's entirely possible that the book you used was geared toward .NET 1.x/VisualStudio 2003. Partial classes (and the advent of the Designer.cs file) were introduced in .NET 2.0/Visual Studio 2005. Again, it wasn't my intention to sound condescending, though you're obviously free to infer whatever meaning you feel you need to. I was trying to convey that the fact that you're performing custom drawing in the form's `Load` event led me to think that you might be missing out on some of the basics. It wasn't intended as a slight on you, just a suggestion that you do further study.
Adam Robinson
+1  A: 

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.

Pedery
I'm using the OnPaint method overriding, and it's causing the buttons to not draw at all now. Also, it's requiring me to have a Form1_Load method, which I don't have anything in now. When I had the Button creation code in the Form1_Load method, it was at least drawing the buttons.
Sev
So where exatcly are you overriding OnPaint? In the form itself? In that case you will draw on the form, but any contained controls will appear on top of what you draw. It sounds to me like you want to make some custom controls. What exactly are you trying to do and would you care to post your code?
Pedery
Thank you the well thought out answer. It didn't help my problem directly, but it did guide me in the right direction.
Sev