CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2006
    Posts
    15

    Drawing on Mouse Click

    Im developing a project with .Net framework 1.1 using VB.Net.
    During runtime, on mouse click or mouse down event of the label1 i want to draw cross lines over the label1. I have tried with drawing2d to draw lines, but i want to handle that paint event of label1 during the mouse down or mouse click event. The function is given below to draw lines simply. But i have to draw the lines on the label.

    Private Sub Label1_Click(ByVal sender As Object, ByVal e As System. EventArgs) Handles Label1.Click
    Draw(da)
    End Sub

    Private Sub Draw(ByVal e As PaintEventArgs)
    e.Graphics.DrawLine(Pens.Black, New Point(10, 10), New Point(100, 50))
    End Sub

    Please can anyone help me out to solve this problem. Thanks in Advance

    Regards
    Mukil

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Drawing on Mouse Click

    Hello psmukilan!

    Is there any specific reason as to why you need to draw on a label ¿
    IMHO, drawing on a picturebox would make more sense, in that way, I could easily persist the drawing.

    Seeing the fact that you want to draw on a label, have a look at this :
    Code:
        Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
            Dim daX As New Point(0, 50) 'declare point 
            Dim daY As New Point(50, 100) 'declare another point 
    
            Dim g As Graphics = Label1.CreateGraphics() 'enable drawing capabilities on label
    
            Dim pPen As New Pen(Color.Red) 'new pen
            pPen.Width = 5 'size of pen
    
            g.DrawLine(pPen, 1, 1, 50, 50) 'draw the line 
    
            g.DrawLine(pPen, daX, daY) 'draw another line using the points
    
        End Sub
    You might want to have a look at GDI+ on MSDN

  3. #3
    Join Date
    Mar 2006
    Posts
    15

    Re: Drawing on Mouse Click

    Thanks a lot for your reply, Its working fine on mouse click, but whne the form gets restored, lines are invisible, that is redraw is not taking place.

    The code is written below. Please give me your suggestion. I have tried to redarw while the form is getting activated.

    Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
    DrawCrossLine(Label1)
    ControlNames.Add(Label1.Name)
    End Sub

    Private Sub DrawCrossLine(ByVal ControlName As Control)
    Dim daX As New Point(0, 50) 'declare point
    Dim daY As New Point(850, 100) 'declare another point

    Dim g As Graphics = ControlName.CreateGraphics()
    'Dim g As Graphics = Label1.CreateGraphics() 'enable drawing capabilities on label

    Dim pPen As New Pen(Color.Red) 'new pen
    pPen.Width = 2 'size of pen

    'g.DrawLine(pPen, 1, 1, 250, 50) 'draw the line
    'g.DrawLine(pPen, 1, 50, 50, 1) 'draw the line

    Dim pts() As Point
    pts = New Point() {New Point(1, 5), New Point(108, 30), _
    New Point(108, 1), New Point(1, 30)}
    g.DrawPolygon(pPen, pts)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    AddHandler Label1.Paint, AddressOf myClickHandler
    End Sub

    Private Sub myClickHandler(ByVal sender As System.Object, ByVal e As PaintEventArgs)
    e.Graphics.DrawLine(Pens.Black, New Point(10, 10), New Point(100, 50))
    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
    ControlNames.Add(Label2.Name)
    DrawCrossLine(Label2)
    End Sub

    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    Static Done As Boolean = False
    Dim i As Integer = 0
    Dim name As String
    If Not Done And ControlNames.Count > 0 Then
    For Each ctrl As Control In Me.Controls
    Select Case True
    Case TypeOf ctrl Is Label
    name = System.Convert.ToString(ControlNames.Item(i))
    If ctrl.Name = name Then
    DrawCrossLine(ctrl)
    i = i + 1
    End If
    End Select
    Next
    End If
    End Sub

    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
    DrawCrossLine(Label1)
    End Sub

    Private Sub Label2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label2.Paint
    DrawCrossLine(Label2)
    End Sub
    End Class

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Drawing on Mouse Click

    Hello again psmukilan!

    Your whole problem is the Form_Activate event.
    Remember the Activate event also fiers when the form is busy loading. based on that, your Polygons should have been drawn when the form is first shown as well.

    BTW, what is that select case True in your Form_Activate event - it should not be like that!

    Personally, what I'd do here is just to do this in the Form_Click event :

    Code:
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
    DrawCrossLine(Label1)
    DrawCrossLine(Label2)
    End Sub
    Your Activate event doesn't work. By doing the above, when the user clicks on the form, it will draw those lines again. It won't work with clicking the TitleBar though, but it should give you an idea of what to do

    Are you Happy with this ¿

    PS : Just remember using CODE tags when posting code, have a look in my signature to see how it is done.
    Last edited by HanneSThEGreaT; August 2nd, 2006 at 08:56 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured