|
-
Mar 5th, 2007, 10:14 AM
#1
Thread Starter
Member
Rectangle Click Event
Is there a Way to Add a Click event to a Rectangle that is created at run time?
I use the Following Code to Create the Rectangle:
Code:
Dim g As Graphics = TableLayoutPanel1.CreateGraphics()
Dim rect As RectangleF = New Rectangle(100, 100, 100, 400)
g.FillRectangle(Brushes.Red, rect)
g.Dispose()
I want this rectangle to trigger a Sub when the user clicks anywhere inside the Rectangle.
-
Mar 5th, 2007, 10:55 AM
#2
Re: Rectangle Click Event
2005:
vb Code:
Private g As Graphics
Private x As Int32 = 100
Private y As Int32 = 100
Private h As Int32 = 100
Private w As Int32 = 100
Private rect As New RectangleF(x, y, w, h)
Private Sub TableLayoutPanel1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseClick
If rect.Contains(e.Location) Then
MessageBox.Show("Inside")
End If
End Sub
Private Sub TableLayoutPanel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TableLayoutPanel1.Paint
g = e.Graphics
g.FillRectangle(Brushes.Red, rect)
End Sub
This also works if the box is ever relcoated (you would have to change the box's position obviously, but the event will adjust itself).
-
Mar 5th, 2007, 11:01 AM
#3
Re: Rectangle Click Event
At runtime:
vb Code:
Private g As Graphics
Private x As Int32 = 100
Private y As Int32 = 100
Private h As Int32 = 100
Private w As Int32 = 100
Private rect As New RectangleF(x, y, w, h)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler TableLayoutPanel1.MouseClick, AddressOf MyMouseClicks
End Sub
Private Sub MyMouseClicks(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If rect.Contains(e.Location) Then
MessageBox.Show("Inside")
End If
End Sub
Private Sub TableLayoutPanel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TableLayoutPanel1.Paint
g = e.Graphics
g.FillRectangle(Brushes.Red, rect)
End Sub
-
Mar 5th, 2007, 09:03 PM
#4
Addicted Member
Re: Rectangle Click Event
AFAIK, it is not recommended to declare a graphics variable in class without dispose it immediately when it's done.
If you need a rectangle in your TableLayoutPanel, I suggest you insert a panel into it.
Also you can change this:
vb Code:
Dim g As Graphics = TableLayoutPanel1.CreateGraphics()
Dim rect As RectangleF = New Rectangle(100, 100, 100, 400)
g.FillRectangle(Brushes.Red, rect)
g.Dispose()
With this:
vb Code:
Panel1.Backgroundcolor=Color.Red
Panel1.Parent=TableLayoutPanel1
Panel1.Size=New Size(100,100)
Panel1.Location=New Point(100,100)
Use Panel1_CLick event to make your code.
-
Mar 6th, 2007, 11:25 AM
#5
Re: Rectangle Click Event
AFAIK, it is not recommended to declare a graphics variable in class without dispose it immediately when it's done.
Why is that? Please give more information about this...
The reason i recommend drawing as opposed to panels is for overlapping. Say you have the following variation of the above:
vb Code:
Private g As Graphics
Private x As Int32 = 100
Private y As Int32 = 100
Private h As Int32 = 100
Private w As Int32 = 100
Private rect As New RectangleF(x, y, w, h)
Private rect2 As New RectangleF(x + 50, y + 50, w, h)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler TableLayoutPanel1.MouseClick, AddressOf MyMouseClicks
End Sub
Private Sub MyMouseClicks(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If rect.Contains(e.Location) Then MessageBox.Show("Inside rect")
If rect2.Contains(e.Location) Then MessageBox.Show("Inside rect2")
End Sub
Private Sub TableLayoutPanel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TableLayoutPanel1.Paint
g = e.Graphics
g.FillRectangle(Brushes.Red, rect)
g.FillRectangle(Brushes.Blue, rect2)
End Sub
If you click in the overlapped area, this slight change will handle it. If you used a panel, that would be a checking nightmare to determine exactly where every panel is and if it's in the area.
Panels were designed to house controls. Plain and simple. I would highly recommend against using panels for this.
-
Mar 6th, 2007, 09:41 PM
#6
Addicted Member
Re: Rectangle Click Event
I've read several articles on the net. And from what I read, graphics object should be disposed after used. So, when I playing with graphics, I put them within OnPaint, or Paint events or making my own sub to make sure that the object will be disposed after I use them. I'll try to search the link again. If I found them, I'll post here.
If you click in the overlapped area, this slight change will handle it. If you used a panel, that would be a checking nightmare to determine exactly where every panel is and if it's in the area.
Oops, I didn't think about overlapping before. I only assumed that those rectangle will be placed normally.
-
Mar 6th, 2007, 09:50 PM
#7
Re: Rectangle Click Event
You should not be creating a Graphics object at all. You should be drawing the rectangle in the TLP's Paint event using the Graphics object provided by the event.
As for detecting clicks, you've got a problem if you have controls in the TLP because the click will never get to the TLP when a child control is clicked. I'd suggest a common Click event handler for the TLP and all child controls. You can then do this:
vb Code:
If myRectangle.Contains(myTableLayoutPanel.PointToClient(Cursor.Position)) Then
'The user clicked within the rectangle.
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|