Drawing Lines Through A Label Control

jtstanish

Newcomer
Joined
Dec 10, 2004
Messages
10
I have several label controls on a form.
The first time the user CLICKS on a LABEL I want to draw a line from the top left corner to the top left to bottom right of that LABEL.
The second time the user CLICKS on a LABEL I want to draw a line from the bottom left corner to the top right to bottom right of that LABEL.
The third time the user CLICKS on a LABEL I want to draw a line from the middle left to the middle right of that LABEL.
The forth time the user CLICKS on a LABEL I want to draw a line from the middle top to the middle bottom of that LABEL.

This will form an "x" & a "+" over the LABEL. (i.e. Label is completely crossed out" Each LABEL will be 100 pixels wide by 100 pixels high.

I NEED A CODE EXAMPLE. I HAVE NEVER DRAWN LINES OVER LABEL CONTROLS BEFORE. I don't even know how to draw a line using reletive coordinates (0,0 being the top left and 100,100 being the bottom right.)

CAN ANYONE HELP ME???
:) :) :)
 
You need to create your own derived class based on the standard label control and in that paint method of the derived class do what ever you like with the lines you want.

So look for derived class examples, there are pleanty of them available.
 
Can't you just create a graphics object to the label control and draw lines using the graphics object?

Visual Basic:
dim glbl as graphics = label1.creategraphics
dim clicks as integer
private sub label1.click(blah)
clicks = clicks + 1
select case clicks
case 1
glbl.drawline(new pen(color.black), 0,0,100,100)
case 2
glbl.drawline(new pen(color.black),0,100,100,0)
case 3
glbl.drawline(new pen(color.black),0,50,100,50)
case 4
glbl.drawline(new pen(color.black),50,0,50,100)
case 5
'labeliscompletelycrossedout
end select
 
jstanish:
I put the together your requirement real simply inside of a test form (frmCustomLabel). I agree with donnacha in suggesting you derive your own Label and wrap up the logic inside of it....especially if you are to need this same label functionality all over the place.

Hope this helps:

Code:
private lblClick = 0;

public frmCustomLabel()
{
    //
    // Required for Windows Form Designer support
   //
   InitializeComponent();

   // Register Events
   this.label1.Click+=new EventHandler(label1_Click);
   this.label1.Paint+=new PaintEventHandler(label1_Paint);
}


private void label1_Click(object sender, System.EventArgs e)
{
   if(this.lblClick <= 4)
   {
      this.lblClick++;
      this.label1.Invalidate();
   }
}

private void label1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
   if(lblClick >= 1)
   {
      e.Graphics.DrawLine(Pens.Blue, new Point(0, 0), new Point(label1.Width, label1.Height));
   }

   if(lblClick >= 2)
   {
      e.Graphics.DrawLine(Pens.Blue, new Point(0, label1.Height), new Point(label1.Width, 0));
   }

   if(lblClick >=3)
   {
      e.Graphics.DrawLine(Pens.Blue, new Point(0, label1.Height/2), new Point(label1.Width, label1.Height/2));
   }

   if(lblClick >= 4)
   {
      e.Graphics.DrawLine(Pens.Blue, new Point(label1.Width/2, 0), new Point(label1.Width/2, label1.Height));
   }
}
 
Last edited:
How about a sub that will do it

Visual Basic:
dim g as graphics
private sub drawyourcrosses(byval label as label)
g = label.creategraphics
'dowhatididbefore
end sub
and then in your label click event simple call that sub with the name of the label that was clicked?

Btw: Your last post made it seem like I was the original poster, I'm not.
 
thenerd:
Thanks for pointing out that I had addressed my reply to the wrong poster.....I fixed my previous post.
The problem with what you are suggesting is that the lines will only be drawn when the Label is clicked on. The lines will disappear as soon as the Label is invalidated and redrawn. The reason you always do custom drawing from a Paint event handler is that the Paint event is raised every time Windows redraws the interface (something that happens very excessively).
It is also a best practice to keep an independent object's logic encapsulated inside of itself.......it makes your code reusable and keeps your application code tidy and easy to understand. This is why donnacha and I are chanting the 'derive your own Control' mantra. :p
 
Well, that's easy enough to fix
using the sub I said before and the code I said before it:
private sub label1_paint(blah)
drawyourcrosses(label1)
end sub

But ok.. I guess you win, It would be annoying to have to put that into every label's paint sub. Though I still think it would be easier for newbies like me.
 
You could use the non-paint sub and set the label's background or image properity to the drawn bitmap. This allows for further painting in the label's paint event if necessary and the background or image is also redrawn with the label.
 
Back
Top