Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make image button

Tags:

c#

winforms

I was wondering how could I do this. I know I can use the button component but it has the little gray stuff around it when I give it a image. With image button how could I show another image for the hover effect

like image 425
Matthewj Avatar asked Sep 12 '11 23:09

Matthewj


People also ask

How do I make an image a button in CSS?

The default button in HTML can be changed to an image using CSS. The required button is selected using the respective CSS selector. The background property can then be set to include a background image and change the image type as required. The border of the button can also be removed to show only the image itself.

Can a button have an image?

Button with an ImageYou can include an <img> element inside your <button> element to display the image on the button. You can still add text to the button, resulting in a combination of image and text.

How do I make an image accessible in button?

The image button has an accessible name through the alt attribute.


2 Answers

You want to create a button with no border but displays different images when the user hovers over it with the mouse? Here's how you can do it:

  1. Add an ImageList control to your form at add two images, one for the button's normal appearance and one for when the mouse is hovering over.

  2. Add your button and set the following properties:
    FlatStyle = Flat
    FlatAppearance.BorderColor (and maybe MouseOverBackColor & MouseDownBackColor) to your form's background color
    ImageList = the ImageList you added to the form
    ImageIndex to the index value of your normal image

Code the MouseHover and MouseLeave events for the button like this:

// ImageList index value for the hover image.
private void button1_MouseHover(object sender, EventArgs e) => button1.ImageIndex = 1;

// ImageList index value for the normal image.
private void button1_MouseLeave(object sender, EventArgs e) => button1.ImageIndex = 0;

I believe that will give you the visual effect you're looking for.

like image 106
Jay Riggs Avatar answered Oct 21 '22 08:10

Jay Riggs


Small summary (Border, MouseDownBackColor, MouseOverBackColor)

FlatApperance

BorderColor = Black or what ever you want
BorderSize = can be set to 0
MouseDownBackColor = Transparent
MouseOverBackColor = Transparent

Text = none

For MouseDown:

// ImageList index value for the mouse down image.
private void button1_MouseDown(object sender, MouseEventArgs e) => button1.ImageIndex = 2;
like image 21
user2422690 Avatar answered Oct 21 '22 10:10

user2422690