nhagiaubungbu
asked on
C# - Toggle button with image, no text - need help, please!!!!
Hi guys,
I want to have a toggle button on my form. I already knew about the way to use a ChechBox or a RadioButton with the Appearance as a Button, but the problem is that I want my toggle button to display an image and there is no text on it.
I want to have a toggle button on my form. I already knew about the way to use a ChechBox or a RadioButton with the Appearance as a Button, but the problem is that I want my toggle button to display an image and there is no text on it.
Or you could make your own button type deriving from Button. Then override OnClick wher you can change the image according to an internal state variable you define.
Something like this could work:
public class MyButton : Button
{
private bool _toggle = false;
private Image _img1;
private Image _img2;
protected override void OnClick(EventArgs e)
{
_toggle = !_toggle;
this.Image = _toggle ? img1 : img2;
base.OnClick(e);
}
public Image Img1
{
get { return _img1; }
set { _img1 = value; }
}
public Image Img2
{
get { return _img2; }
set { _img2 = value; }
}
}
public class MyButton : Button
{
private bool _toggle = false;
private Image _img1;
private Image _img2;
protected override void OnClick(EventArgs e)
{
_toggle = !_toggle;
this.Image = _toggle ? img1 : img2;
base.OnClick(e);
}
public Image Img1
{
get { return _img1; }
set { _img1 = value; }
}
public Image Img2
{
get { return _img2; }
set { _img2 = value; }
}
}
I fail to see what's wrong with a CheckBox?
Bob
Bob
ASKER
I don't want any text appearing on my button, in that case, a checkbox having appearance as a button will not be shown (it is shown as a tiny square in designer)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you could simulate it using a simple button:
- at first, assign an image to it.
then on teh onclick event, change the image to something else according to a condition.
something like:
if clicked = 1 then load image1
else load image2
clicked:=(clicked+1) % 2;
Cheers!