2

How do I draw a circle around a label?

Right now I've tried this:

public void drawUseCase(int width, int height, UseCase useCase)
{
    Label lbUseCase = new Label();
    Graphics g = lbUseCase.CreateGraphics();
    Pen p = new Pen(Color.Black, 1);
    g.DrawEllipse(p, width, height, 200, 200);
    lbUseCase.Location = new System.Drawing.Point(width, height);
    lbUseCase.Text = useCase.name;
    mainPanel.Controls.Add(lbUseCase);
}

But that's not working. Any ideas?

It's in winforms. With 'it's not working' I mean that only the label shows up but no circle or what so ever.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
Jamie
  • 10,302
  • 32
  • 103
  • 186
  • 2
    This is Windows Phone 7? 8, 10? ASP.NET, MVC? Forms? – Caspar Kleijne Feb 16 '17 at 13:01
  • 1
    "But that's not working " what's not working? How it's not working? What unexpected thing your are seeing? Error? Crash ? – Chetan Feb 16 '17 at 13:02
  • 1
    @CasparKleijne Yes. That defienitevly is MVC... This code indicates clearly that it's WinForms... – mrogal.ski Feb 16 '17 at 13:03
  • 1
    We all know Caspar is just fishing to show OP that he misses critical information to diagnose the issue. Asking what framework they are using is just to show how much is missing. – Patrick Hofman Feb 16 '17 at 13:04
  • write the code inside the Form_paint event. That must work – Mitz Feb 16 '17 at 13:07
  • your label is almost certainly overwriting your ellipse. – BugFinder Feb 16 '17 at 13:08
  • 1
    You are missing the whole concept of how drawing occurs in winforms. You can simply create `UserControl` where you combine controls (is there one to draw lines in winforms?) to have appearance you want or create custom control (e.g. see [this](http://stackoverflow.com/a/20997144/1997232)) which draw circle around in `Paint` event. – Sinatr Feb 16 '17 at 13:08
  • 1
    CreateGraphics() is a **temporary** drawing surface; it will get erased when the form/controls refresh themselves. The Paint() event of _something_ is the better approach; use the `e.Graphics` supplied within it. A control can't draw outside of its rectangular bounds, though...so if you want the circle "around" the Label it would have to drawn in the Paint() event of the **Labels Container**. – Idle_Mind Feb 16 '17 at 13:17
  • Refer this MSDN document. https://msdn.microsoft.com/en-us/library/h34kh0x2(v=vs.110).aspx – Mitz Feb 16 '17 at 13:20
  • [How can I treat the circle as a control after drawing it? - Moving and selecting shapes](https://stackoverflow.com/questions/38345828/how-can-i-treat-the-circle-as-a-control-after-drawing-it-moving-and-selecting) – Reza Aghaei Feb 16 '17 at 13:48
  • You also can save your drawing: [How to save shapes which I draw on a Panel as binary](http://stackoverflow.com/a/40575797/3110834) ` – Reza Aghaei Feb 16 '17 at 14:04
  • Also here is another example: [How to drag and move shapes in C#](http://stackoverflow.com/a/38749134/3110834) – Reza Aghaei Feb 16 '17 at 14:06
  • 1
    Forget about `Label` and use a solution like above examples. You will find theme useful :) – Reza Aghaei Feb 16 '17 at 14:08

1 Answers1

5

Try this:

private void Form1_Load(object sender, EventArgs e)
{
    Label Label = new Label();
    Label.Location = new System.Drawing.Point(50, 50);
    Label.Width = 50;
    Label.Height = 50;
    Label.Name = "lblTest";
    Label.Text = "test";
    this.Controls.Add(Label);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    var lbl = this.Controls.Find("lblTest",true); // find label with name

    foreach (var item in lbl) 
    // there can be multiple lblTest with same name so I used foreach (this is optional btw you can remove it)
    {
        Label tempLabel = item as Label;
        System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
        System.Drawing.Pen myPen = new Pen(myBrush, 2);
        e.Graphics.DrawEllipse(myPen, new System.Drawing.Rectangle(tempLabel.Location.X - (tempLabel.Width / 2),
        tempLabel.Location.Y - (tempLabel.Height / 2)  , tempLabel.Width + 40, tempLabel.Height + 40));
        myBrush.Dispose();
        myPen.Dispose();
    }
}

Result: enter image description here

Hope helps.

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37