Solved

Create draggable retangle shapes in a Picturebox.

Posted on 2004-10-06
6
813 Views
Last Modified: 2012-06-27
I want the users of a Windows Forms application to be able to drag simple rectangle shapes arround in a picturebox and then save this picture as a file.

I have no problem with saving the file. The problem I have is creating draggable rectangle shapes. These rectagle shapes will be created by the application. The only thing the user needs to do, is drag them arround and place them in the correct position.

For those who thinks this is kind of wierd: This is for an application used by sales representative from a sign company. We want the sales representative to be able to give us a sketch of the layout of the signs on the post.

Thanks everyone.
0
Comment
Question by:DJ_Back-Q
6 Comments
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 12242076
One thing you could do would be to create your own class and inherit from Panel.

Public Class Sign: Inherits Panel

   'Override the Paint method and draw whatever shapes you need inside the bounds of the panel

End

This way you can use standard drag and drop functionality exposed from the base class.

However, as far as dragging it around the picture box - I'm not sure.

0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12245500
If it is going to be just Rectangular shapes then what you can do is use a Label control instead of Drawing a Rectangle

What I am suggesting is When user draws a Rectangle inside the Picture box Dynamically add a Lable control with visible border and no text so that it looks like a rectangle. Now when he needs to  moves this rectangle (i.e. the Label control) you can use the Drag Drop events to actually move the Lable.

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12246061
Hi DJ_Back-Q,

    I've been wanting to write something similar for awhile so I decided to go ahead and whip something up for you.  First of all, don't be intimidated by the amount of code posted below.  The app consists of only one form and one serializable class.  Build the project and play with it first before you make any judgements.

    You create rectangles in the picturebox by simply dragging the mouse.  A realtime rubberband box will appear to show you the size of the rectangle as you drag.  Once the rectangle has been created, you can move and resize it by dragging it from the middle or one of its edges.  Double click on a rectangle to change the text in it.  While you are editing the text, press F9 to change the text font and F10 to change the text color.  When you are done editing, press the Escape key to exit the edit mode.  The "Load Layout" and "Save Layout" buttons do just that.  They load/save the layout of your rectangles to/from a file using Binary Serialization.  Finally, you can use the "Delete Sign" button, to remove a rectangle from your layout.

    Create a new project and paste the form code below over the default form code.  Add a class and paste the class code below over the default class code.  Run and enjoy.

Regards,

Idle_Mind

