I know how to change the background color of a button or label in properties, but I need help figuring out how to get it to change when it is clicked.

Dani AI

Generated

If you are using ASP.NET Web Forms (tags suggest vb.net + asp.net), remember that a Button click posts back to the server. Setting a property in the server Click handler will only show after the page reloads. If you want the color to change immediately when the user clicks, do it on the client and optionally persist it on postback.

Immediate change without a full page refresh:

.clicked { background-color: #ffd54f !important; }
<asp:Button runat="server" ID="Btn1" Text="Click me"
    OnClientClick="this.className += ' clicked';" />

For a label-like element, either attach onclick or use a LinkButton for accessibility:

<asp:Label runat="server" ID="Lbl1" Text="Click me"
    onclick="this.className += ' clicked';" style="cursor:pointer;" />

Persist the color after postback by also adding the class on the server:

Protected Sub Btn1_Click(sender As Object, e As EventArgs) Handles Btn1.Click
    Btn1.CssClass = (Btn1.CssClass & " clicked").Trim()
End Sub

If you only want a momentary effect while the mouse is pressed, pure CSS works via the active state, e.g., .myBtn:active { background: #ffd54f; } applied to the rendered element’s class. See Button.OnClientClick and WebControl.CssClass on Microsoft Learn, and CSS :active on MDN for details.

Recommended Answers

All 9 Replies

in the form designer, add a button, then double click on it to go to the code, then type:

button1.BackColor = Color.Red //change the background color to red of button 1

Better to not refer to the control by name. In the Click handler do

Dim btn As Button = sender
sender.BackColor = Color.Red

or more simply

DirectCast(sender, Button).BackColor = Color.Red

you might even get away with

sender.BackColor = Color.Red

button1.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Red");

Button1.BackColor=Color.Blue
Changes the button's color to Blue
Label1.BackColor=Color.Blue
Changes the label's color to Blue
but note that the label should not be transparent

I think You are talking about the "hover/click/leave" the mouse change the color of it so,
If yes, then Yes You can
If so then msg me me I will give the codes for it so.

commented: Do not help via PM -3

If so then msg me me I will give the codes for it so.

Code should be posted in the thread for everyone to benefit from.

Ok,
I am Sorry.
Let me Add the code,
I will explain it with an example of Button,
Codes:

The Button You want the Change, Click on it go the Property Tab,
And then check top most of property tab, You will find some more tab with the icon, Go to the Event Tab.

Then, Find the Mouse Hover and Double click to enter the code.
Then Type this Code as for Example:

Button1.Forecolor = color.Blue

Then, again Same but now double Click on the Mouse Leave Button
And enter the Default setting Code:

Button1.Forecolor = color.Black

So what this do is: Whenever you are just taking your mouse on button/hover on the button then the font color will change to blue
else it will remain Black.

Thank You.
I guess that You got the solution.
<snip>

Your last post has a link to a website that promotes software theft. I have removed it. If you post that link again you will receive an infraction.

Reverend Jim:
That the link of Project File.
which I made in VB.net,
This link is not as for advertising...
Here I used to say that The Link which I post will know How the loigin forms I create so. In this login form I used the 1 user login and also one master login,
And In this login System, I made the use of the "Settings" in VB.Net,
And I rate this Level as "Interdimate" as for new user.
As to know settings in the starting of vb is hard even though I get this hard, but with the help of youtube and other friends, I know about it and made the new Login Form

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.