Add ContextMeunStrip To Rectangle
Public Class Form1 Private list As List(Of String) = New List(Of String) Private rect As Rectangle = New Rectangle(10, 10, 100, 20) Private selectedText As String = Nothing Private Sub Form1_Load(sender As Object, e As EventArgs)
Handles MyBase.Load Me.list.Add("Omar") Me.List.Add("Amin") Me.List.Add("Ibrahim") End Sub Private Sub Form1_Paint(sender As Object, e As
PaintEventArgs) Handles MyBase.Paint Using sb As New SolidBrush(Color.Black) e.Graphics.FillRectangle(sb,
rect) Using sf As StringFormat = New
StringFormat sf.Alignment =
StringAlignment.Center sf.LineAlignment
= StringAlignment.Center
e.Graphics.DrawString(selectedText, Control.DefaultFont, Brushes.White,
rect, sf) End Using End Using End Sub Private Sub Form1_MouseDown(sender As Object, e As
MouseEventArgs) Handles MyBase.MouseDown Dim pt As Point =
Me.PointToClient(Cursor.Position) If rect.Contains(pt) Then Dim strip As ContextMenuStrip
= New ContextMenuStrip() For i As Integer = 0 To
Me.list.Count - 1 Dim s As String
= Me.list(i) Dim item As
ToolStripMenuItem = CType(strip.Items.Add(s, Nothing, AddressOf
Me.OnContextMenuItemClick), ToolStripMenuItem) item.Tag = s Next Dim stripLocation As Point =
New Point(Me.rect.Left + (rect.Width / 2), Me.rect.Bottom) strip.Show(Me, stripLocation) Return End If End Sub Private Sub OnContextMenuItemClick(sender As Object, ea As
EventArgs) If TypeOf sender Is ToolStripItem AndAlso
TypeOf (CType(sender, ToolStripItem)).Tag Is String Then Me.selectedText =
CType((CType(sender, ToolStripItem)).Tag, String) Invalidate(rect) End If End Sub End Class |
Comments
Post a Comment