Results 1 to 8 of 8

Thread: Display buttons dynamically

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Display buttons dynamically

    Hi,

    I have a form (FrmMain), on form load I'll have like 7 buttons. Now different users have access to different buttons;

    E.g.,

    User1 - Can ONLY access Btn1 and Btn6
    User2 - Can ONLY access Btn1 , Btn2, and Btn7

    Now on my form, the buttons are displayed as;

    Btn1 Btn2 Btn3 Btn4 Btn5 Btn6 Btn7

    Now; if i use the logic where when user1 logs on to make the other buttons invisible/disabled only to view Btn1 and Btn6

    I have;

    Btn1 then big space then Btn6

    This doesnt look nice on my form; so regardless if they are ONLY 2 buttons the space between should be similar to a user with 3 , 4 or even 7 buttons (standard).

    Any help how can i tackle this problem?

    Thanks

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Display buttons dynamically

    What code did you use to make some of them invisible, depending on which user logs in?

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Display buttons dynamically

    Quote Originally Posted by dr223 View Post
    This doesnt look nice on my form; so regardless if they are ONLY 2 buttons the space between should be similar to a user with 3 , 4 or even 7 buttons (standard).
    Might want to try putting the buttons into a FlowLayoutPanel, size the panel & set properties like FlowDirection, etc,etc as needed.
    Set the buttons Visible property at run time and they will auto group without spaces between them.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,039

    Re: Display buttons dynamically

    One thing you could do is leave the buttons alone. Have all the buttons on the form, with click events, but the behavior of the button is changed. For example, the click event for button 2 might be something like this:

    Code:
    If user = someUser Then
     DoSomeUserStuff()
    Else
     DoOtherUserStuff()
    End If
    By doing something like this, you can decide the number of buttons you want to show based on the users, you can also decide the captions you want on the buttons based on the user, and the code behind the button will call different methods based on the user.

    The alternative would be hiding all but the buttons you want and moving the others. The amount of work is probably about the same either way.
    My usual boring signature: Nothing

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    Re: Display buttons dynamically

    many good suggestions
    this is one way of doin it, control the controls

    ex
    Code:
            Dim c As Control
            Dim iLeft As Integer
            Dim iSpace As Integer
            Dim iTop As Integer
            iSpace = 2
            iLeft = 20
            iTop = 20
            For Each c In Me.Controls
                If TypeOf c Is Button Then
                    If c.Visible Then
                        c.Left = iLeft
                        c.Top = iTop
                        iLeft = c.Left + c.Width + iSpace
                    End If
                End If
            Next
        End Sub
    shows u how u can automate the process of controling controls
    -= a peet post =-

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,039

    Re: Display buttons dynamically

    WOW!! A peerless peet post!
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: Display buttons dynamically

    Ok Thanks guys..

    On my Form Load I have the following code;

    Code:
    Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		Dim c As Control
    		Dim iLeft As Integer
    		Dim iSpace As Integer
    		Dim iTop As Integer
    		iSpace = 20
    		iLeft = 20
    		iTop = 20
    
    		For Each c In Me.Controls
    			If TypeOf c Is Button Then
    				If c.Visible Then
    					c.Left = iLeft
    					c.Top = iTop
    					iLeft = c.Left + c.Width + iSpace
    				End If
    			End If
    		Next
    	End Sub
    I have 10 buttons on my form arranged as;

    A B C D E F G H I

    J

    I have the buttons named as BtnA, BtnB, BtnC, .....BtnJ..

    How can I update the above code so that when USERA logs on - and he should not see BtnA and BtnB, for example the other buttons are moved to the right so that it shows consisently.

    Thank you
    Last edited by dr223; Apr 24th, 2014 at 05:03 AM.

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Display buttons dynamically

    Quote Originally Posted by dr223 View Post
    How can I update the above code so that when USERA logs on - and he should not see BtnA and BtnB
    In the loop first check the user name and only make the button(s) that you want that user to have access to visible and increment the location variable for the next button.

    Code:
    Private userName As String = "USERA"
    
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim iLeft As Integer = 20
        Dim iSpace As Integer = 20
        Dim iTop As Integer = 20
    
        For Each c As Control In Me.Controls
            If TypeOf c Is Button Then
                If userName = "USERA" Then ' "USERA" should not see BtnA and BtnB.
                    If c.Name <> "BtnA" AndAlso c.Name <> "BtnB" Then
                        c.Visible = True
                        c.Location = New Point(iLeft, iTop)
                        iLeft += c.Width + iSpace
                    Else ' hide button from this user
                        c.Visible = False
                    End If
                Else ' everyone else sees all buttons
                    c.Visible = True
                    c.Location = New Point(iLeft, iTop)
                    iLeft += c.Width + iSpace
                End If
            End If
        Next
    End Sub
    Just note when looping thru controls like this they'll be in the order they were added to the form!
    If you need to do this for many users then you'll most likely want to use lists to hold user names and the buttons they have access to.
    Last edited by Edgemeal; Apr 24th, 2014 at 01:27 PM. Reason: TYPOs

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