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.
LVL 4
nhagiaubungbuAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
Here is a check box, with Appearance set to Button, which doesn't have the tiny square:

      //
      // checkBox1
      //
      this.checkBox1.Appearance = System.Windows.Forms.Appearance.Button;
      this.checkBox1.Location = new System.Drawing.Point(40, 44);
      this.checkBox1.Name = "checkBox1";
      this.checkBox1.Size = new System.Drawing.Size(44, 36);
      this.checkBox1.TabIndex = 0;

Bob
0
 
2266180Commented:
Hi nhagiaubungbu,
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!
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
alaranCommented:
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.
0
 
alaranCommented:
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; }
      }
}
0
 
Bob LearnedCommented:
I fail to see what's wrong with a CheckBox?

Bob
0
 
nhagiaubungbuAuthor Commented:
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)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.