Results 1 to 7 of 7

Thread: VB.NET Userform Hide/UnHide TableLayoutPanels

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: VB.NET Userform Hide/UnHide TableLayoutPanels

    Thanks for that @jmcilhinney.... can you show me an example on setting that up?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    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
    Last edited by mikeg71; Apr 26th, 2024 at 01:22 PM.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width