' -------------------------------------------------------------------------------------
'  Class Code
' -------------------------------------------------------------------------------------
<Serializable()> Public Class Sign

    Public Enum SignEdge
        None = 0
        North = 1
        NorthEast = 2
        East = 3
        SouthEast = 4
        South = 5
        SouthWest = 6
        West = 7
        NorthWest = 8
    End Enum

    Public Enum Action
        None = 0
        Resize = 1
        Move = 2
        Editing = 3
    End Enum

    Public Text As String
    Public TextFont As Font
    Public TextColor As Color = Color.Black
    Private Rect As Rectangle

    Public ReadOnly Property signSize() As Size
        Get
            Return Rect.Size
        End Get
    End Property

    Public ReadOnly Property signLocation() As Point
        Get
            Return New Point(Rect.X, Rect.Y)
        End Get
    End Property

    Public Property signAction() As Action
        Get
            Return curAction
        End Get
        Set(ByVal Value As Action)
            curAction = Value
        End Set
    End Property

    <NonSerialized()> Public Highlighted As Boolean
    <NonSerialized()> Private draggingEdge As SignEdge
    <NonSerialized()> Private startX As Integer
    <NonSerialized()> Private startY As Integer
    <NonSerialized()> Private curAction As Action = Action.None
    <NonSerialized()> Private Const EdgeThreshold As Byte = 7
    <NonSerialized()> Private WithEvents sourcePictureBox As PictureBox

    Private Sub New()
    End Sub

    Public Sub New(ByVal r As Rectangle)
        Rect = r
    End Sub

    Public Sub Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        If Highlighted Then
            e.Graphics.DrawRectangle(Pens.Red, Rect)
        Else
            e.Graphics.DrawRectangle(Pens.Black, Rect)
        End If
        Dim b As New SolidBrush(TextColor)
        e.Graphics.DrawString(Text, _
            IIf(TextFont Is Nothing, sender.font, TextFont), _
            b, New RectangleF(Rect.X, Rect.Y, Rect.Width, Rect.Height))
        b.Dispose()
    End Sub

    Public Function isOverEdge(ByVal x As Integer, ByVal y As Integer) As SignEdge
        Dim r As Rectangle

        r = New Rectangle(Rect.X, Rect.Y - EdgeThreshold, Rect.Width, EdgeThreshold)
        If r.Contains(x, y) Then Return SignEdge.North

        r = New Rectangle(Rect.X + Rect.Width, Rect.Y - EdgeThreshold, EdgeThreshold, EdgeThreshold)
        If r.Contains(x, y) Then Return SignEdge.NorthEast

        r = New Rectangle(Rect.X + Rect.Width, Rect.Y, EdgeThreshold, Rect.Height)
        If r.Contains(x, y) Then Return SignEdge.East

        r = New Rectangle(Rect.X + Rect.Width, Rect.Y + Rect.Height, EdgeThreshold, EdgeThreshold)
        If r.Contains(x, y) Then Return SignEdge.SouthEast

        r = New Rectangle(Rect.X, Rect.Y + Rect.Height, Rect.Width, EdgeThreshold)
        If r.Contains(x, y) Then Return SignEdge.South

        r = New Rectangle(Rect.X - EdgeThreshold, Rect.Y + Rect.Height, EdgeThreshold, EdgeThreshold)
        If r.Contains(x, y) Then Return SignEdge.SouthWest

        r = New Rectangle(Rect.X - EdgeThreshold, Rect.Y, EdgeThreshold, Rect.Height)
        If r.Contains(x, y) Then Return SignEdge.West

        r = New Rectangle(Rect.X - EdgeThreshold, Rect.Y - EdgeThreshold, EdgeThreshold, EdgeThreshold)
        If r.Contains(x, y) Then Return SignEdge.NorthWest

        Return SignEdge.None
    End Function

    Public Function isOverSign(ByVal x As Integer, ByVal y As Integer) As Boolean
        Return Rect.Contains(x, y)
    End Function

    Public Sub dragEdge(ByVal pb As PictureBox, ByVal edge As SignEdge, ByVal x As Integer, ByVal y As Integer)
        curAction = Action.Resize
        draggingEdge = edge
        startX = x
        startY = y
        sourcePictureBox = pb
    End Sub

    Public Sub dragSign(ByVal pb As PictureBox, ByVal x As Integer, ByVal y As Integer)
        curAction = Action.Move
        startX = x
        startY = y
        sourcePictureBox = pb
    End Sub

    Private Sub sourcePictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sourcePictureBox.MouseMove
        Dim p As Point
        Select Case curAction
            Case Action.Resize
                Select Case draggingEdge
                    Case SignEdge.North
                        If e.Y < ((Rect.Y + Rect.Height) - EdgeThreshold) Then
                            Rect.Y = Rect.Y + (e.Y - startY)
                            Rect.Height = Rect.Height - (e.Y - startY)
                        Else
                            Rect.Y = (Rect.Y + Rect.Height) - EdgeThreshold
                            Rect.Height = EdgeThreshold
                            p = sender.PointToScreen(New Point(e.X, Rect.Y))
                            Cursor.Position = p
                        End If

                    Case SignEdge.NorthEast
                        If e.Y < ((Rect.Y + Rect.Height) - EdgeThreshold) Then
                            Rect.Y = Rect.Y + (e.Y - startY)
                            Rect.Height = Rect.Height - (e.Y - startY)
                        Else
                            Rect.Y = (Rect.Y + Rect.Height) - EdgeThreshold
                            Rect.Height = EdgeThreshold
                            p = sender.PointToScreen(New Point(e.X, Rect.Y))
                            Cursor.Position = p
                        End If
                        If e.X > (Rect.X + EdgeThreshold) Then
                            Rect.Width = Rect.Width + (e.X - startX)
                        Else
                            Rect.Width = EdgeThreshold
                            If e.Y < ((Rect.Y + Rect.Height) - EdgeThreshold) Then
                                p = sender.PointToScreen(New Point(Rect.X + EdgeThreshold, e.Y))
                            Else
                                p = sender.PointToScreen(New Point(Rect.X + EdgeThreshold, Rect.Y))
                            End If
                            Cursor.Position = p
                        End If

                    Case SignEdge.East
                        If e.X > (Rect.X + EdgeThreshold) Then
                            Rect.Width = Rect.Width + (e.X - startX)
                        Else
                            Rect.Width = EdgeThreshold
                            p = sender.PointToScreen(New Point(Rect.X + EdgeThreshold, e.Y))
                            Cursor.Position = p
                        End If

                    Case SignEdge.SouthEast
                        If e.Y > (Rect.Y + EdgeThreshold) Then
                            Rect.Height = Rect.Height + (e.Y - startY)
                        Else
                            Rect.Height = EdgeThreshold
                            p = sender.PointToScreen(New Point(e.X, (Rect.Y + EdgeThreshold)))
                            Cursor.Position = p
                        End If
                        If e.X > (Rect.X + EdgeThreshold) Then
                            Rect.Width = Rect.Width + (e.X - startX)
                        Else
                            Rect.Width = EdgeThreshold
                            If e.Y > (Rect.Y + EdgeThreshold) Then
                                p = sender.PointToScreen(New Point(Rect.X + EdgeThreshold, e.Y))
                            Else
                                p = sender.PointToScreen(New Point(Rect.X + EdgeThreshold, (Rect.Y + EdgeThreshold)))
                            End If
                            Cursor.Position = p
                        End If

                    Case SignEdge.South
                        If e.Y > (Rect.Y + EdgeThreshold) Then
                            Rect.Height = Rect.Height + (e.Y - startY)
                        Else
                            Rect.Height = EdgeThreshold
                            p = sender.PointToScreen(New Point(e.X, (Rect.Y + EdgeThreshold)))
                            Cursor.Position = p
                        End If

                    Case SignEdge.SouthWest
                        If e.Y > (Rect.Y + EdgeThreshold) Then
                            Rect.Height = Rect.Height + (e.Y - startY)
                        Else
                            Rect.Height = EdgeThreshold
                            p = sender.PointToScreen(New Point(e.X, (Rect.Y + EdgeThreshold)))
                            Cursor.Position = p
                        End If
                        If e.X < ((Rect.X + Rect.Width) - EdgeThreshold) Then
                            Rect.X = Rect.X + (e.X - startX)
                            Rect.Width = Rect.Width - (e.X - startX)
                        Else
                            Rect.X = (Rect.X + Rect.Width) - EdgeThreshold
                            Rect.Width = EdgeThreshold
                            If e.Y > (Rect.Y + EdgeThreshold) Then
                                p = sender.PointToScreen(New Point(Rect.X, e.Y))
                            Else
                                p = sender.PointToScreen(New Point(Rect.X, (Rect.Y + EdgeThreshold)))
                            End If
                            Cursor.Position = p
                        End If

                    Case SignEdge.West
                        If e.X < ((Rect.X + Rect.Width) - EdgeThreshold) Then
                            Rect.X = Rect.X + (e.X - startX)
                            Rect.Width = Rect.Width - (e.X - startX)
                        Else
                            Rect.X = (Rect.X + Rect.Width) - EdgeThreshold
                            Rect.Width = EdgeThreshold
                            p = sender.PointToScreen(New Point(Rect.X, e.Y))
                            Cursor.Position = p
                        End If

                    Case SignEdge.NorthWest
                        If e.Y < ((Rect.Y + Rect.Height) - EdgeThreshold) Then
                            Rect.Y = Rect.Y + (e.Y - startY)
                            Rect.Height = Rect.Height - (e.Y - startY)
                        Else
                            Rect.Y = (Rect.Y + Rect.Height) - EdgeThreshold
                            Rect.Height = EdgeThreshold
                            p = sender.PointToScreen(New Point(e.X, Rect.Y))
                            Cursor.Position = p
                        End If
                        If e.X < ((Rect.X + Rect.Width) - EdgeThreshold) Then
                            Rect.X = Rect.X + (e.X - startX)
                            Rect.Width = Rect.Width - (e.X - startX)
                        Else
                            Rect.X = (Rect.X + Rect.Width) - EdgeThreshold
                            Rect.Width = EdgeThreshold
                            If e.Y < ((Rect.Y + Rect.Height) - EdgeThreshold) Then
                                p = sender.PointToScreen(New Point(Rect.X, e.Y))
                            Else
                                p = sender.PointToScreen(New Point(Rect.X, Rect.Y))
                            End If
                            Cursor.Position = p
                        End If

                End Select
                startX = e.X
                startY = e.Y
                sender.Refresh()

            Case Action.Move
                Rect.X = Rect.X + (e.X - startX)
                Rect.Y = Rect.Y + (e.Y - startY)
                startX = e.X
                startY = e.Y
                sender.Refresh()

        End Select
    End Sub

    Private Sub sourcePictureBox_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sourcePictureBox.MouseUp
        sourcePictureBox = Nothing
        curAction = Action.None
    End Sub

