locked
Button color RRS feed

  • Question

  • I am using Visual Studio 2019 programming in Visual Basic and would like to change the color of a button when it is pushed.

    The code is Button1.Background = Brush.

    This is the best that I can find, if you have a better set of commands I would like to use them.

    Wednesday, September 30, 2020 9:23 PM

All replies

  • Hi

    Here is an example.  One of many different ways to do it.This version needs a Button1 on Form1. It sets the initial button text in the Form load event and then each time the button is clicked it will toggle the background colour.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Button1.Text = "NO PRESS"
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		With Button1
    			Select Case .Text
    				Case "NO PRESS"
    					.Text = "CLICKED"
    					.BackColor = Color.Pink
    				Case Else
    					.Text = "NO PRESS"
    					.BackColor = Color.FromKnownColor(KnownColor.ControlLight)
    			End Select
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, September 30, 2020 9:43 PM
  • XAML

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:WpfApp2"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="Toggle Button color"
        Width="306.849"
        Height="194.178"
        WindowStartupLocation="CenterScreen"
        mc:Ignorable="d">
        <Grid>
            <Button x:Name="ChangeColorsButton"
                Width="235"
                Height="60"
                Margin="35,48,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Content="Button" />
    
        </Grid>
    </Window>

    Code behind

    Class MainWindow
        Private Sub ChangeColorsButton_Click(sender As Object, e As RoutedEventArgs) _
            Handles ChangeColorsButton.Click
    
            Dim btn As Button = TryCast(sender, Button)
            btn.Background = If(btn.Background Is Brushes.Red,
                                CType((New BrushConverter()).ConvertFrom("#FFDDDDDD"),
                                      SolidColorBrush), Brushes.Red)
    
        End Sub
    End Class


    If it was WinForms

    Private Sub Button1_Click(sender As Object, e As EventArgs) _
        Handles Button1.Click
    
        CType(sender, Button).BackColor = If(CType(sender, Button).BackColor = SystemColors.Control,
                                             Color.White,
                                             SystemColors.Control)
    
    
    End Sub

    Another thing, when working with WPF it's wise to always look for a code path that does not have to name controls. Naming controls is a Windows Form thing while with WPF there are Triggers and RoutedCommand/CommandBinding.

    Check out the following project. Make sure to review the code in Application.xaml which is where the magic is for changing colors on a button.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, September 30, 2020 11:05 PM
  • Do you want to change its colour during mouse-click operation only?

    Note that there is a ToggleButton control, which looks like a button, but acts like a checkbox.

    Thursday, October 1, 2020 8:17 AM
  • Hello,

    How are you doing with the task?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 1, 2020 5:17 PM
  • Settings button to change colors is the best solution
    Thursday, October 1, 2020 8:52 PM
  • Settings button to change colors is the best solution
    That is way to vague of a recommendation.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 1, 2020 9:20 PM
  • This may be what you want.

    Public Class Form1
    
        Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
            Button1.BackColor = Color.Green
        End Sub
    
        Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
            Button1.BackColor = Color.LightGray
        End Sub
    
    End Class

    Saturday, October 3, 2020 11:36 AM