Meilleur auteur de réponses
changer de couleur checkbox

Question
-
Réponses
-
Quel est l'objectif recherché ici?
Est-ce que le bouton slider doit fonctionner comme une checkbox?
Si c'est le cas alors il faut gérer le click sur le bouton avec l'élément graphique.Voici un exemple tout simple:
Et le code:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click If _Checked = False Then _Checked = True PictureBox1.Image = My.Resources.SlideDroite Else _Checked = False PictureBox1.Image = My.Resources.SlideGauche End If End Sub Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick _Checked = False PictureBox1.Image = My.Resources.SlideInBetween 'Smiley!!!! End Sub
Voila....
Marquer comme Réponse si cela convient.... :)
Cyrille Precetti
- Marqué comme réponse zorro591 jeudi 10 décembre 2015 08:13
Toutes les réponses
-
-
je utilise Windows form1.vb
Voici un exemple ce que je voudrais faire : couleur checkbox
es ce que c'est possible faire en vb.net? si oui comment?
- Modifié zorro591 mardi 8 décembre 2015 12:24
-
Il y a peut être des toolkits qui implémentent des modifications du contrôle CheckBox; regardez pour ToggleButtonExtender par exemple.
Mais plus simplement il y a moyen d'utiliser une image.
Voici ce que cela donne:
Les images sont 17x17 pixels, et 15x15 qui est mieux, inclus dans les ressources.
Le code est dans l'événement Paint:
Private Sub CheckBox1_Paint(sender As Object, e As PaintEventArgs) Handles CheckBox1.Paint If CheckBox1.Checked = True Then e.Graphics.DrawImageUnscaled(My.Resources.Checked, 0, 0) Else e.Graphics.DrawImageUnscaled(My.Resources.UnChecked, 0, 0) End If End Sub
Private Sub CheckBox2_Paint(sender As Object, e As PaintEventArgs) Handles CheckBox2.Paint If CheckBox2.Checked = True Then e.Graphics.DrawImageUnscaled(My.Resources.Checked2, 0, 0) Else e.Graphics.DrawImageUnscaled(My.Resources.UnChecked2, 0, 0) End If End Sub
Cyrille Precetti
- Modifié Cyrille Précetti mardi 8 décembre 2015 22:53
-
-
Quel est l'objectif recherché ici?
Est-ce que le bouton slider doit fonctionner comme une checkbox?
Si c'est le cas alors il faut gérer le click sur le bouton avec l'élément graphique.Voici un exemple tout simple:
Et le code:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click If _Checked = False Then _Checked = True PictureBox1.Image = My.Resources.SlideDroite Else _Checked = False PictureBox1.Image = My.Resources.SlideGauche End If End Sub Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick _Checked = False PictureBox1.Image = My.Resources.SlideInBetween 'Smiley!!!! End Sub
Voila....
Marquer comme Réponse si cela convient.... :)
Cyrille Precetti
- Marqué comme réponse zorro591 jeudi 10 décembre 2015 08:13
-
Bonjour, Merci Cyrille Precetti pour la réponde mais j'ai trouver la solution a mon problème avec nombre de recherche.
voici en image :
il faudra créer une classe :
Imports System.Drawing.Drawing2D Public Class ButtonOnOff Inherits Control Event CheckedChanged(ByVal sender As Object) Private _checked As Boolean Public Property Checked() As Boolean Get Return _checked End Get Set(ByVal value As Boolean) _checked = value Invalidate() End Set End Property Sub New() SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or _ ControlStyles.UserPaint Or ControlStyles.ResizeRedraw, True) Size = New Size(40, 17) End Sub Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(e) Dim G As Graphics = e.Graphics G.SmoothingMode = SmoothingMode.HighQuality G.Clear(Parent.BackColor) Dim slope As Integer = (Height - 1) / 2 Height = 18 Dim mainRect As New Rectangle(0, 0, Width - 1, Height - 1) Dim mainPath As GraphicsPath = RoundRect(mainRect, slope) If _checked Then Dim bgBrush As New LinearGradientBrush(mainRect, Color.FromArgb(10, 30, 50), Color.FromArgb(5, 80, 140), 90.0F) G.FillPath(bgBrush, mainPath) Dim switchRect As New Rectangle(Width - 14, 3, 10, 10) Dim switchBrush As New LinearGradientBrush(switchRect, Color.FromArgb(100, 220, 250), Color.FromArgb(15, 150, 220), 90.0F) G.FillEllipse(switchBrush, switchRect) Dim textY As Integer = ((Height - 1) / 2) - (G.MeasureString("On", New Font("Arial", 8)).Height / 2) + 1 G.DrawString("On", New Font("Arial", 8), New SolidBrush(Color.FromArgb(180, 180, 180)), New Point(5, textY)) G.DrawPath(New Pen(Color.FromArgb(5, 80, 140)), mainPath) Else Dim bgBrush As New LinearGradientBrush(mainRect, Color.FromArgb(40, 40, 40), Color.FromArgb(80, 80, 80), 90.0F) G.FillPath(bgBrush, mainPath) Dim switchRect As New Rectangle(3, 3, 10, 10) Dim switchBrush As New LinearGradientBrush(switchRect, Color.FromArgb(150, 150, 150), Color.FromArgb(120, 120, 120), 90.0F) G.FillEllipse(switchBrush, switchRect) Dim textY As Integer = ((Height - 1) / 2) - (G.MeasureString("Off", New Font("Arial", 8)).Height / 2) + 1 G.DrawString("Off", New Font("Arial", 8), New SolidBrush(Color.FromArgb(180, 180, 180)), New Point(15, textY)) G.DrawPath(New Pen(Color.FromArgb(80, 80, 80)), mainPath) End If End Sub Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs) MyBase.OnMouseDown(e) If _checked Then _checked = False Else _checked = True End If RaiseEvent CheckedChanged(Me) Invalidate() End Sub Private Function RoundRect(Rect As Rectangle, slope As Integer) As GraphicsPath Dim gp As GraphicsPath = New GraphicsPath() Dim arcWidth As Integer = slope * 2 gp.AddArc(New Rectangle(rect.X, rect.Y, arcWidth, arcWidth), -180, 90) gp.AddArc(New Rectangle(rect.Width - arcWidth + rect.X, rect.Y, arcWidth, arcWidth), -90, 90) gp.AddArc(New Rectangle(rect.Width - arcWidth + rect.X, rect.Height - arcWidth + rect.Y, arcWidth, arcWidth), 0, 90) gp.AddArc(New Rectangle(rect.X, rect.Height - arcWidth + rect.Y, arcWidth, arcWidth), 90, 90) gp.CloseAllFigures() Return gp End Function End Class
Pour choisir une couleur de ta form1 :
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.BackColor = Color.FromArgb(128, 128, 128) ToolTip1.SetToolTip(ButtonOnOff1, "Cliquer ici pour choisir le mode ON ou OFF") ToolTip1.IsBalloon = True End Sub
cordialement,