Location of a control regardless of Vertical scroll bar effect - c#

C# WinForms: if I want to have the location of a control on a panel regardless of where is the Vertical scroll bar, what should I use? I mean the form is large so we use scroll bar to move up and down, now I want that location to be independent of where I have set the scroll bar this.PointToClinet?

The Location property is already independent of the scrollbar position. A control at (0,0) stays at (0,0) in a panel with a scrollbar. What changes is the AutoScrollPosition property value. With a vertical scrollbar, the AutoScrollPosition.Y value becomes negative when the user scrolls down. Which makes whatever is inside the panel move up.
Trying to keep something in the same position even if the scrollbar is used is technically possible by correcting for AutoScrollPosition.Y. But doesn't work well in practice, whatever you are scrolling is doing the pogo, rapidly jumping up and down as you scroll. Which is caused by Windows blitting the scrolled pixels after which you redraw it back in the original position. Find a workaround for that problem in this post.

Related

PictureBox anchor not working properly

On my form I have a Panel control that contains a PictureBox control and a Label control.
The panel is not visible in the image above but it's basically the area around these two controls.
I have set the Anchor property of all these three controls to Top, Bottom, Left, Right so that they follow their parent container's re-sizing behavior.
The L abel control (postbagfolderempty) works properly but the PictureBox (EMPTY!) does not seem to be moving from its original position.
Is there an additional property I need to set?
Update: I changed my PictureBox's AutoSize property to None. It has started to move, but as I try to enlarge my form, it starts sinking into a white area (image below).
Make sure your PictureBox doesn't have SizeMode set to AutoSize.
Anchoring changes the size, if it's autosized it won't change anything
Also, make 100% sure your PictureBox is actually a child of the panel. It's easy to check: select it on the designer and press Esc, it should select the panel.
Update
As per the comments, seems the problem is that you are anchoring to all the sides (thus producing the scaling of the control).
If you want a panel that scales with the form, and controls within the panel that are centered but not scaled along, then anchor that panel to all sides, place the controls inside the panel centered in the designer, and set their anchors to None, that way they won't scale, and since they are not anchored, they will move along when the panel scales (but they won't scale with it, which -seems- it's what you are aiming at)

UI Automation: How to determine how far a scroll bar scrolls a control

I'm faced with a problem: I am trying to automate a control with UI Automation. The control is a viewer in a client application, which hooks into a service hosted remotely. As a result of some legacy design decisions, this viewer simply displays a bitmap on a canvas. When interactions occur (e.g. clicks), the position of the click is sent to the service, which uses the co-ordinates to work out where the click occurred, and react correspondingly. The result of this is a nightmare for UI test automation. There is no way of hooking directly into sub-controls, because they are simply painted on to the bitmap. I have found a back-end way of accessing information about what is in this canvas, but now I need to work out where, in this scrolling bitmap, those items appear, so I can interact with them. I use positional information based on the upper part of the control, but since I don't have access to the bounding rects of these sub-controls, as soon as I scroll, all this information becomes invalid. My main problem is that I can't work out how far the scroll bar moves the canvas. Since scroll bars in UIA only have values from 0-100 (despite the actual magnitude of the scrolling effect), I can't work out how far down the canvas I've moved from a known position (it would depend on how far the scroll bar can move at that given time - i.e. how many sub-controls have been rendered in the bitmap). Is there any way of working out the magnitude of the scroll event on this canvas? I know that this must be done internally - the scroll bar has to know how far to move the canvas, based on the actual size of the canvas. However, the bounding rect of the canvas only gives it's visible on-screen position - it doesn't indicate how big the underlying image is. I either need to get the full size of the bitmap (as if it was rendered fully on screen, without scrolling), or to know how much adjusting the scroll value effects the visible position of the image. Is there any way of working this out?

WinForms: How to avoid horizontal scroll bar with AutoScroll?

