locked
Multipage Control Hidden Pages RRS feed

  • Question

  • I have a userform with a Multipage control on it.  I want the ability to hide individual pages.  Is it possible to Hide individual pages without having to rebuild each hidden page when I need it?  I am hoping that I do not have to rebuild all the control via code upon unhiding the page.

     

    The idea here being that I want restrict the users access to certain tabs given their status.  User1 may have access to pages 1-3 where User2 may have access to all 5 tabs.  So I want to send the EXE to people have their User status dictate the number of pages they see on the form.  If that User status is upgraded, then I want to show/enable the pages which were once hidden/disabled.

     

    Thanks

     

    EM

    Wednesday, May 2, 2007 4:29 PM

Answers

  • Add a UserControl to your project for each page you want to display.

     

    Either:

    Drag and drop each control into the tab page of a tab strip.

    Or:

    Create a list of UserControl (or individual variables) with a reference to each page you created.

    Set the parent container of each of the controls as appropriate.

     

    It's more work to do it this way, but it's a bit more versatile: here's what you could do (the button controls which UserControl/page is visible by selecting a random page):

     

    Code Snippet

    Public Class Form1

    '

    ' Requires:

    ' 3 User Controls (UserContol1, UserContol2, UserControl3)

    ' Panel (Panel1)

    ' Button (Button1)

     

    ' Create a dictionary of pages

    Private Pages As Dictionary(Of Integer, UserControl)

    ' Randomizer for (TESTING)

    Private rnd As New Random

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' Create the pages

    Pages = New Dictionary(Of Integer, UserControl)

    Pages.Add(0, New UserControl1) ' Page0 is a UserControl

    Pages.Add(1, New UserControl2)

    Pages.Add(2, New UserControl3)

    Pages.Add(3, New UserControl1)

    '

    ' Set the parent (Needs a Panel Control)

    For Each ctl As UserControl In Pages.Values

    '

    ' Set the Parent/docking/visibility

    ctl.Parent = Panel1

    ctl.Dock = DockStyle.Fill

    ctl.Visible = False

    '

    ' Get a random color

    Dim r As Integer = rnd.Next(0, 255)

    Dim g As Integer = rnd.Next(0, 255)

    Dim b As Integer = rnd.Next(0, 255)

    '

    ' Set the panel background color (TESTING)

    ctl.BackColor = Color.FromArgb(255, r, g, b)

    Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' Select a random page

    Dim page As Integer = rnd.Next(0, 3)

    Button1.Text = page.ToString

    '

    ' Find this page and make it visible (bringToFront)

    If Pages.ContainsKey(page) Then

    Dim ctl As UserControl = Pages.Item(page)

    If ctl IsNot Nothing Then

    ctl.BringToFront()

    ctl.Visible = True

    End If

    End If

    End Sub

    End Class

     

    In each user control you can add your buttons, text, validation for those controls, etc. without having to add it to your main form. You still have access to each of those controls through the page (UserControl) it's on.

    Wednesday, May 2, 2007 10:15 PM

All replies

  • If you're talking about the TabControl then you can show/hide them by adding/removing them from the TabControl.TabPages collection.
    Wednesday, May 2, 2007 4:58 PM
  • I understand that you are talking about a Tabbed Control.

    I could not see a way to hide the page itself (some thing that would appear the same if we would remove the page), but you can hide its contents/controls. In the selectedIndexChanged event of the tab Control, you put this

    Code Snippet
    Me.TabControl1.TabPages.Item(TabControl1.SelectedIndex).Hide()

     

    Use .Show to reverse the action. That would simply hide all controls inside the page, but will keep the page as it is. That is the only solution I can think of now (But there may be a better one).

    Wednesday, May 2, 2007 5:02 PM
  • So adding and removing implies to me that I will have to rebuild each page if I remove it.

     

    Hiding is an option but it still renders the page visible when selected.  This may create some confusion for the user.  I could get around this by having all the controls hidden when the user selects the page and have a message on the page saying that the user currently does not have access to this page.  I would have to hide all controls on the page and then create a temporary textbox or label with the new message.

     

    I am shocked that you cannot hide the pages themself.  You can do it in VBA.

     

    Thanks

     

    EM

     

     

    Wednesday, May 2, 2007 5:32 PM
  • You can definetly remove the page from the Tab Control if you want, and that does not require rebuild. You can just use Me.TabControl1.TabPages.removeAt([Index]). That will totally remove the page from the Tab Control. You can remove by key too.

     

    In that case, next time you need that removed page you have to add all the controls to the page and then add the page to the Tab control.

    Wednesday, May 2, 2007 6:04 PM
  • >rebuild each page if I remove it.

     

    No you don't, just re-add it back to the collection.

    Wednesday, May 2, 2007 6:29 PM
  • Another option is to build each page as a user control, then load/show/hide each user control as appropriate. It also means you can encapsulate all the functionality for the controls on the user control within that user control. Your main 'container' page is less cluttered and easier to troubleshoot.

    Wednesday, May 2, 2007 7:46 PM
  • Can you clarify what you mean by build each page as a usercontrol?

     

    EM

    Wednesday, May 2, 2007 8:19 PM
  • Add a UserControl to your project for each page you want to display.

     

    Either:

    Drag and drop each control into the tab page of a tab strip.

    Or:

    Create a list of UserControl (or individual variables) with a reference to each page you created.

    Set the parent container of each of the controls as appropriate.

     

    It's more work to do it this way, but it's a bit more versatile: here's what you could do (the button controls which UserControl/page is visible by selecting a random page):

     

    Code Snippet

    Public Class Form1

    '

    ' Requires:

    ' 3 User Controls (UserContol1, UserContol2, UserControl3)

    ' Panel (Panel1)

    ' Button (Button1)

     

    ' Create a dictionary of pages

    Private Pages As Dictionary(Of Integer, UserControl)

    ' Randomizer for (TESTING)

    Private rnd As New Random

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' Create the pages

    Pages = New Dictionary(Of Integer, UserControl)

    Pages.Add(0, New UserControl1) ' Page0 is a UserControl

    Pages.Add(1, New UserControl2)

    Pages.Add(2, New UserControl3)

    Pages.Add(3, New UserControl1)

    '

    ' Set the parent (Needs a Panel Control)

    For Each ctl As UserControl In Pages.Values

    '

    ' Set the Parent/docking/visibility

    ctl.Parent = Panel1

    ctl.Dock = DockStyle.Fill

    ctl.Visible = False

    '

    ' Get a random color

    Dim r As Integer = rnd.Next(0, 255)

    Dim g As Integer = rnd.Next(0, 255)

    Dim b As Integer = rnd.Next(0, 255)

    '

    ' Set the panel background color (TESTING)

    ctl.BackColor = Color.FromArgb(255, r, g, b)

    Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' Select a random page

    Dim page As Integer = rnd.Next(0, 3)

    Button1.Text = page.ToString

    '

    ' Find this page and make it visible (bringToFront)

    If Pages.ContainsKey(page) Then

    Dim ctl As UserControl = Pages.Item(page)

    If ctl IsNot Nothing Then

    ctl.BringToFront()

    ctl.Visible = True

    End If

    End If

    End Sub

    End Class

     

    In each user control you can add your buttons, text, validation for those controls, etc. without having to add it to your main form. You still have access to each of those controls through the page (UserControl) it's on.

    Wednesday, May 2, 2007 10:15 PM