Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, i need to make a button that is used to hid a label object or make appear again.
so i need the button when pressed to sink in like a switch and make the label disappear and when pressed again, become raised and make label appear.

any idea how to create a toggle button?
Posted
Updated 28-Apr-23 11:36am

You may use a checkbox for the purpose.
You may also use an ordinary (push) button, in the click event handler just toggle the Visible property of the label:
VB
myLabel.Visible = Not myLabel.Visible
 
Share this answer
 
v3
A toggle-Button is nothing more than a simple Button which
asks for a value (for example is this boolean true)
and after that a simple if(boolean == true) do this else do that



C#
boolean boo = false;

buttonPressed()
{
  if(boo == true)
  {
    boo = false;
    doSomething.Visible = true;
  }
  else
  {
    boo = true;
    doSomething.Visible = false;
  }
}
 
Share this answer
 
simply use this code

VB
checkBox.Appearance = System.Windows.Forms.Appearance.Button
 
Share this answer
 
Comments
VikrantDs 25-Nov-14 3:52am    
thanks
I got this from somewhere and it works like charm!!!
1. Right click project in VS and select 'Add' then 'User Control...'
2. Name your new file "Toggle.vb"
3. Paste the code below(please pay ATTENTION to step 4):

VB.NET
Imports System.Drawing
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class Toggle
Inherits System.Windows.Forms.UserControl

Private _checked As Boolean
Public Property Checked As Boolean
Get
Return _checked
End Get
Set(ByVal value As Boolean)
If Not _checked.Equals(value) Then
_checked = value
Me.OnCheckedChanged()
End If
End Set
End Property

Protected Overridable Sub OnCheckedChanged()
RaiseEvent CheckedChanged(Me, EventArgs.Empty)
End Sub

Public Event CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

Protected Overrides Sub OnMouseClick(e As MouseEventArgs)
Me.Checked = Not Me.Checked
Me.Invalidate()
MyBase.OnMouseClick(e)
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Me.OnPaintBackground(e)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias

Using path = New GraphicsPath()
Dim d = Padding.All
Dim r = Me.Height - 2 * d
path.AddArc(d, d, r, r, 90, 180)
path.AddArc(Me.Width - r - d, d, r, r, -90, 180)
path.CloseFigure()
e.Graphics.FillPath(If(Checked, Brushes.DarkGray, Brushes.LightGray), path)
r = Height - 1
Dim rect = If(Checked, New System.Drawing.Rectangle(Width - r - 1, 0, r, r), New System.Drawing.Rectangle(0, 0, r, r))
e.Graphics.FillEllipse(If(Checked, Brushes.Green, Brushes.LightSlateGray), rect)
End Using
End Sub
End Class


4. Switch to your form and drag your 'toggle' control from ToolBox to form(in some cases the toggle button does not appear on the ToolBox immediately. In that case to the following:
Go to Tools > Options > Windows Forms Designer > General
At the bottom of the list you'll find Toolbox > AutoToolboxPopulate which on a fresh install defaults to False. Set it true and then rebuild your solution. The user controls in you solution should be automatically added to the toolbox. You might have to reload the solution as well.

If your User Control is in a library you can add this to you Toolbox using
Toolbox -> right click -> Choose Items -> Browse
Select your assembly with the User Control.
5. Size & settings can be changed like standard control
6. Colors can be changed in OnPaint method of Toggle Class
C# Code:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Your_Project_Name
{
class Toggle : CheckBox
{
public Toggle()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Padding = new Padding(6);
}
protected override void OnPaint(PaintEventArgs e)
{
this.OnPaintBackground(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var path = new GraphicsPath())
{
var d = Padding.All;
var r = this.Height - 2 * d;
path.AddArc(d, d, r, r, 90, 180);
path.AddArc(this.Width - r - d, d, r, r, -90, 180);
path.CloseFigure();
e.Graphics.FillPath(Checked ? Brushes.DarkGray : Brushes.LightGray, path);
r = Height - 1;
var rect = Checked ? new System.Drawing.Rectangle(Width - r - 1, 0, r, r)
: new System.Drawing.Rectangle(0, 0, r, r);
e.Graphics.FillEllipse(Checked ? Brushes.Green : Brushes.LightSlateGray, rect);
}
}
}
}


below is the link to the original Source:
https://stackoverflow.com/questions/4793105/toggle-button-control[^]
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900