End Class

' -------------------------------------------------------------------------------------
'  Form Code
' -------------------------------------------------------------------------------------
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog
    Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents Button3 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.FontDialog1 = New System.Windows.Forms.FontDialog
        Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.Button3 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.PictureBox1.Location = New System.Drawing.Point(8, 40)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(504, 336)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(112, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Load Layout"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(128, 8)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(112, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Save Layout"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(248, 8)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(112, 24)
        Me.Button3.TabIndex = 3
        Me.Button3.Text = "Delete Sign"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
        Me.ClientSize = New System.Drawing.Size(520, 382)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "Form1"
        Me.Text = "RubberBanding ""Signs"" in a PictureBox"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Enum Action
        None = 0
        Creating = 1
        Sizing = 2
        Moving = 3
        Deleting = 4
    End Enum

    Private startX As Integer
    Private startY As Integer
    Private lastX As Integer
    Private lastY As Integer
    Private firstBoxDrawn As Boolean = False
    Private Signs As New ArrayList
    Private curAction As Action = Action.None
    Private s As Sign
    Private se As Sign.SignEdge
    Private curSign As Sign

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = MouseButtons.Left Then
            If curAction = Action.None Then
                Dim p As Point = PictureBox1.PointToScreen(New Point(e.X, e.Y))
                startX = p.X
                startY = p.Y
            ElseIf curAction = Action.Deleting Then
                If Not (curSign Is Nothing) Then
                    Signs.Remove(curSign)
                    PictureBox1.Refresh()
                End If
                Button3.Enabled = True
                curAction = Action.None
            End If
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        Dim p As Point
        If e.Button = MouseButtons.Left Then
            Select Case curAction
                Case Action.None
                    Cursor.Clip = sender.RectangleToScreen(New Rectangle(0, 0, sender.Width, sender.Height))
                    If Not (s Is Nothing) Then
                        If se = Sign.SignEdge.None Then
                            curAction = Action.Moving
                            s.dragSign(sender, e.X, e.Y)
                        Else
                            curAction = Action.Sizing
                            s.dragEdge(sender, se, e.X, e.Y)
                        End If
                    Else
                        curAction = Action.Creating
                        firstBoxDrawn = False
                        createSign(sender, e)
                    End If

                Case Action.Creating
                    createSign(sender, e)

            End Select
        ElseIf curAction = Action.None Or curAction = Action.Deleting Then
            HighlightSign(sender, e)
        End If
    End Sub

    Private Sub HighlightSign(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        overSign(sender, e)
        If s Is Nothing Then
            If Not (curSign Is Nothing) Then
                curSign.Highlighted = False
                curSign = Nothing
                sender.Refresh()
            End If
        Else
            If curSign Is Nothing Then
                curSign = s
                curSign.Highlighted = True
                sender.Refresh()
            ElseIf Not (curSign Is s) Then
                curSign.Highlighted = False
                curSign = s
                curSign.Highlighted = True
                sender.Refresh()
            End If
        End If
    End Sub

    Private Sub overSign(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        s = Nothing
        se = Sign.SignEdge.None

        For Each s In Signs
            If s.signAction = Sign.Action.None And s.isOverSign(e.X, e.Y) Then
                sender.Cursor.Current = Cursors.SizeAll
                Exit Sub
            End If
        Next

        For Each s In Signs
            If s.signAction = Sign.Action.None Then
                se = s.isOverEdge(e.X, e.Y)
                If se <> Sign.SignEdge.None Then
                    Select Case se
                        Case Sign.SignEdge.North, Sign.SignEdge.South
                            sender.Cursor.Current = Cursors.SizeNS

                        Case Sign.SignEdge.NorthEast, Sign.SignEdge.SouthWest
                            sender.Cursor.Current = Cursors.SizeNESW

                        Case Sign.SignEdge.East, Sign.SignEdge.West
                            sender.Cursor.Current = Cursors.SizeWE

                        Case Sign.SignEdge.SouthEast, Sign.SignEdge.NorthWest
                            sender.Cursor.Current = Cursors.SizeNWSE

                    End Select
                    Exit Sub
                End If
            End If
        Next

        s = Nothing
        se = Sign.SignEdge.None
        sender.Cursor.Current = Cursors.Default
    End Sub

    Private Sub createSign(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim p As Point = sender.PointToScreen(New Point(e.X, e.Y))
        If Not firstBoxDrawn Then
            firstBoxDrawn = True
            ControlPaint.DrawReversibleFrame(New Rectangle(startX, startY, p.X - startX, p.Y - startY), Color.Black, FrameStyle.Dashed)
        Else
            ControlPaint.DrawReversibleFrame(New Rectangle(startX, startY, lastX - startX, lastY - startY), Color.Black, FrameStyle.Dashed)
            ControlPaint.DrawReversibleFrame(New Rectangle(startX, startY, p.X - startX, p.Y - startY), Color.Black, FrameStyle.Dashed)
        End If
        lastX = p.X
        lastY = p.Y
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        If e.Button = MouseButtons.Left Then
            Cursor.Clip = New Rectangle(0, 0, Screen.GetBounds(New Point(0, 0)).Width, Screen.GetBounds(New Point(0, 0)).Height)
            If curAction = Action.Creating Then
                If firstBoxDrawn Then
                    Dim r As Rectangle = New Rectangle(Math.Min(startX, lastX), Math.Min(startY, lastY), Math.Abs(lastX - startX), Math.Abs(lastY - startY))
                    ControlPaint.DrawReversibleFrame(r, Color.Black, FrameStyle.Dashed)
                    r = PictureBox1.RectangleToClient(r)
                    Dim s As New Sign(r)
                    s.Text = "New Sign" & vbCrLf & vbCrLf & Now().ToString
                    Signs.Add(s)
                    sender.Refresh()
                End If
            End If
            curAction = Action.None
            PictureBox1.Cursor.Show()
        End If
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim s As Sign
        For Each s In Signs
            s.Paint(sender, e)
        Next
    End Sub

    Private Sub PictureBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
        Dim s As Sign
        Dim p As Point
        For Each s In Signs
            p = PictureBox1.PointToClient(Cursor.Position)
            If s.isOverSign(p.X, p.Y) Then
                Dim tb As New TextBox
                tb.BorderStyle = BorderStyle.FixedSingle
                tb.Multiline = True
                tb.Location = Me.PointToClient(PictureBox1.PointToScreen(s.signLocation))
                tb.Size = New Size(s.signSize.Width + 1, s.signSize.Height + 1)
                tb.Text = s.Text
                If s.TextFont Is Nothing Then
                    tb.Font = PictureBox1.Font
                Else
                    tb.Font = s.TextFont
                End If
                tb.ForeColor = s.TextColor
                tb.Tag = s

                AddHandler tb.KeyPress, AddressOf Me.tb_KeyPress
                AddHandler tb.KeyDown, AddressOf Me.tb_KeyDown
                Me.Controls.Add(tb)
                tb.Visible = True
                tb.BringToFront()
                tb.Focus()
                tb.SelectionStart = tb.Text.Length
                s.signAction = Sign.Action.Editing
                Exit Sub
            End If
        Next
    End Sub

    Private Sub tb_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Asc(e.KeyChar) = 27 Then ' Escape
            sender.tag.text = sender.text
            sender.tag.signaction = Sign.Action.None
            Me.Controls.Remove(sender)
            PictureBox1.Refresh()
        End If
    End Sub

    Private Sub tb_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.F9 Then
            If FontDialog1.ShowDialog = DialogResult.OK Then
                sender.Tag.TextFont = FontDialog1.Font
                sender.Font = FontDialog1.Font
            End If
        ElseIf e.KeyCode = Keys.F10 Then
            If ColorDialog1.ShowDialog = DialogResult.OK Then
                sender.Tag.TextColor = ColorDialog1.Color
                sender.ForeColor = ColorDialog1.Color
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Title = "Open a Previously Saved Layout"
        OpenFileDialog1.Filter = "Sign Layout Files (*.sly)|*.sly|All files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim fs As New FileStream(OpenFileDialog1.FileName, FileMode.OpenOrCreate)
            Dim formatter As New BinaryFormatter
            Try
                Signs = DirectCast(formatter.Deserialize(fs), ArrayList)
                PictureBox1.Refresh()
            Catch x As SerializationException
                MsgBox("Failed to Deserialize. Reason: " & x.Message)
                Throw
            Finally
                fs.Close()
            End Try
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SaveFileDialog1.Title = "Save Sign Layout to a File"
        SaveFileDialog1.Filter = "Sign Layout Files (*.sly)|*.sly|All files (*.*)|*.*"
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            Dim fs As New FileStream(SaveFileDialog1.FileName, FileMode.Create)
            Dim formatter As New BinaryFormatter
            Try
                formatter.Serialize(fs, Signs)
            Catch x As SerializationException
                MsgBox("Failed to serialize. Reason: " & x.Message)
                Throw
            Finally
                fs.Close()
                MsgBox(SaveFileDialog1.FileName, MsgBoxStyle.Information, "Sign Layout Saved")
            End Try
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button3.Enabled = False
        curAction = Action.Deleting
    End Sub

End Class
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 3

Author Comment

by:DJ_Back-Q
ID: 12247411
Thanks everyone,

I will have to go with Idle_Mind, that guy wrote the whole damn code for god sakes!!! It's exactly what I wanted.

Thanks alot Idle_Mind, you know what you do.
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12247599
Vey cool, Idle_Mind
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 12247742
Very nice, Idle_Mind.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now