Reputation: 229
Just like in Word, any browser and tons of other applications, how do you make the content of a panel scroll up or down when dragging an object beyond the visible area of that panel?
This is the closest I come to what I'm looking for but it's still not perfect.
private void Werkorders_DragOver(object sender, DragEventArgs e)
{
grpWerkorders.AutoScrollPosition = grpWerkorders.PointToClient(new Point(e.X, e.Y));
}
Upvotes: 0
Views: 681
Reputation: 81620
This can be done with a WinForms Timer. Example code:
private Timer scrollTimer = new Timer();
private int scrollJump = 0;
public Form1() {
InitializeComponent();
panel1.AllowDrop = true;
panel1.AutoScroll = false;
panel1.AutoScrollMinSize = new Size(0, 1000);
panel1.MouseMove += panel1_MouseMove;
panel1.DragEnter += panel1_DragEnter;
panel1.DragOver += panel1_DragOver;
panel1.QueryContinueDrag += panel1_QueryContinueDrag;
scrollTimer.Tick += scrollTimer_Tick;
}
The drag events:
void panel1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
panel1.DoDragDrop("test", DragDropEffects.Move);
}
}
void panel1_DragEnter(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Move;
}
void panel1_DragOver(object sender, DragEventArgs e) {
Point p = panel1.PointToClient(new Point(e.X, e.Y));
if (p.Y < 16) {
scrollJump = -20;
if (!scrollTimer.Enabled) {
scrollTimer.Start();
}
} else if (p.Y > panel1.ClientSize.Height - 16) {
scrollJump = 20;
if (!scrollTimer.Enabled) {
scrollTimer.Start();
}
} else {
if (scrollTimer.Enabled) {
scrollTimer.Stop();
}
}
}
void panel1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
if (e.Action != DragAction.Continue) {
scrollTimer.Stop();
}
}
The timer code:
void scrollTimer_Tick(object sender, EventArgs e) {
if (panel1.ClientRectangle.Contains(panel1.PointToClient(MousePosition))) {
Point p = panel1.AutoScrollPosition;
panel1.AutoScrollPosition = new Point(-p.X, -p.Y + scrollJump);
} else {
scrollTimer.Stop();
}
}
You can adjust the scrollJump value to increase or decrease the amount the scrollbar changes, or adjust the timer's Interval amount, which defaults to 100 ms.
Upvotes: 1