Introduction
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
Me.Controls.Add(L)
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
SenderLine.StartFlashLine(Color.Red)
Me.ToolStripMenuItem2.Checked = True
Else
SenderLine.EndFlashLine()
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.
History
- 24th May, 2007: First version of the control