[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

Draw a filled square or circle .... at my x,y

I have been marking places on my map with small picture boxes in fact they are 5x5 and are red ... it looks like a small red square .... sort of like the square you see on wikimapia when you select the measuring tool from geotools ... it gives you this line with a sqaure to mark each selected point.

I am wanting to make a square or a circle that size to mark my map and do away with the pictur box controls ...

I am going to give an example of how I am marking my map and ask if you could show me how to mark the map with a filled square or circle ....

Here is the code
If mMarkers = True Then
            Dim myX As Integer
            Dim myY As Integer
            myX = (tlLon - myLon) / delX
            myY = (tlLat - myLattitude) / delY
            Me.Text = myX & "," & myY
 
            Dim PB As New PictureBox ' Create a PictureBox
            Dim mT As New ToolTip
 
            mT.SetToolTip(PB, "The X,Y Coordinates are: " & myX & ", " & myY) 
            mT.IsBalloon = True
            mT.ToolTipTitle = "Lat: " & myLattitude & " Lon: " & myLon
            mT.BackColor = Color.Azure
 
 
            With PB
                .Location = New System.Drawing.Point(myX, myY) ' on Mouse click place PB
                .Size = New Size(5, 5)
                .BackColor = Color.Red
                .BringToFront()
 
 
 
            End With
 
            Me.PictureBox1.Controls.Add(PB)
 
            AddHandler PB.Click, AddressOf MyPicClicked
        Else
            ' Dont place a marker
        End If

Open in new window

0
vbMarkO
Asked:
vbMarkO
  • 5
  • 4
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What are you "marking" ON?...is it a PictureBox that has the Map in it?

Anyhoo, just store the x,y values in an ArrayList or List and then in the Paint() event iterate thru them and draw the markers...
0
 
vbMarkOAuthor Commented:
I have a image inside a picturebox ..... I presently mark a spot by clicking the place I want and a smaller picuture box is created as seen in the code above and placed at that x,y coordinate.
I used pictureboxes to mark the spot cause I do not kjnow how to use the draw features of vb this is my work around

I would like to use a filled square or circle .... to mark the area I click on .... instead of using picturebox controls

How do I do it?

Ok about the arrays ... not needed ... as I am marking the spot on mouse click so the mark should be placed where the user clicks within in the picture box .....

Mark

0
 
vbMarkOAuthor Commented:
My bad .... left something out

The code I gave is within the Picturebox1_Mouseclick event

1. User clicks on spot on map
2. Code above is activated creates a smaller picture box and places it a the x,y coordinates where user clicked.

Iagain simply would not want to use pictureboxes ... ideally I would like to use a drawn filled square to mark the spot .... then when the users is ready can alos simply clear picture box of all marks they placed on the map.

Since this happens in the Picturebox mouseclick event I do not see the need for arrays to iterate through as the mark is placed at the e.X & e.Y or in my code the myX and myY x,y coordiinates.

I hope this is clear if not ask away

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This is really simpler to implement than you think!...

The following code allows you to place as many circular dots on the PictureBox as you want...and also shows how to erase all of them:
Public Class Form1
 
    Private marks As New List(Of Point)
 
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        marks.Add(PictureBox1.PointToClient(Cursor.Position))
        PictureBox1.Refresh()
    End Sub
 
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        For Each pt As Point In marks
            Dim rc As Rectangle = New Rectangle(pt.X, pt.Y, 1, 1)
            rc.Inflate(6, 6)
            e.Graphics.FillEllipse(Brushes.Red, rc)
        Next
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' clear all the marks
        marks.Clear()
        PictureBox1.Refresh()
    End Sub
 
End Class

Open in new window

0
 
vbMarkOAuthor Commented:
This is simple ... I had no idea ..... and it works perfectly ... may I ask .... can I assign a tooltip to it as I did to the pb's I used ..... I didn't see any properties for it .....

Note this the solution .... I'll understand if you would not want answer a 2nd question but couldnt help but ask


Great code
0
 
vbMarkOAuthor Commented:
Easy to understand and was exactly what I needed .. works perfect
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Therein lies the tradeoff...since we are not making actual controls we have to manage any custom action by manually hit-testing the cursor position against all known "marks".

I'll post another example later today...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try this example out...the form has an additional Label and a ContextMenuStrip with a menu called "Delete":
Public Class Form1
 
    Private Dist As Integer = 6
    Private Counter As Integer
    Private Marks As New List(Of Mark)
    Private CurMark As Mark = Nothing
 
    Private Class Mark
 
        Public Pt As Point
        Public Data As String
 
        Public Sub New(ByVal pt As Point, ByVal data As String)
            Me.Pt = pt
            Me.Data = data
        End Sub
 
    End Class
 
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            DeleteToolStripMenuItem.Visible = Not IsNothing(CurMark)
            PictureBox1.ContextMenuStrip = ContextMenuStrip1
        ElseIf e.Button = Windows.Forms.MouseButtons.Left Then
            Counter = Counter + 1
            Marks.Add(New Mark(PictureBox1.PointToClient(Cursor.Position), "Mark #" & Counter))
            PictureBox1.Refresh()
        End If
    End Sub
 
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        For Each m As Mark In Marks
            Dim rc As Rectangle = New Rectangle(m.Pt.X, m.Pt.Y, 1, 1)
            rc.Inflate(Dist, Dist)
            If rc.Contains(New Point(e.X, e.Y)) Then
                CurMark = m
                Label1.Text = m.Data
                Exit Sub
            End If
        Next
        CurMark = Nothing
        Label1.Text = ""
    End Sub
 
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        For Each m As Mark In Marks
            Dim rc As Rectangle = New Rectangle(m.Pt.X, m.Pt.Y, 1, 1)
            rc.Inflate(Dist, Dist)
            e.Graphics.FillEllipse(Brushes.Red, rc)
        Next
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' clear all the marks
        Marks.Clear()
        CurMark = Nothing
        Label1.Text = ""
        PictureBox1.Refresh()
    End Sub
 
    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        If Not IsNothing(CurMark) Then
            Marks.Remove(CurMark)
            CurMark = Nothing
            Label1.Text = ""
            PictureBox1.Refresh()
        End If
    End Sub
 
End Class

Open in new window

PictureBoxMarks.jpg
0
 
vbMarkOAuthor Commented:
This was interesting ... and a real eye opener

I am playing this cod right now ... thanx very much .... let me really think about othe methods I would not have even begin to consider

Mark
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now