Click here to Skip to main content
14,033,293 members
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 24 May 2007
Licenced CPOL

Developing a Line Control

, 24 May 2007
Rate this:
Please Sign up or sign in to vote.
This is a control that takes a starting point and an ending point as objects and draws a line between them
Screenshot - Snapshot.jpg


This code is a line control for .NET applications. The difference between this and a normal line drawn using the drawline method is that this control enables you to use the line as a control. You can use a context menu to it, you can detect whether the line is clicked or not. Also, you don't have to worry about the position of the line since it calculates everything for you. All you need to do is refresh it in the Form_Paint event.

The control has several properties with it. You can change the color of line, its width and the arrow cap style. You can also make the end of the line follow the mouse cursor.

Using the Code

There is no need to go through the line control code because it contains everything and you don't need to edit it (unless you want to add more features). For using the line control in your code, all that you need to do is import the *.dll file into your project and start using the code.

In the sample found in the zipped file, you can see that we can add a line by first clicking on a button which executes the following code:

If AddLineFlag = True Then
    L.EndObject = sender
    AddLineFlag = False
    Exit Sub
End If
L = New LineControl.Line
L.ContextMenuStrip = Me.ContextMenuStrip1
L.StartObject = sender
L.UseCursorAsEndObject = True
AddLineFlag = True

As seen in this code, we first check if there is already a line that we need to add or not. If there is no line, we create the line control, then assign the context menu to it and make the start object as the clicked button and the end of the line should follow the mouse. After setting all the properties, we add the control to the form and set the flag to true.

Private Sub ContextMenuStrip1_Opening_
    (ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) _
    Handles ContextMenuStrip1.Opening
    Dim SenderLine As LineControl.Line = sender.SourceControl
    Me.ToolStripTextBox1.Text = SenderLine.LineWidth
    Me.ToolStripMenuItem2.Checked = SenderLine.IsFlashing
End Sub

Private Sub ToolStripTextBox1_TextChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles ToolStripTextBox1.TextChanged
    If Me.ToolStripTextBox1.Text = "" Then
        Exit Sub
    End If
    If Me.ToolStripTextBox1.Text > 0 And Me.ToolStripTextBox1.Text <= 9 Then
        Dim SenderLine As LineControl.Line = sender.OwnerItem.Owner.SourceControl
        SenderLine.LineWidth = Me.ToolStripTextBox1.Text
    End If
End Sub

Private Sub ToolStripComboBox1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles ToolStripComboBox1.Click
    If Not TypeOf sender.owner.sourcecontrol Is LineControl.Line Then
        Exit Sub
    End If
    Dim SenderLine As LineControl.Line = sender.Owner.SourceControl
    Dim R As New Random()
    SenderLine.LineColor = Color.FromArgb(R.Next_
        (0, 255), R.Next(0, 255), R.Next(0, 255))
End Sub

Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
    If Not TypeOf sender.owner.sourcecontrol Is LineControl.Line Then
        Exit Sub
    End If
    Dim SenderLine As LineControl.Line = sender.Owner.SourceControl
    If Me.ToolStripMenuItem2.Checked = False Then
        Me.ToolStripMenuItem2.Checked = True
        Me.ToolStripMenuItem2.Checked = False
    End If
End Sub

The previous functions are for using the properties of the line. The first function is to setup the context menu items to reflect the current properties of the clicked line. The second function is to change the width of the line. The third function is to choose a random color for the line. And finally, the last function is to start or stop flashing the line. I needed this property because the project I developed this control for had to alert the user in one way or another and this is the best way to do it.

Points of Interest

This is the first full control that I have developed. Earlier, I was only writing a Hello World type of control. Also, this is my first article here and hope you like my work.


  • 24th May, 2007: First version of the control


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Saudi Arabia Saudi Arabia
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionLine Control Pin
denson16-Sep-09 15:22
memberdenson16-Sep-09 15:22 
GeneralRelated Article Pin
chimeric6929-May-07 17:28
memberchimeric6929-May-07 17:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190423.1 | Last Updated 24 May 2007
Article Copyright 2007 by alhokail
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid