0

When I click a label I want it to draw rectangles (like bars) on specific areas of my form. I created this:

private void paintbars(PaintEventArgs e)
{
    Pen blackPen = new Pen(Color.FromArgb(155,114,97), 1);
    Rectangle rect = new Rectangle(0, 0, 200, 200);
    e.Graphics.DrawRectangle(blackPen, rect);
}

and from the event of the label click I go with paintbars; or paintbars(); or paintbars(e); but it always underlines this specific command (paintbars without the e because it doesn't have the e, and with the e because it says it can't convert from eventargs to painteventargs).

I don't know how to call the method to draw. I also need this rectangles to be erased and redrawed later. How can I do this?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • 1
    What you want to do is (a) handle the `Paint` event, and do your drawing there (using the Graphics object that's part of the paint eventargs), and (b) `Invalidate` the area you want painted, when you want to paint the bars – Flydog57 Oct 18 '20 at 21:02
  • 1
    This seems to be a close copy of your last question about ellipses. There is no other help we can give you. There are so many posts about how to draw in Winforms that you really just need to study all or some of those posts..! – TaW Oct 18 '20 at 22:01
  • Hint: The fist thing you need to learn is that you always need to decide if your graphics shall persist or shall be non-persistent. Look up these concepts! Persistent graphics can only be created froma e.Graphics object directly from a Paint event: Winforms graphics basic rule #1 : Never use `control.CreateGraphics`! Never try to cache a `Graphics` object! Either draw into a `Bitmap bmp` using a `Graphics g = Graphics.FromImage(bmp)` or in the `Paint` event of a control, using the `e.Graphics` parameter.. – TaW Oct 18 '20 at 22:03
  • i suppose there's some sense in all of this, surely...but now i think "ok...i get about half of what i read". what do you mean by persist? it remains on the form? or it will remain as it is? i just need to draw some rectangles and redraw them later, so if i have to delete them and redraw them or just to invalidate them to resize them, it's not a problem. i lack basics just to do it (probably that's main problem) and struggle finding explanations. also i think like "come on, i just need to draw a rectangle, it can't seriously be so complicated to draw a figure and remove/resize it!"...mmm.. – Roberto D V Leonardo Oct 19 '20 at 06:59
  • The only right way is to force the `Paint` event to be raised, so that you have a `PaintEventArgs` given to you from the framework to pass to your method. See duplicate. If you take a moment to think about it, you'll realize that this means that you _must_ maintain a data structure that completely describes what your visual graphics look like _at all times_, so that if and when the `Paint` event is raised, you are able to draw _everything_ that's in the window. Correct sequence is: respond to user input, update visual data structures, call `Invalidate()`, handle `Paint` event by drawing. – Peter Duniho Oct 19 '20 at 08:55

1 Answers1

-1

You cannot force WinForms to pass you args that you want. So if WinForms generated paintbars method with first argument as EventArgs e, let it stay that way.

Now, you need to get instance of Graphics from somewhere and draw rectangle using it. If you want to draw on your form directly, and not on some Control, use this.CreateGraphics()

Here is more answers on how to get instance of Graphics How to manually get instance of Graphics object in WinForms?

Edit: Here is code sample of what I was talking about. I assume that paintbars is label click event.

private void paintbars(EventArgs e)
{
    Pen blackPen = new Pen(Color.FromArgb(155,114,97), 1);
    Rectangle rect = new Rectangle(0, 0, 200, 200);
    using (Graphics g = this.CreateGraphics())
    {
       g.DrawRectangle(blackPen, rect);
    }
}
maxc137
  • 2,291
  • 3
  • 20
  • 32
  • i tried reading but to me it's not clear enough to understand. also, i'd need a clear example, like "let's try, write this, if you want a method, this is how you call it, the method goes like this, these are the parameters for the drawing, and finally this is the result". i understand very well with examples but i really struggle finding explanations and examples of all this. it doesn't make sense to me, it can't be so difficult to do something so simple...i think? – Roberto D V Leonardo Oct 19 '20 at 07:05
  • Added code sample, should be more clear now. Create label click event (I assume that paintbars is label click event, but if it's not - create one) by double-clicking on the label on the form. And put this code inside it. – maxc137 Oct 19 '20 at 08:49
  • No. Any code that calls `CreateGraphics()` is fundamentally broken. No answer posted to a [tag:winforms] question should ever suggest doing so, and _especially_ never to someone who is clearly just starting to learn the API. – Peter Duniho Oct 19 '20 at 08:56
  • @Peter Duniho i already read this elsewhere, so i think you must be experienced and right to say so, so thank you for your feedback! – Roberto D V Leonardo Oct 22 '20 at 08:15