切换按钮控件

时间:2011-01-25 11:45:48

标签: vb.net

我可以将按钮控件更改为切换按钮吗?

有没有简单的方法来更改按钮属性以使其切换按钮?

3 个答案:

答案 0 :(得分:6)

根据OSIX上的this post,您需要做的就是使用CheckBox,但将其外观设置为Button

在代码中:

CheckBox checkBox1 = new System.Windows.Forms.CheckBox();
checkBox1.Appearance = System.Windows.Forms.Appearance.Button;

(C#代码,但你看它是如何工作的。)

但您可以从设计器的“属性”对话框中执行此操作。

答案 1 :(得分:1)

要将复选框更改为简单闩锁开/关按钮

enter image description here

myCheckBox.Appearance = System.Windows.Forms.Appearance.Button

添加自定义切换(滑动)开/关开关

enter image description here

  1. 在VS中右键单击项目,然后依次选择“添加”和“用户控制...”

    enter image description here

  2. 将新文件命名为“ Toggle.vb”

  3. 粘贴下面的代码
  4. 切换到表单并将“切换”控件从工具箱拖动到表单 enter image description here
  5. 大小和设置可以像标准控件一样更改
  6. 可以使用切换类的OnPaint方法更改颜色

VB.net

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

C#

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);
            }
        }
    }
}

这些年来,这些代码来自多个来源,并且有一些细微的调整。看来它以各种形式存在于不同的站点上,因此尚不清楚应归谁

所有代码均已在Visual Studio 2017中进行了测试

答案 2 :(得分:0)

如果我理解正确,您可以使用标志来实现此功能。例如: bool isClicked = false;

并在单击的事件中放入以下代码以反转bool: isClicked =!isClicked;