locked
How can you change the colour of a cell in a TableLayoutPanel with mouseover event

    Question

  • Hello Everyone,

    Please bear with me here. I have a two part Question.

    I have a Windows Forms application, where on one of the forms I have a TableLayoutPanel ( tlp1 )

    tlp1 has 96 Columns and only 1 Row.

    Question 1: is, IF tlp1 Background set to Transparent, is there

    any way to get each of the columns ( one at a time ) to change colour ( LIMEGREEN ) as the mouse

    moves over them? Then change back to Transparent as the mouse moves off to the next cell?

    Question 2: If Q 1 can be done, Can someone give me an example in code?

    Thank you in advance

    Wednesday, June 25, 2008 3:30 PM

Answers

  • I don't think you can change the backcolor of the cells of a table layout panel. What you can do is add a panel to each cell and then change the backcolor of it. Try something like this.

    Code Snippet

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    For x As Integer = 0 To 95

    Dim ctr As New Panel

    ctr.Dock = DockStyle.Fill

    ctr.BackColor = Color.Transparent

    AddHandler ctr.MouseEnter, AddressOf PanelMouseEnter

    AddHandler ctr.MouseLeave, AddressOf PanelMouseLeave

    Me.TableLayoutPanel1.Controls.Add(ctr, x, 0)

    Next

    End Sub

     

    Private Sub PanelMouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)

    sender.backcolor = Color.Yellow

    End Sub

     

    Private Sub PanelMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)

    sender.backcolor = Color.Transparent

    End Sub

     

     

    Hope this helps.

     

    Wednesday, June 25, 2008 5:02 PM
  • You could create a customised tablelayoutpanel as follows and use it instead of the standard one:

     

    Public Class myTLP

    Inherits TableLayoutPanel

    Public Sub New()

    DoubleBuffered = True

    End Sub

    Protected Overrides Sub OnCellPaint(ByVal e As System.Windows.Forms.TableLayoutCellPaintEventArgs)

    MyBase.OnCellPaint(e)

    If e.CellBounds.Contains(PointToClient(Cursor.Position)) Then

    e.Graphics.FillRectangle(Brushes.LimeGreen, e.CellBounds)

    End If

    End Sub

    Private Sub myTLP_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

    Invalidate()

    End Sub

    End Class

     

    Thursday, June 26, 2008 9:04 PM

All replies

  • I don't think you can change the backcolor of the cells of a table layout panel. What you can do is add a panel to each cell and then change the backcolor of it. Try something like this.

    Code Snippet

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    For x As Integer = 0 To 95

    Dim ctr As New Panel

    ctr.Dock = DockStyle.Fill

    ctr.BackColor = Color.Transparent

    AddHandler ctr.MouseEnter, AddressOf PanelMouseEnter

    AddHandler ctr.MouseLeave, AddressOf PanelMouseLeave

    Me.TableLayoutPanel1.Controls.Add(ctr, x, 0)

    Next

    End Sub

     

    Private Sub PanelMouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)

    sender.backcolor = Color.Yellow

    End Sub

     

    Private Sub PanelMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)

    sender.backcolor = Color.Transparent

    End Sub

     

     

    Hope this helps.

     

    Wednesday, June 25, 2008 5:02 PM
  •  

    Why are all my b's been turned into ******? Oh, well, change all those ***** to b.

    Wednesday, June 25, 2008 5:05 PM
  • I was beginning to think that was me!  I'm glad someone else sees that too!

     

    Chris

     

    Thursday, June 26, 2008 5:54 PM
  • You could create a customised tablelayoutpanel as follows and use it instead of the standard one:

     

    Public Class myTLP

    Inherits TableLayoutPanel

    Public Sub New()

    DoubleBuffered = True

    End Sub

    Protected Overrides Sub OnCellPaint(ByVal e As System.Windows.Forms.TableLayoutCellPaintEventArgs)

    MyBase.OnCellPaint(e)

    If e.CellBounds.Contains(PointToClient(Cursor.Position)) Then

    e.Graphics.FillRectangle(Brushes.LimeGreen, e.CellBounds)

    End If

    End Sub

    Private Sub myTLP_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

    Invalidate()

    End Sub

    End Class

     

    Thursday, June 26, 2008 9:04 PM