Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. AutoScroll Puzzle

AutoScroll Puzzle

Scheduled Pinned Locked Moved C#
graphicswinformshelpquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Griffin
    wrote on last edited by
    #1

    Hi All, i've a tricky little problem with AutoScroll functionality that i can't seem to overcome. I have a panel with its autoscroll property set to true. On the panel i have several combo boxes which are surrounded by some graphics implemented with GDI+. My problem is as follows .. say a combo box at the bottom of the panel has focus, when i click on the panel i want it to gain focus but not refresh and redraw itself at its initial position i.e. (0,0). In other words when the panel gets focus i still want to be able to view the combo box at the bottom of the panel which had previously been the active control. Is there any way of preventing the panel from redrawing itself at its initial (0,0) position??? Or is it possible to calculate the autoscroll position prior to giving the panel focus then explicitly setting the autoscroll position to the desired position. Any ideas and suggestions are very welcome and appreciated! Thanks, Paul Griffin

    H 1 Reply Last reply
    0
    • P Paul Griffin

      Hi All, i've a tricky little problem with AutoScroll functionality that i can't seem to overcome. I have a panel with its autoscroll property set to true. On the panel i have several combo boxes which are surrounded by some graphics implemented with GDI+. My problem is as follows .. say a combo box at the bottom of the panel has focus, when i click on the panel i want it to gain focus but not refresh and redraw itself at its initial position i.e. (0,0). In other words when the panel gets focus i still want to be able to view the combo box at the bottom of the panel which had previously been the active control. Is there any way of preventing the panel from redrawing itself at its initial (0,0) position??? Or is it possible to calculate the autoscroll position prior to giving the panel focus then explicitly setting the autoscroll position to the desired position. Any ideas and suggestions are very welcome and appreciated! Thanks, Paul Griffin

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Simple math and the ScrollableControl.AutoScrollPosition (inheritted by derivative classes like Panel). Just be sure to read the .NET Framework SDK documentation about when to use client- and screen-based coordinates (which can be easily converted using methods like Control.PointToClient and Control.PointToScreen). If you have a ComboBox that is partly hidden and want to make sure that it's completely visible without scrolling it to the client point 0,0 for its container, find out how much is hidden and offset the current AutoScrollPosition by that amount:

      int offset = comboBox1.Bottom - this.Height;
      Point p = this.AutoScrollPosition;
      p.X -= offset;
      this.AutoScrollPosition = p;

      This is only an example, but hopefully gives you some idea. Also, do not simple set AutoScrollPosition.X to something. A Point is a value type - not a reference type - and needs to be copied first before being modified. If you set AutoScrollPosition.X to some offset, you'll notice no change because a copy was made but never assigned to AutoScrollPosition again. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

      P 1 Reply Last reply
      0
      • H Heath Stewart

        Simple math and the ScrollableControl.AutoScrollPosition (inheritted by derivative classes like Panel). Just be sure to read the .NET Framework SDK documentation about when to use client- and screen-based coordinates (which can be easily converted using methods like Control.PointToClient and Control.PointToScreen). If you have a ComboBox that is partly hidden and want to make sure that it's completely visible without scrolling it to the client point 0,0 for its container, find out how much is hidden and offset the current AutoScrollPosition by that amount:

        int offset = comboBox1.Bottom - this.Height;
        Point p = this.AutoScrollPosition;
        p.X -= offset;
        this.AutoScrollPosition = p;

        This is only an example, but hopefully gives you some idea. Also, do not simple set AutoScrollPosition.X to something. A Point is a value type - not a reference type - and needs to be copied first before being modified. If you set AutoScrollPosition.X to some offset, you'll notice no change because a copy was made but never assigned to AutoScrollPosition again. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

        P Offline
        P Offline
        Paul Griffin
        wrote on last edited by
        #3

        Hi Heath, thanks for your response! Your suggestion is actually the way i had approached the problem. In the MouseDown event handler for my panel i create a vertical offset from the MouseEventArgs e.Y. as follows: int offset = e.Y ; this.myPanel.Focus(); Point p = this.myPanel.AutoScrollPosition; p.Y = offset; this.myPanel.AutoScrollPosition = p; When i debug the code i see that p does get initialised to the correct value but the assignment to AutoScrollPosition never works. AutoScrollPosition always remains (0,0)??? Any ideas as to what is happening here?? :confused: Thanks again! Paul Griffin

        H 1 Reply Last reply
        0
        • P Paul Griffin

          Hi Heath, thanks for your response! Your suggestion is actually the way i had approached the problem. In the MouseDown event handler for my panel i create a vertical offset from the MouseEventArgs e.Y. as follows: int offset = e.Y ; this.myPanel.Focus(); Point p = this.myPanel.AutoScrollPosition; p.Y = offset; this.myPanel.AutoScrollPosition = p; When i debug the code i see that p does get initialised to the correct value but the assignment to AutoScrollPosition never works. AutoScrollPosition always remains (0,0)??? Any ideas as to what is happening here?? :confused: Thanks again! Paul Griffin

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Your offset isn't an offset at all. You need to off-set the current Point (the AutoScrollPosition) by a certain amount like I did in my example. What you're doing now would make the Location of your control scroll to the top which would be 0,0 (the position within the container control). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

          P 1 Reply Last reply
          0
          • H Heath Stewart

            Your offset isn't an offset at all. You need to off-set the current Point (the AutoScrollPosition) by a certain amount like I did in my example. What you're doing now would make the Location of your control scroll to the top which would be 0,0 (the position within the container control). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

            P Offline
            P Offline
            Paul Griffin
            wrote on last edited by
            #5

            Hi Heath, sorry if i seem to be missing the point! Perhaps i need to re-phrase what it is i'm trying to accomplish. I'm trying to get a panel with AutoScroll set to true to scroll to a certain location i.e. the location last clicked before i gave focus to the panel thus causing it to redraw at its initial position. No in your example you use this.AutoScrollPosition = p; i assume for me this would be mypanel.AutoScrollPosition = p; but when i do this and debug it if i step over the assignment the AutoScrollPosition value = (0,0); so i understand that i need to offset the AutoScrollPosition .. i just can't seem to get it to change from (0,0) again i appreciate your help here! this is starting to drive me mad! :) Paul Griffin

            H P 2 Replies Last reply
            0
            • P Paul Griffin

              Hi Heath, sorry if i seem to be missing the point! Perhaps i need to re-phrase what it is i'm trying to accomplish. I'm trying to get a panel with AutoScroll set to true to scroll to a certain location i.e. the location last clicked before i gave focus to the panel thus causing it to redraw at its initial position. No in your example you use this.AutoScrollPosition = p; i assume for me this would be mypanel.AutoScrollPosition = p; but when i do this and debug it if i step over the assignment the AutoScrollPosition value = (0,0); so i understand that i need to offset the AutoScrollPosition .. i just can't seem to get it to change from (0,0) again i appreciate your help here! this is starting to drive me mad! :) Paul Griffin

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              It's what you do to the Point before assigning it back to mypanel.AutoScrollPosition that matters. If you haven't already, read about the ScrollableControl.AutoScrollPosition and pay close attention to the the note just before the example. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

              P 1 Reply Last reply
              0
              • P Paul Griffin

                Hi Heath, sorry if i seem to be missing the point! Perhaps i need to re-phrase what it is i'm trying to accomplish. I'm trying to get a panel with AutoScroll set to true to scroll to a certain location i.e. the location last clicked before i gave focus to the panel thus causing it to redraw at its initial position. No in your example you use this.AutoScrollPosition = p; i assume for me this would be mypanel.AutoScrollPosition = p; but when i do this and debug it if i step over the assignment the AutoScrollPosition value = (0,0); so i understand that i need to offset the AutoScrollPosition .. i just can't seem to get it to change from (0,0) again i appreciate your help here! this is starting to drive me mad! :) Paul Griffin

                P Offline
                P Offline
                Paul Griffin
                wrote on last edited by
                #7

                Reading that back still didn't sound right i'll try one more time. The sequence of events are as follows: 1. My panel receives focus and using the mouse wheel or scroll bar itself i scroll to some point on the panel. 2. I click on a second control e.g. a combo box. 3. I'm now finished using the combo box and i would like the panel to regain focus so that i can continue scrolling it. 4. I click back onto the panel, the panel_MouseDown event handler fires in which i include the line panel.Focus(); Problem: Each time this line is executed the panel redraws itself with the scrollbar at its initial position and i have to scroll all the way down to where i was before i can continue to the next control lower down on the panel. May sound like a trivial problem but as i've said repeatedly the functionality to set the scroll bar position explicitly doesn't seem to be there! There hope that describes the situation a bit more clearly! :) Paul Griffin

                1 Reply Last reply
                0
                • H Heath Stewart

                  It's what you do to the Point before assigning it back to mypanel.AutoScrollPosition that matters. If you haven't already, read about the ScrollableControl.AutoScrollPosition and pay close attention to the the note just before the example. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

                  P Offline
                  P Offline
                  Paul Griffin
                  wrote on last edited by
                  #8

                  Hi Heath, just to let you know i finally cracked it, a combination of thinking about your posts properly and spotting a very stupid mistake i was making. Thanks a lot for your replies! Paul Griffin

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups