locked
Auto hide side bar RRS feed

  • Question

  • I want to create autohide Side bar in Winforms using VB.Net 2005. I have main form there is already fixed side bar with lot of options now just i want it autohide just like VS.NET 2005 Toolbox any help??
    Friday, July 10, 2009 5:31 PM

Answers

  • I did this in vs2008 using a Panel to hold the buttons, textbox, etc

    Not sure if it will help you

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Panel1.Width = 10  ' set initial panel width (hides the panel)
            Panel1.BorderStyle = BorderStyle.None<br/>        Panel1.Dock = DockStyle.Left
        End Sub
    
        Private Sub Panel1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseEnter
            Panel1.BorderStyle = BorderStyle.Fixed3D
            Panel1.BackColor = Color.AliceBlue
            For i As Integer = 10 To 160
                    Panel1.Width = i
            Next
          
        End Sub
        Private Sub Panel1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
            
                For i As Integer = 160 To 10 Step -1
                    Panel1.Width = i
                Next
           
            Panel1.BorderStyle = BorderStyle.None
            Panel1.BackColor = Color.Transparent
        End Sub
    
    Friday, July 10, 2009 5:38 PM
  • i deleted a previous post due to finally getting the webbrowser to actually anchor to all sides.. (sighs) all that work just for a '+'..

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            WebBrowser1.Navigate("http://disneyworld.disney.go.com/")
            WebBrowser1.Dock = DockStyle.None 'undock if docked
            WebBrowser1.Size = Me.Size 'sizes the webbrowser, similar to docking, plus some
            WebBrowser1.Left = WebBrowser1.Left + 10 'moves webbrower +10 from the left side of window
            WebBrowser1.Width = WebBrowser1.Width - 30 'keeps the width of webbrowser inside the window
            WebBrowser1.Height = WebBrowser1.Height - 40 'keeps the height of webbrowser inside the window
            WebBrowser1.Anchor = AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right 'anchors to all sides
        End Sub
     
    and done..
    hope this is what you meant by supplying the code to do so..
    trujade.

    i like: VB General   google   fast cars   username password
    • Edited by •.trujade.• Monday, July 13, 2009 6:28 AM changed the web address to disney.. ;o)
    • Marked as answer by M Imran Shafiq Monday, July 13, 2009 8:31 AM
    Monday, July 13, 2009 6:12 AM

