Triangular Shape Checkbox (ButtonStyle)

  • Thread starter Thread starter Jess
  • Start date Start date
I wand to create a diamond shape button that has consists of a left
right top and button segment (4 triangles) that toggles.
 
So does each segment toggle (i.e. four on/off boxes on each control)?

If so here's one method you could use. You could just inherit the
checkbox class and add a property that holds which position the
triangle is in (left, right, top, or bottom). Then, in it's overriden
paint method reference that property and draw the appropriate image
based on the value it returns (I would use an enum and a switch
statement) Then add four of this custom check box to your user
control, set the new position property, and drag them to where you
need them on the control. All that would be left is to create any
properties you might need for the control.

I hope that made since (it's getting pretty late here :-) )

Thanks,

Seth Rowe
 
I had tried this (decided I wanted a pie instead of a triangle) But
the problem is that although it displays the pie you still see the
outline of the square button. This is the code for 1 of the 4 buttons

For the Class

Public Class NewCheckBox2
Inherits CheckBox
Private CenterSquareColor As Color = _
Color.Red
Private CenterSquareColor2 As Color = _
Color.Blue
Protected Overrides Sub OnPaint( _
ByVal pevent As System.Windows.Forms.PaintEventArgs)
Dim CenterSquare As New Rectangle(-15, -40, 90, 90)
Me.FlatStyle = Windows.Forms.FlatStyle.Flat
'Me.Bounds
MyBase.OnPaint(pevent)
If Me.Checked Then
pevent.Graphics.FillPie(New SolidBrush(CenterSquareColor),
CenterSquare, 45, 90)
Else
pevent.Graphics.FillPie(New
SolidBrush(CenterSquareColor2), CenterSquare, Dim1, Dim2)
End If
End Sub
End Class



For the Form

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyCheckBox2 As New NewCheckBox2
With MyCheckBox2
.Text = "Custom Colored Check Box"
.Left = 125 ' OldCheckBox.Left
.Top = 100 ' OldCheckBox.Top + OldCheckBox.Height
.Size = New Size(60, 60) ' OldCheckBox.Size
.Appearance = Appearance.Button
.BackColor = Color.Transparent
.FlatStyle = FlatStyle.Flat
.ForeColor = Color.Transparent
End With
Controls.Add(MyCheckBox2)
End Sub
End Class
 
This sounds like a custom control would be better suited to the task.

I have outlined a very basic control below which you can expand upon to give
you exactly what you want.
For instance, you may like to add a Slices property to give you a collection
of PieSlices instead of having a fixed array of 4 PieSlices.

\\\
Public Class CheckPie
Inherits System.Windows.Forms.UserControl

Protected Overrides ReadOnly Property DefaultSize() As Size
Get
Return New Size(48, 48)
End Get
End Property

Private Slices() As PieSlice

Private Class PieSlice
Public Path As New Drawing2D.GraphicsPath
Public Checked As Boolean
Public Sub New()
Me.Path = New Drawing2D.GraphicsPath
End Sub
End Class

Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Slices = New PieSlice() {New PieSlice, New PieSlice, _
New PieSlice, New PieSlice}
Me.OnResize(EventArgs.Empty)
End Sub

Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
MyBase.OnResize(e)
Dim r As Rectangle = Me.ClientRectangle
r.Width -= 1
r.Height -= 1
For id As Int32 = 0 To 3
Slices(id).Path.Reset()
Slices(id).Path.AddPie(r, 45 + (id * 90), 90)
Next
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
If Slices.Length = 0 Then Return
For Each slice As PieSlice In Slices
If slice.Checked Then
e.Graphics.FillPath(Brushes.Red, slice.Path)
Else
e.Graphics.FillPath(Brushes.Blue, slice.Path)
End If
e.Graphics.DrawPath(Pens.Gray, slice.Path)
Next
End Sub

Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
MyBase.OnClick(e)
For Each slice As PieSlice In Slices
Dim pt As Point = Me.PointToClient(Me.MousePosition)
If slice.Path.IsVisible(pt.X, pt.Y) Then
slice.Checked = Not slice.Checked
Me.Invalidate(New Region(slice.Path))
End If
Next
End Sub

End Class
///
 
Try the code and got the following warning

Public Sub New()' in designer-generated type 'Project1.CheckPie'
should call InitializeComponent method.

Not sure what this means
 
InitializeComponent() is a method that contains all the designer
generated code that is used for initializing the control. To
demonstrate, create a form and set a few properties and maybe add a
control or two. Then in the solution explorer, click the button at top
that shows everything. Then expand the newly created form's node and
open the Form.Designer.vb file. In that code file should be the
InitializeComponent method, you might have to expand the #Region
sections first in order to find the sub though. In that method you
will see the code that is generated for each change you made in the
designer.

The reason you (normally) need do call the InitializeComponent method
first is that it responsible for instantiating the objects. If you
need to call InitializeComponent but don't, then every time you try to
reference a designer added component you will get a
NullReferenceException, since none of the objects were ever created.
However, if you handle all the instantiation/initialization somewhere
else in your code then you can completely ignore having to use the
InitializeComponent sub. For
compliance to the standards, or to get rid of the warning, you may
just want to write your initialization code in a method called
InitializeComponent. It's completely up to you.

Hope that helps.

Thanks,

Seth Rowe
 
It just means that you added a UserControl to the project which also adds a
Partial class and auto-generated code, whereas I added a Class so didn't get
any extras.

In Sub New() just replace
MyBase.New()
with
InitializeComponent()

I also put
Me.PointToClient(Me.MousePosition)
in OnClick() but it should have been
Me.PointToClient(Control.MousePosition)
 
Back
Top