Results 1 to 7 of 7

Thread: Rectangle Click Event

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Rectangle Click Event

    2005:
    vb Code:
    1. Private g As Graphics
    2.     Private x As Int32 = 100
    3.     Private y As Int32 = 100
    4.     Private h As Int32 = 100
    5.     Private w As Int32 = 100
    6.     Private rect As New RectangleF(x, y, w, h)
    7.  
    8.     Private Sub TableLayoutPanel1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseClick
    9.         If rect.Contains(e.Location) Then
    10.             MessageBox.Show("Inside")
    11.         End If
    12.     End Sub
    13.     Private Sub TableLayoutPanel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TableLayoutPanel1.Paint
    14.         g = e.Graphics
    15.         g.FillRectangle(Brushes.Red, rect)
    16.     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).

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Rectangle Click Event

    At runtime:
    vb Code:
    1. Private g As Graphics
    2.     Private x As Int32 = 100
    3.     Private y As Int32 = 100
    4.     Private h As Int32 = 100
    5.     Private w As Int32 = 100
    6.     Private rect As New RectangleF(x, y, w, h)
    7.  
    8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9.         AddHandler TableLayoutPanel1.MouseClick, AddressOf MyMouseClicks
    10.     End Sub
    11.     Private Sub MyMouseClicks(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    12.         If rect.Contains(e.Location) Then
    13.             MessageBox.Show("Inside")
    14.         End If
    15.     End Sub
    16.     Private Sub TableLayoutPanel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TableLayoutPanel1.Paint
    17.         g = e.Graphics
    18.         g.FillRectangle(Brushes.Red, rect)
    19.     End Sub

  4. #4
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    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:
    1. Dim g As Graphics = TableLayoutPanel1.CreateGraphics()
    2.         Dim rect As RectangleF = New Rectangle(100, 100, 100, 400)
    3.         g.FillRectangle(Brushes.Red, rect)
    4.         g.Dispose()

    With this:
    vb Code:
    1. Panel1.Backgroundcolor=Color.Red
    2. Panel1.Parent=TableLayoutPanel1
    3. Panel1.Size=New Size(100,100)
    4. Panel1.Location=New Point(100,100)

    Use Panel1_CLick event to make your code.

  5. #5
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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:
    1. Private g As Graphics
    2.     Private x As Int32 = 100
    3.     Private y As Int32 = 100
    4.     Private h As Int32 = 100
    5.     Private w As Int32 = 100
    6.     Private rect As New RectangleF(x, y, w, h)
    7.     Private rect2 As New RectangleF(x + 50, y + 50, w, h)
    8.  
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         AddHandler TableLayoutPanel1.MouseClick, AddressOf MyMouseClicks
    11.     End Sub
    12.     Private Sub MyMouseClicks(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    13.         If rect.Contains(e.Location) Then MessageBox.Show("Inside rect")
    14.         If rect2.Contains(e.Location) Then MessageBox.Show("Inside rect2")
    15.     End Sub
    16.     Private Sub TableLayoutPanel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TableLayoutPanel1.Paint
    17.         g = e.Graphics
    18.         g.FillRectangle(Brushes.Red, rect)
    19.         g.FillRectangle(Brushes.Blue, rect2)
    20.     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.

  6. #6
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. If myRectangle.Contains(myTableLayoutPanel.PointToClient(Cursor.Position)) Then
    2.     'The user clicked within the rectangle.
    3. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width