AutoScroll Puzzle
-
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
-
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
Simple math and the
ScrollableControl.AutoScrollPosition
(inheritted by derivative classes likePanel
). 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 likeControl.PointToClient
andControl.PointToScreen
). If you have aComboBox
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 currentAutoScrollPosition
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. APoint
is a value type - not a reference type - and needs to be copied first before being modified. If you setAutoScrollPosition.X
to some offset, you'll notice no change because a copy was made but never assigned toAutoScrollPosition
again. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] -
Simple math and the
ScrollableControl.AutoScrollPosition
(inheritted by derivative classes likePanel
). 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 likeControl.PointToClient
andControl.PointToScreen
). If you have aComboBox
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 currentAutoScrollPosition
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. APoint
is a value type - not a reference type - and needs to be copied first before being modified. If you setAutoScrollPosition.X
to some offset, you'll notice no change because a copy was made but never assigned toAutoScrollPosition
again. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]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 -
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 GriffinYour offset isn't an offset at all. You need to off-set the current
Point
(theAutoScrollPosition
) by a certain amount like I did in my example. What you're doing now would make theLocation
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] -
Your offset isn't an offset at all. You need to off-set the current
Point
(theAutoScrollPosition
) by a certain amount like I did in my example. What you're doing now would make theLocation
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]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 bemypanel.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 -
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 bemypanel.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 GriffinIt's what you do to the
Point
before assigning it back tomypanel.AutoScrollPosition
that matters. If you haven't already, read about theScrollableControl.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] -
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 bemypanel.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 GriffinReading 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
-
It's what you do to the
Point
before assigning it back tomypanel.AutoScrollPosition
that matters. If you haven't already, read about theScrollableControl.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]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