I'm writing a custom control that contains a list of items (child controls) that resize horizontally to fit the width of the control. If there are lots of items (or the control is resized so that it is not tall enough vertically) then a vertical scroll bar is necessary; but when the vertical scroll bar appears, the child controls are suddenly too wide, which causes a horizontal scroll bar to appear.
What's the proper way to guarantee that a horizontal scroll bar does not appear when it is not necessary, given that I am controlling the control placement manually (not relying on AnchorStyles)? (Note: I can't control the VScroll property manually because I'm on Compact Framework; and if an item's minimum width is wider than the client area then a horizontal scroll bar will be required legitimately.)
What I did in a similar situation was after every time I added an item to the list I detected whether the scroll bar was visible or not and adjusted my the width manually.
What I did to detect whether the scroll bar was showing was either:
Test for the WS_VSCROLL was set on the control via P/Invoke via GetWindowLong().
Scan the control's children for a vertical scroll bar control.
It depends on how the control handles scroll bars as to which one is correct.
Also this was on Windows, not in the CF so I'm not sure if this will work exactly the same way.
Take the width of the vertical scrollbar into account when calculating the required width for your child controls:
System.Windows.Forms.SystemInformation.VerticalScrollBarWidth

how to adjust sliding of scroll according to the value calculated by me?

I am working on scroll. I have made a program wherein if the scroll box is in particular range of the panel then it automatically moves to a particular position in the slider.
For eg, I have a panel of size 500. the scroll has height of 100. Now the total intervals are 5. Now if i slide the scroll box at a position 225 then i want that it automatically slides to the start of that interval that is at position 200. or if i slide to position 450 then instead of staying there it goes to position 400.
Is it possible?
It's possible, in the same way that running your fingernails across a chalkboard is possible.
I'm not sure exactly what you're trying to do here, but I assume it's something to do with handling a scroll bar's Scroll event and forcing it to be set to some value other than what the user intended to set it to. It's generally inadvisable to make controls behave in some weird, unexpected manner.

How can I make sure a control is visible when it's inside a scrolling control?

I have a tabcontrol with several tabpages. I also have hooked into the InputPanel.EnabledChanged event so that the tabcontrol resizes when the inputpanel is enabled. What I would like to do is ensure that the control with the focus is visible when the tabpage is resized. What's the best way to accomplish this. Note that I am using the .NET Compact Framework.
Thanks,
MrB
You have a couple of things you need to/may want to keep track of here:
• Current scroll position
• Position of the tapped element (I assume a textbox) when they tapped it
Note that the EnabledChanged event for the InputPanel is pretty easy to deal with (I assume you've already done it): check the InputPanel's Enabled state and do a += or -= to the tabcontrol's Height so the InputPanel doesn't cover anything up.
My understanding is that your issue is really to determine where the textbox was, because your worst-case scenario is that it will be moved off-screen when the tabcontrol resizes.
I believe (not 100% certain) that resizing a tabcontrol will keep the top of the tabcontrol contents visible and you'll have to scroll down to see anything that is now "covered" by the InputPanel.
What you'll want to do in the EnabledChanged event is find out the location of the control that was tapped (or the screen coordinate that was tapped - whatever is easiest for you). If it falls within the danger zone of (ClientSize.Height-InputPanel.Bounds.Height,ClientSize.Height), you'll need to adjust your scroll.
This is half-C#-half-pseudocode but you should be able to see where I'm going with it:
int yOffset = top y-coordinate of the tapped control;
if ( yOffset > ClientSize.Height-inputPanel.Bounds.Height )
{
int yShift = amount to shift everything up;
Point scrollPosition = AutoScrollPosition;
scrollPosition.Y = scrollPosition.Y - yShift;
AutoScrollPosition = scrollPosition;
}
What this does, in a nutshell, is checks if your InputPanel is going to cover up the desired control, and if so, scroll up by yShift pixels, where yShift might be the whole height of the inputPanel, or just enough to barely show your control (whatever you want).
• .NET CF seems very forgiving about setting invalid scroll rules. If you can only scroll up 10 pixels and you tell it to scroll up 100, it will stop at 10.
• Screen coordinate (0,0) is the top left of the visible screen - NOT the form being displayed. You can get tripped up if you assume that (0,0) is the top of your form.
The only way that comes to my mind is calculate this by currrent controls positions on a screen.

Categories

Resources