-1

I am using a checkbox as a toggle switch (ON-OFF Button) by changing its appearance to Button using Checkbox.Appearance property. When I run the VB.NET application, the Checkbox turns into a button only after the first click. Is it possible to change the appearance of the Checkbox to button as default? I also want the button to Popup when the user clicks "ON". I have tried changing the appearance using FlatStyle property, but it doesn't work.

I am using the following code:

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged

    CheckBox1.Appearance = System.Windows.Forms.Appearance.Button

    If CheckBox1.Checked = True Then
        CheckBox1.Text = "ON"
    ElseIf CheckBox1.Checked = False Then
        CheckBox1.Text = "OFF"
    End If 

End Sub
Brandon
  • 4,491
  • 6
  • 38
  • 59
Snehal
  • 1
  • 1
  • 5
  • 1
    Please provide the relevant code you have written so far. – Alex B. Nov 25 '16 at 10:56
  • Assuming you have set the Appearance property at designtime, then if it's not a button from the off, something else is very wrong with your code.Try putting a breakpoint on the click, and see if you can discover why it changes then. Or show us your code. – peterG Nov 25 '16 at 11:08
  • From the code you have posted, it's apparent why it changes on the click event. But does it look like a button in the designer? If not, why not? – peterG Nov 25 '16 at 11:28
  • It doesn't look like a button in the designer. It appears as a checkbox until the first click. – Snehal Nov 25 '16 at 11:45
  • Flipping the Appearance property in the designer should result in the corresponding change in appearance in the design view. If you are sure you have the Appearance property set to 'Button' in the designer, and it still looks like a checkbox then something is wrong somewhere. Perhaps you have another checkbox hidden away somewhere and you are looking at one and changing the proeprties of the other? – peterG Nov 25 '16 at 11:46
  • I changed the Appearance property in the designer and it worked. :) Thank you. – Snehal Nov 25 '16 at 11:54

1 Answers1

1

It is possible to select Appearance into the Load event of the form

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Me.CheckBox1.Appearance = Appearance.Button
End Sub
genespos
  • 3,211
  • 6
  • 38
  • 70
  • I have tried the same solution as mentioned by you and i am getting the output. But only after the first click on the checkbox. Initially, right after I run the application the checkbox does not change its appearance. Is there something else i need to add? – Snehal Nov 25 '16 at 11:16
  • It should work. Try with debug line by line to see if the code is executed But the code you showed is on another event! Be sure to have it into load event – genespos Nov 25 '16 at 11:47
  • @Snehal : What happens if you try to add `Me.CheckBox1.Refresh()` in the `Load` event too? – Visual Vincent Nov 25 '16 at 11:48
  • I changed the Appearance property in the Properties tab and it worked :) – Snehal Nov 25 '16 at 11:55