Results 1 to 3 of 3
  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005

    How to check if a property exists in a control.

    I am working on a "theme" system for my app(basic...just change back/fore colours, etc...) and would like to offer the ability to change the FlatStyle. However I hit a build error when iterating through the controls since FlatStyle isn't available to all controls. I was able to use "typeof" = "button" to add the FlatStyle to the buttons but I would rather just check if the property "FlatStyle" was available to the control instead.

    VB Code:
    1. Private Sub setTheme(ByRef frmObj as Form)
    2.         For i As Integer = 0 To frmObj.Controls.Count - 1
    3.             If frmObj.Controls.Item(i).Controls.Count <> 0 Then
    4.                 For ind As Integer = 0 To frmObj.Controls.Item(i).Controls.Count - 1
    5.                     frmObj.Controls.Item(i).Controls.Item(ind).ForeColor() = Color.FromName(themeForeColour)
    6.                     frmObj.Controls.Item(i).Controls.Item(ind).BackColor() = Color.FromName(themeBackColour)
    7.                 Next
    8.             End If
    9.             frmObj.ForeColor() = Color.FromName(themeForeColour)
    10.             frmObj.BackColor() = Color.FromName(themeBackColour)
    11.             frmObj.Opacity = themeOpacity
    12.             frmObj.Controls.Item(i).ForeColor = Color.FromName(themeForeColour)
    13.             frmObj.Controls.Item(i).BackColor = Color.FromName(themeBackColour)
    14.             If TypeOf frmObj.Controls.Item(i) Is Button Then
    15.                 Dim thisButton As Button = frmObj.Controls.Item(i)
    16.                 Select Case themeFlatStyle.ToLower
    17.                     Case "flat"
    18.                         thisButton.FlatStyle = 0
    19.                     Case "popup"
    20.                         thisButton.FlatStyle = 1
    21.                     Case "standard"
    22.                         thisButton.FlatStyle = 2
    23.                     Case Else
    24.                         thisButton.FlatStyle = 3
    25.                 End Select
    26.             End If
    27.         Next i
    28. End Sub

    The variables prefixed with "theme" above are 'global' variables that are loaded when the app starts or the theme is changed by the user on an options page.

    Hopefully you will be able to help....or give me a better way to implement "themes". Thanks.

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2005

    Re: How to check if a property exists in a control.

    In case anyone is trying to use the above code and hitting problems with the "opacity" property (doubt it...but just in case) themeOpacity is set, elsewhere, by me as
    themeOpacity = Double.Parse(themeOpacity / 100)
    since I allow the user to scroll the opacity from 25 to 100.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: How to check if a property exists in a control.

    VB Code:
    1. For Each ctl As Control In Me.Controls
    2.             If ctl.GetType().GetProperty("FlatStyle") Is Nothing Then
    3.                 MessageBox.Show(ctl.Name & " does not have a FlatStyle property.")
    4.             Else
    5.                 MessageBox.Show(ctl.Name & " has a FlatStyle property.")
    6.             End If
    7.         Next
    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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.