Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active June 14, 2022 13:55
Show Gist options
  • Save bjoerntx/1ed4507a52a27ad4ce188e29bb67b170 to your computer and use it in GitHub Desktop.
Save bjoerntx/1ed4507a52a27ad4ce188e29bb67b170 to your computer and use it in GitHub Desktop.
private bool scrolling = false;
private void setScrollOffset() {
// remember old scroll location
var oldScrollLocation = textControl1.ScrollLocation;
// scroll to bottom to get max value
textControl1.ScrollLocation = new Point(0, int.MaxValue);
int scrollY = textControl1.ScrollLocation.Y;
// mirror the max value in custom scroll bar
vScrollBar1.Maximum = scrollY;
// restore scroll location
textControl1.ScrollLocation = oldScrollLocation;
}
private void textControl1_Changed(object sender, EventArgs e) {
setScrollOffset();
}
private void textControl1_VScroll(object sender, EventArgs e) {
if (scrolling == true)
return;
// set the scroll location
if (textControl1.ScrollLocation.Y <= vScrollBar1.Maximum) {
vScrollBar1.Value = textControl1.ScrollLocation.Y;
vScrollBar1.SmallChange = 300; // fine tune speed here
vScrollBar1.LargeChange = 300;
}
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e) {
// mirror the scroll location
scrolling = true;
textControl1.ScrollLocation = new Point(0, e.NewValue);
scrolling = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment