Results 1 to 3 of 3

Thread: [RESOLVED] Change BackColor of empty panels in FlowLayoutPanel

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    13

    Resolved [RESOLVED] Change BackColor of empty panels in FlowLayoutPanel

    Hello All
    It's been years since I've programmed VB and it seems I've forgotten more than I remember. I'm just working on a simple calendar (based on someone else's code) and I'm trying to change the BackColor of the empty panels in a FlowLayoutPanel. If there is no label text then the panel would be a different color. It's not really important to the calendar, but it's bugging me that I can't figure it out myself(even though I'm sure it's simple). Thanks in advance.
    Code:
        Private Sub GenerateDayPanel(ByVal totalDays As Integer)
            flDays.Controls.Clear()
            listFlDay.Clear()
            Dim pflowh As Integer
            pflowh = pnlFlow.Height
            For i As Integer = 1 To totalDays
                Dim fl As New FlowLayoutPanel
                fl.Name = $"flDay{i}"
                fl.Width = Label1.Width
                fl.Height = pflowh / 6
                fl.Margin = New Padding(0)
                fl.BackColor = Color.DimGray
                fl.BorderStyle = BorderStyle.FixedSingle
                fl.Cursor = Cursors.Hand
                fl.AutoScroll = True
                '  AddHandler fl.Click, AddressOf AddNewAppointment
                flDays.Controls.Add(fl)
                listFlDay.Add(fl)
            Next
        End Sub
    Code:
     Private Sub AddLabelDayToFlDay(ByVal startDayAtFlNumber As Integer, ByVal totalDaysInMonth As Integer)
            For Each fl As FlowLayoutPanel In listFlDay
                fl.Controls.Clear()
                fl.Tag = 0
    
                fl.BackColor = Color.White
    
            Next
            Dim myMargin = New Padding
            myMargin.All = 0
    
    
    
            For i As Integer = 1 To totalDaysInMonth
                Dim lbl As New Label
                lbl.Name = $"lblDay{i}"
                lbl.AutoSize = False
                lbl.TextAlign = ContentAlignment.TopLeft
                lbl.Margin = myMargin
                lbl.Text = i
                lbl.Font = New Font("Microsoft Sans Serif", 18)
                listFlDay((i - 1) + (startDayAtFlNumber - 1)).Tag = i
                listFlDay((i - 1) + (startDayAtFlNumber - 1)).Controls.Add(lbl)
    
                If New Date(currentDate.Year, currentDate.Month, i) = Date.Today Then
                    listFlDay((i - 1) + (startDayAtFlNumber - 1)).BackColor = Color.Aqua
                End If
    
            Next
    
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Change BackColor of empty panels in FlowLayoutPanel

    Quote Originally Posted by Clutch001 View Post
    I'm trying to change the BackColor of the empty panels in a FlowLayoutPanel. If there is no label text then the panel would be a different color.
    So all Panels contain a Label and an "empty" Panel is one where the Text of that Label is an empty String? In that case:
    vb.net Code:
    1. Dim emptyPanels = myFlowLayoutPanel.Controls.
    2.                                     OfType(Of Panel)().
    3.                                     Where(Function(p) p.Controls.
    4.                                                         OfType(Of Label)().
    5.                                                         First().
    6.                                                         Text = String.Empty).
    7.                                     ToArray()
    8.  
    9. For Each p In emptyPanels
    10.     'Do as required.
    11. Next
    If you wanted to be able to change the colour both ways:
    vb.net Code:
    1. For Each p In myFlowLayoutPanel.Controls.OfType(Of Panel)()
    2.     If p.Controls.OfType(Of Label)().First().Text = String.Empty Then
    3.         'The Panel is "empty".
    4.     Else
    5.         'The Panel is not "empty".
    6.     End If
    7. Next
    Note that, if the FlowLayoutPanel contains only Panel controls then OfType would work but Cast would be more appropriate. As provided, the code will examine the first Label control in each Panel. It could be adjusted to work slightly differently if required.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    13

    Re: Change BackColor of empty panels in FlowLayoutPanel

    Thanks so much for replying. I kept trying to use your code but just kept getting sequence contains no elements. But it's OK I figured out a quick "cheap" way to do it. I set the initial empty panel back color , and then changed the created label color's back color, and then changed the panels back color .
    Code:
       For Each fl As FlowLayoutPanel In listFlDay
                fl.Controls.Clear()
                fl.Tag = 0
    
                fl.BackColor = Color.LightGray
    
            Next
    
    
      lbl.BackColor = Color.White
                If lbl.BackColor = Color.White Then
                    listFlDay((i - 1) + (startDayAtFlNumber - 1)).BackColor = Color.White
                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
  •  



Click Here to Expand Forum to Full Width