VB.NET Userform Hide/UnHide TableLayoutPanels
Hi All - I am trying to figure out a user control method on my form to hide/un-hide table layout panels. It seems like the way I am going is just very messy and there has to be a more elegant way of doing this. Right now I have 22 panels on my form. Under each panel I have a checkbox to show or hide it, then I am saving the checkbox state to my.settings. On form load, I am checking my.settings to see if it is "ON" or "OFF" then that will show or hide it. I have not really had too many "user" options in the past and not really sure how this could be handled better. Anyway, this is the mess I have now... Any help or ideas are greatly appreciated.
Code:
Private Sub CBST1_CheckStateChanged(sender As Object, e As EventArgs) Handles CBST1.CheckStateChanged
If CBST1.CheckState = CheckState.Checked Then
My.Settings.ST1 = "OFF"
My.Settings.Save()
TableLayoutPanel1.Visible = False
ElseIf CBST1.CheckState = CheckState.Unchecked Then
My.Settings.ST1 = "ON"
My.Settings.Save()
TableLayoutPanel1.Visible = False
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.ST1 = "OFF" Then
TableLayoutPanel1.Visible = False
CBST1.CheckState = CheckState.Unchecked
ElseIf My.Settings.ST1 = "ON" Then
TableLayoutPanel1.Visible = True
CBST1.CheckState = CheckState.Checked
End If
End Sub
Re: VB.NET Userform Hide/UnHide TableLayoutPanels
You should not be using the CheckState. That's only relevant when you want to support three states. If you only care about two states, which is usually the case, then you should be using the Checked property. Checked is type Boolean, so it will be either True or False. Coincidentally, that's the same type as the Visible property of your TableLayoutPanels, so you can simply handle the CheckedChanged event of each CheckBox and assign its Checked property directly to Visible property of the corresponding TLP.
That said, it can be even easier than that and can be done with zero code. For a start, your settings should also be type Boolean, not storing silly Strings like "ON" and "OFF". You'd have to test this to make sure it worked the way you'd expect but I think that you should be able to create on Boolean setting and bind it to both the Checked property of a CheckBox and the Visible property of the corresponding TLP. Checking and unchecking the box would then automatically update the setting and that would automatically update the TLP. Zero code required. You do the binding vis the Properties window and you can even create the settings as you do, or you can create the settings first.
Re: VB.NET Userform Hide/UnHide TableLayoutPanels
Thanks for that @jmcilhinney.... can you show me an example on setting that up?
Re: VB.NET Userform Hide/UnHide TableLayoutPanels
Assuming that ST1 is a boolean...
Code:
TableLayoutPanel1.Visible = My.Settings.ST1
CBST1.Checked = My.Settings.ST1
TableLayoutPanel2.Visible = My.Settings.ST2
CBST2.Checked = My.Settings.ST2
No if statements...
-tg
Re: VB.NET Userform Hide/UnHide TableLayoutPanels
So in the CheckedChanged event, should I still be doing an IF statement? IF checked then false, else true?
Code:
Private Sub CBST1_CheckedChanged(sender As Object, e As EventArgs) Handles CBST1.CheckedChanged
If CBST1.Checked = False Then
My.Settings.ST1 = True
Else
My.Settings.ST1 = False
End If
My.Settings.Save()
End Sub
Re: VB.NET Userform Hide/UnHide TableLayoutPanels
Wait... why are you setting your setting to false when the checkbox is true? ...
It should be as simple as this:
Code:
Private Sub CBST1_CheckedChanged(sender As Object, e As EventArgs) Handles CBST1.CheckedChanged
My.Settings.ST1 = CBST1.Checked
My.Settings.Save()
End Sub
That's it... when the checkBox is checked, you mark the setting as true... when they unselect it, you turn it to false.
And you'll probably want to toggle your panel too...
Code:
Private Sub CBST1_CheckedChanged(sender As Object, e As EventArgs) Handles CBST1.CheckedChanged
TableLayoutPanel1.Visible = CBST1.Checked
My.Settings.ST1 = CBST1.Checked
My.Settings.Save()
End Sub
Now, when you check the checkbox, the panel will be visible, your setting will be checked, etc...
-tg
EDIT - Ah ok, so I went back and looked... it makes sense now, the checkbox hides something not showing...sooo....
Instead, this:
Code:
Private Sub CBST1_CheckedChanged(sender As Object, e As EventArgs) Handles CBST1.CheckedChanged
TableLayoutPanel1.Visible = not CBST1.Checked 'If it's checked, it becomes false and hides the panel...
My.Settings.ST1 = CBST1.Checked
My.Settings.Save()
End Sub
And this in the load becomes this:
Code:
TableLayoutPanel1.Visible = not My.Settings.ST1
CBST1.Checked = My.Settings.ST1
TableLayoutPanel2.Visible = not My.Settings.ST2
CBST2.Checked = My.Settings.ST2
I missed it the first time around that the checkbox is hiding panels....
Bottom line, keep the checkbox in sync with your setting, and then "not" it to determine if the panel is shown or not.
Re: VB.NET Userform Hide/UnHide TableLayoutPanels
Ok, perfect. Thank you for this, it's making a lot more sense now. I was starting to go around in circles on this. Thanks a lot for the help. This is great learning.