All replies

  • I did this in vs2008 using a Panel to hold the buttons, textbox, etc

    Not sure if it will help you

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Panel1.Width = 10  ' set initial panel width (hides the panel)
            Panel1.BorderStyle = BorderStyle.None<br/>        Panel1.Dock = DockStyle.Left
        End Sub
    
        Private Sub Panel1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseEnter
            Panel1.BorderStyle = BorderStyle.Fixed3D
            Panel1.BackColor = Color.AliceBlue
            For i As Integer = 10 To 160
                    Panel1.Width = i
            Next
          
        End Sub
        Private Sub Panel1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
            
                For i As Integer = 160 To 10 Step -1
                    Panel1.Width = i
                Next
           
            Panel1.BorderStyle = BorderStyle.None
            Panel1.BackColor = Color.Transparent
        End Sub
    
    Friday, July 10, 2009 5:38 PM
  • jwavila.. nice code. although i do have a question.
    it works fine with the mouse enter, mouse leave events for the panel, but is there a simple code that will keep the panel open by hovering the mouse over a button, checkebox, etc.., and close the panel when clicked on?
    something like

    for 'every control' in panel1

    instead of having to add the code to all the controls events?
    trujade..

    i like: VB General  google  fast cars  username password
    Friday, July 10, 2009 5:56 PM

  • In the Form1.load event add:



            For Each Control As Control In Panel1.Controls
                AddHandler Control.Click, AddressOf ControlClicked
            Next
    and replace the MouseLeave event by



        Private Sub ControlClicked(ByVal sender As Object, ByVal e As System.EventArgs)
            For i As Integer = 160 To 10 Step -1
                Panel1.Width = i
            Next
            Panel1.BorderStyle = BorderStyle.None
            Panel1.BackColor = Color.Transparent
    
        End Sub








    • Proposed as answer by Shariq Ayaz Sunday, July 12, 2009 4:48 PM
    Friday, July 10, 2009 6:32 PM
  • thanx crazypennie..

    i also found this useful to keep the panel from trying to reopen when hovering over the different controls..

        Private Sub Panel1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseEnter
            If Panel1.Width = 160 Then
            Else
                Panel1.BorderStyle = BorderStyle.Fixed3D
                Panel1.BackColor = Color.AliceBlue
                For i As Integer = 10 To 160
                    Panel1.Width = i
                Next
            End If
        End Sub

    just added the if statement to jwavila's code.

    i also added the code from panel1_mouse leave to form1_mouse enter. seems to work well w/a plain form.. ;o)
    just some suggestions..
    trujade



    i like: VB General   google   fast cars   username password
    Friday, July 10, 2009 7:22 PM
  • Thanks Crazypennie

    just came back on, saw the question by trujade and was going to respond when I saw your answer.

    this code still needs some work. for example, with the above to hide the panel with a click and removing the MouseLeave, now if the mouse leaves the panel without clicking a control, it won't hide. But if you leave that, when the mouse leaves the panel and enters a control (eg button), then it hides.  Nothing major that can't be fixed - like I said, still needs some work.
    Friday, July 10, 2009 7:23 PM
  • good startup jwavila.
    this kind of homework i find pleasant. ;o)
    i hope the op thinks as well.
    trujade.
    i like: VB General  google  fast cars  username password
    Friday, July 10, 2009 7:31 PM
  • Its working great but when i added controls on it like web browser control with fill dock then its not working. I want to add lot of controls on it.
    Sunday, July 12, 2009 11:38 AM
  • Its effeciency low when i put 10 to 15 controls on it.
    Sunday, July 12, 2009 11:43 AM
  • about the webbrowser problem, right click the webbrowser, select 'send to back'..

    you can also set the webbrowser location to '10, 0', size it to your form, anchor it to all 4 corners and not have it docked.. this should keep the web content viewed properly in your webrowser.

    about efficiency.. do you mean that it slows down with the more controls that you add to the panel? because i added 20 controls in my panel and it reacts the same.

    trujade.

    i like: VB General  google  fast cars  username password
    Sunday, July 12, 2009 12:14 PM
  • Can you tell us what kind of controls you have,

    I can not reproduce the problem neither
    Sunday, July 12, 2009 12:47 PM
  • about the webbrowser problem, right click the webbrowser, select 'send to back'..

    you can also set the webbrowser location to '10, 0', size it to your form, anchor it to all 4 corners and not have it docked.. this should keep the web content viewed properly in your webrowser.

    about efficiency.. do you mean that it slows down with the more controls that you add to the panel? because i added 20 controls in my panel and it reacts the same.

    trujade.

    i like: VB General   google   fast cars   username password

    Can u provide me code which u r using??
    Monday, July 13, 2009 4:40 AM
  • this was done manually and set from the properties of the the webbrowser.

    i like: VB General  google  fast cars  username password
    Monday, July 13, 2009 4:41 AM
  • i deleted a previous post due to finally getting the webbrowser to actually anchor to all sides.. (sighs) all that work just for a '+'..

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            WebBrowser1.Navigate("http://disneyworld.disney.go.com/")
            WebBrowser1.Dock = DockStyle.None 'undock if docked
            WebBrowser1.Size = Me.Size 'sizes the webbrowser, similar to docking, plus some
            WebBrowser1.Left = WebBrowser1.Left + 10 'moves webbrower +10 from the left side of window
            WebBrowser1.Width = WebBrowser1.Width - 30 'keeps the width of webbrowser inside the window
            WebBrowser1.Height = WebBrowser1.Height - 40 'keeps the height of webbrowser inside the window
            WebBrowser1.Anchor = AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right 'anchors to all sides
        End Sub
     
    and done..
    hope this is what you meant by supplying the code to do so..
    trujade.

    i like: VB General   google   fast cars   username password
    • Edited by •.trujade.• Monday, July 13, 2009 6:28 AM changed the web address to disney.. ;o)
    • Marked as answer by M Imran Shafiq Monday, July 13, 2009 8:31 AM
    Monday, July 13, 2009 6:12 AM
  • M Imran Shafiq,
     you should also mark the first reply from jwavila as an answer since it does apply to the question title..
    just in case someone searches for a 'auto hide side bar ' solution, and will be able to find an answer as well.
    thanx,
    trujade
    i like: VB General  google  fast cars  username password
    Monday, July 13, 2009 9:22 AM
  • i deleted a previous post due to finally getting the webbrowser to actually anchor to all sides.. (sighs) all that work just for a '+'..

        Private
     Sub
     Form1_Load(ByVal
     sender As
     System.Object, ByVal
     e As
     System.EventArgs) Handles
     MyBase
    .Load
            WebBrowser1.Navigate("http://disneyworld.disney.go.com/"
    )
            WebBrowser1.Dock = DockStyle.None 'undock if docked
    
            WebBrowser1.Size = Me
    .Size 'sizes the webbrowser, similar to docking, plus some
    
            WebBrowser1.Left = WebBrowser1.Left + 10 'moves webbrower +10 from the left side of window
    
            WebBrowser1.Width = WebBrowser1.Width - 30 'keeps the width of webbrowser inside the window
    
            WebBrowser1.Height = WebBrowser1.Height - 40 'keeps the height of webbrowser inside the window
    
            WebBrowser1.Anchor = AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right 'anchors to all sides
    
        End
     Sub
    
    

    I've read and been told to use OR when setting a property to multiple values:


    WebBrowser1.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right




    Doug

    SEARCH ... then ask
    Monday, July 13, 2009 1:18 PM
  • good to know doug.. ;o)
    i'll try and remember that, but in proper english that does not register right with me.
    ex.
    i want left or right.. hmm..
    i want left plus right.. better..

    maybe with vb.net, it does register, because i get the same results by using '+' or 'OR'.. just looking at the webbrowser1.Left + 10, gives me confidence that the code i supplied will be picked up for what it is.

    not busting anyone's chops here Doug..
    you ,probably, did not write what you have read, or have not told yourself such.. at least i hope not.. ;o)

    this is something good to know and i'll try to pay attention to, but if it works, it works..
    trujade.
    i like: VB General  google  fast cars  username password
    Monday, July 13, 2009 7:57 PM

  • as a general rule of thumb...

    use + only when working with numbers to do addition. When doing something such as concatenating a string, you should always use &, or use And or Or as in Doug's example with multiple properties. I see a lot of examples where + is used to concatenate a string.

    In trujade's example, obviously webbrowser.Left + 10 works because .Left is an integer. But suppose you were doing something like TextBox.Text.Length + 10 and accidentally left off the .Length?

    The problem is if you're not careful with your variables, and one of them is a string, you are going to end up trying to "add" a string to a number - thus throwing an error.

    Another reason to always have Option Strict On - it won't allow you to do this.

    So while the example AnchorStyles.Top + AnchorStyles.Left does work, in my humble opinion it is not good practice.
    Besides, the MSDN page for Anchor shows Or.
    Monday, July 13, 2009 8:33 PM
  • HI,

    I realize this was posted a long time ago and you may not see this, but I'm looking for answers on this subject and your code was the first I found to work! I'm fairly new to VB, .NET, etc. and am using VS2015. At first, the code wouldn't work in VB, but after removing the expression <br/> and moving the rest of the expression which follows that to the following line the code works fine and the panel opens and closes as expected at runtime!

    However, when I try to add controls to the panel such as a treeview or buttons, they appear on the panel at runtime, but when I try to move the mouse towards the control to use it the panel closes? The controls seem neither to be accessible nor do they seem to function on the panel? I'm not sure if the controls can be added at design time or if they need to be added programatically at runtime? If programatically, I'm not sure how to code for that? Any suggestions?

    Thanks for any help you or anyone else can give!

    Saturday, December 23, 2017 5:03 PM
  • JMW.62,

     You should always start your own question thread instead of asking on someone else's thread.  Post your code in your new question thread and explain what it does or does not do and how you want it to work.

     However,  i will take a quick guess that you are using the MouseLeave event of the Panel to make it move back in.  The MouseLeave event of the Panel will be raised when the mouse moves over top of another control that is placed inside the panel. That is why you have this problem.  To fix that you should check to see if the location of the mouse is still withing the Bounds of the Panel before allowing the code to execute to slide the Panel back in.

     For example...

        Private Sub Panel1_MouseLeave(sender As Object, e As EventArgs) Handles Panel1.MouseLeave
    
            'check to see if the mouse is still inside the bounds of the Panel. If it is Not, then execute the code to slide it in
            If Not Panel1.Bounds.Contains(Me.PointToClient(MousePosition)) Then
    
                'here you execute the code to slide the panel back in or start a Timer that does that
    
            End If
    
        End Sub
    


    If you say it can`t be done then i`ll try it

    Saturday, December 23, 2017 6:09 PM