Hi There,
I would like to draw a line within a panel on my form. Using the OnPaint event, I have been able to draw on the form object itself.
Is it possible (syntatically) to direct the system to draw inside a specific control from the form's OnPaint event, or do I have to create another OnPaint event for that particular control.
Thanks, Karl

How to direct Drawing to a contained object (panel)?
zaph
Thanks for your reply. I'm new to C# and the problem I have is that the panel was created through the Designer, and so I don't know how/where to plant the code that you're suggesting, since I don't want to change the generated code.
When I try this code standing alone in Form1.cs, I get the message:
"Invalid token '+=' in class, struct, or interface member declaration".
Duane Douglas
panel.Paint += new PaintEventHandler(myPanelPaint);
void myPanelPaint(object sender, PaintEventArgs e)
{
}
The code within myPanelPaint() will get run when that panel has been invalidated.
OR, you could roll your own custom panel
class MyCustomPanel : System.Windows.Forms.Panel
{
...
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(); // Up to you to call this or not.
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//base.OnPaintBackground(); // same decision as above.
}
...
}
ivanL
I'll present 2 ways to do it, code-wise, and Designer-wise.
The code-wise way to do it:
-Open your main form's source file, as if you want to edit code, in Visual Studio. I do this by right-clicking the filename in the list in the Solution Explorer, then choosing "View Code"
-Look for the one line of "InitializeComponent()". It's placed in the constructor of your main form's class. Right click this function and choose "Go To Definition". A new source file view should open up, with the function body scrolled into view.
-Look for your panel's comments, it should look something like
//
// panel1
//
If you renamed your panel in the Designer, that name will be reflected here. Anyway, this is where you could add that line I originally posted:
this.panel1.Paint += new PaintEventHandler(myPanelPaint);
-You need to provide a function body for myPanelPaint. This is hand entered by you in the first .cs file. That is, your main form's source code file. Just put it right under the constructor body:
private void myPanelPaint(object sender, PaintEventArgs e)
{
// drarw your red line here, or whatever it was
}
That should be it for the code-wise method.
The Designer-wise way to do it:
-Get to the Properties page of the panel. If the Properties tab isn't visible, then you should be able to get to it by going to the main menu, View, Properties Window. If you can't get to the Designer, left double-click on the main form's filename in the Solution Explorer. Single click your panel, to "highlight" it.
-Click the lightning bolt icon at the top of the window to bring up Events.
-Find the Paint entry.
-Double click in the empty entry next to Paint. This should do everything the code-wise method does, in the blink of an eye.
You should now have the source file up with the cursor at the position for you to start giving draw commands.
thmccarthy
I suspect I'll be spending some time clicking the Lightning Bolt icon from now on.
Take Care,
Karl