2 Replies - 685 Views - Last Post: 09 April 2013 - 10:27 AM Rate Topic: -----

#1 MoonDPC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-July 11

Change button flatstyle of all forms?

Posted 08 April 2013 - 09:12 PM

Hello!
(With code) How do I change the flatstyle of all buttons of all forms (closed or opened)
Here's my current code:
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
            Dim types As Type() = myAssembly.GetTypes()
            For Each myType In types
                If myType.BaseType.FullName = "System.Windows.Forms.Form" Then
                    Dim frm As New Form
                    Dim formName As String = myType.Name
                    formName = Application.ProductName & "." & formName
                    frm = CType(Activator.CreateInstance(myType), Form)
                    For Each c As Control In frm.Controls
                        If TypeOf c Is Button Then
                            Dim btn As Button = CType(c, Button)
                            btn.FlatStyle = FlatStyle.Flat
                        End If
                    Next
                End If
            Next


The problems with my code are:
  • It doesn't change the properties of "btn"
  • It's messy

So, what's the problem with my code?

Is This A Good Question/Topic? 0
  • +

Replies To: Change button flatstyle of all forms?

#2 IronRazer  Icon User is online

  • D.I.C Addict

Reputation: 211
  • View blog
  • Posts: 719
  • Joined: 01-February 13

Re: Change button flatstyle of all forms?

Posted 09 April 2013 - 10:02 AM

Hi,
I am not sure why it is not working or what your trying to use it for but, if you want to change the buttons to flat style you could just set the buttons on all the opened forms and use a boolean to see if the buttons should be set to flat style when another form is opened and then you could set the button style before it is shown.
Public Class Form1
    Dim flat As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Change all buttons on the opened forms when button is clicked
        For Each frms As Form In Application.OpenForms
            ChangeButtons(frms)
        Next
        flat = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'open form 2
        Dim f2 As New Form2
        If flat = True Then ChangeButtons(f2)
        f2.Show()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'open form 3
        Dim f3 As New Form3
        If flat = True Then ChangeButtons(f3)
        f3.Show()
    End Sub

    Private Sub ChangeButtons(ByVal frm As Form)
        For Each ctrl As Control In frm.Controls
            If TypeOf ctrl Is Button Then
                DirectCast(ctrl, Button).FlatStyle = FlatStyle.Flat
            End If
        Next
    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#3 MoonDPC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-July 11

Re: Change button flatstyle of all forms?

Posted 09 April 2013 - 10:27 AM

View PostIronRazer, on 09 April 2013 - 10:02 AM, said:

Hi,
I am not sure why it is not working or what your trying to use it for but, if you want to change the buttons to flat style you could just set the buttons on all the opened forms and use a boolean to see if the buttons should be set to flat style when another form is opened and then you could set the button style before it is shown.
Public Class Form1
    Dim flat As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Change all buttons on the opened forms when button is clicked
        For Each frms As Form In Application.OpenForms
            ChangeButtons(frms)
        Next
        flat = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'open form 2
        Dim f2 As New Form2
        If flat = True Then ChangeButtons(f2)
        f2.Show()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'open form 3
        Dim f3 As New Form3
        If flat = True Then ChangeButtons(f3)
        f3.Show()
    End Sub

    Private Sub ChangeButtons(ByVal frm As Form)
        For Each ctrl As Control In frm.Controls
            If TypeOf ctrl Is Button Then
                DirectCast(ctrl, Button).FlatStyle = FlatStyle.Flat
            End If
        Next
    End Sub
End Class


I suppose I will have to do it that way.
The problem with that is, I have 20+ forms.
But that's fine, thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1