I have a FlowLayoutPanel with 50 UserControl and I use this.KeyPreview = true
to catch the event KeyUp in the event handler Form1_KeyUp in the form.
So I can either move the scrollbar manually and select a UserControl or step to the next or previous UserControl in the FlowLayoutPanel by using arrow keyUp or arrow key down buttons. The one that is selected has another background.
This works perfect except when at the entpoint.
But there is a problem when I click the arrow up when displaying UserControl in index 0 the VerticalScrollbar will carousel to the end and when click arrow down when displaying index 49 it will carousel to the beginning.
I have googled for preventing VerticalScrollbar to carousel in general or
for FlowLayoutPanel but there is nothing about this.
Any hints are helpful.
//Tony
Related
I am creating an image browser that uses a FlowLayoutPanel to display thumbnails of images. See animated GIF that shows how I scroll down the panel, switch to another window, and then back to the form which causes the FlowLayoutPanel to scroll back to the top. I can't imagine any reason why it would do this.
Also, I seem to be able set the scroll location by clicking on the panel. When the form loses and regains focus, it scrolls back to the last Y position that I clicked.
Why is it exhibiting this behavior and how can I prevent it from occurring?
The effect described is quite common: when a FlowLayoutPanel contains Controls that can be activated and one of these child Controls is selected at some point (in this case a UserControl, which has the WS_EX_CONTROLPARENT extended style, so SetStyle(ControlStyles.Selectable, false) won't do much) and the FlowLayoutPanel is then scrolled to hide this Control, when the Form is deactivated and then activated again, the ActiveControl is scrolled into view.
This causes the FlowLayoutPanel to scroll to a position where the child ActiveControl is visible.
▶ This doesn't happen when the child Controls are not selectable, as the PictureBox Control, for example. If this Control were used to present the thumbnails (as shown in the question), the FlowLayoutPanel would not scroll.
I think the simplest way to prevent the FlowLayoutPanel from scrolling to the ActiveControl is to set the FlowLayoutPanel itself as the ActiveControl when the Form is deactivated, handling the Deactivate event.
private void form1_Deactivate(object sender, EventArgs e)
{
this.ActiveControl = this.flowLayoutPanel1;
}
This has no meaningful side affects, except the Control that previously was the ActiveControl will raise the Leave event.
It may be also used to suspend some other activity, since the User is now focusing on another Window.
▶ To set the ActiveControl to the default one instead (the Control that is activated when the Form is first shown), set this.ActiveControl = null;. It will be reset when the Form is activated again.
I've seen sometimes the Activated and Deactivate events used to disable and enable back a ContainerControl: this also prevents the scrolling, of course, but may cause unwanted cascading effects when child Controls are disabled.
But it may also be something expected and possibly desired. It depends on what happens behind the scene (implementation details).
The solution proposed by #Loathing in comments can also work, deriving a Custom Control from FlowLayoutPanel. It depends on the use case.
Stop form from scrolling when moving controls
I want to drag button on table layout panel that consist 4 row and 4 button each inside, when dragging over the panel I want the other button automatically move up or down depend on location the current button being drag over.
Example : if I drag new button to button2 , automatically button 2,3,4 move down empty the button2 place. I have tried out mouseUp, mouseDown, MouseMove events of control, but that is not what I am looking for.
Can I do this? If you can give me an idea it will be great.
I wanted to have only vertical scroll bar in ListView and so have that implemented using ShowScrollBar Win32 API Method and made listview1.Scrollable = false
But with that the issue I am facing is: if I select an item in listview and keep going down using keyboard down arrow key; the vertical scroll doesn't scroll at all (It scroll only on mouse click).
Is there any way I can solve this problem. Please let me know.
As a first try and a dirty way on KeyDown event:
listView1.LabelEdit = true;
listView1.FocusedItem.BeginEdit();
SendKeys.Send("{Enter}");
I'm having an issue where my scrollbar for a panel on a usercontrol always appears at the bottom.
If I hover the mouse over the tab and cause the control to slide out and be shown, the scrollbar is at the bottom. If I then move the scrollbar to the top and move the mouse off the control (which causes it to hide again) and then move the mouse back over the tab (which causes the control to slide back out) the scroll bar is back at the bottom!
Setting the VerticalScroll.Value property on my panel to 0 is not having any effect - the control still shows at the bottom. Can someone tell me what events might fire when a control slides back into view on an autohide tab or just tell what I could do to solve this issue! I assume that the Paint event is what fires when the control is shown but I'm not entirely sure.
I did notice that in the VS designer that the scrollbar by default is shown at the bottom. Not sure whether this may have anything to do with the issue?
Any suggestions appreciated!
Try this:
scrollingCtrl.VerticalScroll.Value = 0;
scrollingCtrl.PerformLayout();
How can we position the auto scroll of a winform at the top? Currently it is automatically scrolling to bottom of the form.
this.AutoScrollPosition = new Point(0,0);
Where 'this' is the form.
Set Focus to a control at the top.
Winforms scrolls to the control that has focus
In my case I wanted to scroll to a certain control and in the Form that contains mycontrol I did:
mycontrol.Focus();
wich scrolls to the Control
If your form has a panel controls click "View" -> "Tab Order" and click the panel first. The tab's Tab Order will change to 0. This will automatically focus the first panel in the form.