Results 1 to 7 of 7

Thread: [RESOLVED] How to set a button background to the default

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2013
    Posts
    196

    Resolved [RESOLVED] How to set a button background to the default

    This should be easy, but I am having difficulty setting a button's background color back to the default state.
    I tried 4 combinations as shown below but Button1's background never matches Button2's background. How can I match it?
    Code:
    Public Class Form1
        Public Sub run()
            Dim clr As Color
            clr = Control.DefaultBackColor
            'clr = DefaultBackColor
            'clr = SystemColors.Control
            'clr = Color.FromArgb(240, 255, 255, 255)
            Button1.BackColor = clr
        End Sub
        Private Sub Quit_Button_Click(sender As Object, e As EventArgs) Handles Quit_Button.Click
            Application.Exit()
        End Sub
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            run()
        End Sub
    End Class

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,398

    Re: How to set a button background to the default

    What happens if you save the background color of the button before changing it the first time? Can you then set it back to the saved color and have it be correct?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2013
    Posts
    196

    Re: How to set a button background to the default

    Surprisingly saving the background color and then setting it to that doesn't work. Button1's background still doesn't match the original!
    Here is how I tried that:
    Code:
    Public Class Form1
        Public Sub run()
            Dim oldclr As Color
            oldclr = Button1.BackColor
            Button1.BackColor = oldclr
        End Sub
        Private Sub Quit_Button_Click(sender As Object, e As EventArgs) Handles Quit_Button.Click
            Application.Exit()
        End Sub
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            run()
        End Sub
    End Class

  4. #4
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How to set a button background to the default

    Code:
    Button1.UseVisualStyleBackColor = True
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  5. #5
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,398

    Re: How to set a button background to the default

    Comment out the statement:
    Button1.BackColor = oldclr
    and run the program and create and post a screen capture of the button on the form.

    Then uncomment that statement and re-run and post another screen capture of the same button.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: How to set a button background to the default

    It seems from your code the reason it doesn't change color is because it's already set to that color. You never set the backcolor to anything else.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2013
    Posts
    196

    Re: How to set a button background to the default

    Arve, adding
    Code:
    Button1.UseVisualStyleBackColor = True
    fixed the problem.

    Thanks!

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