[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# - Toggle button with image, no text - need help, please!!!!

Posted on 2006-05-19
10
Medium Priority
?
2,910 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:nhagiaubungbu
7 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 16716089
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
 
LVL 4

Expert Comment

by:alaran
ID: 16716481
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:alaran
ID: 16716495
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16723155
I fail to see what's wrong with a CheckBox?

Bob
0
 
LVL 4

Author Comment

by:nhagiaubungbu
ID: 16732775
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16760516
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question