我想使用自定义垂直滚动条更改文本框的垂直滚动位置,所以这是我的代码。
private void textBox1_Load(object sender, EventArgs e)
{
vScrollBar1.Value = textBox1.VerticalScroll.Value;
textBox1.VerticalScroll.Value = vScrollBar1.Value;
}
private void timer1_Tick(object sender, EventArgs e)
{
vScrollBar1.Minimum = textBox1.VerticalScroll.Minimum;
vScrollBar1.Maximum = textBox1.VerticalScroll.Maximum;
vScrollBar1.Value = textBox1.VerticalScroll.Value;
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
textBox1.VerticalScroll.Minimum = vScrollBar1.Minimum;
textBox1.VerticalScroll.Maximum = vScrollBar1.Maximum;
textBox1.VerticalScroll.Value = vScrollBar1.Value;
}
private void Form1_Load(object sender, EventArgs e)
{
vScrollBar1.Minimum = textBox1.VerticalScroll.Minimum;
vScrollBar1.Maximum = textBox1.VerticalScroll.Maximum;
vScrollBar1.Value = textBox1.VerticalScroll.Value;
}
在这里,我可以使文本框的滚动条值与自定义滚动条相同,但是当我尝试使用自定义滚动条修改该值不起作用时,有人可以帮助我吗?
答案 0 :(得分:0)
以编程方式更改滚动值也可能引发事件。您应该确保在执行此操作时不要再次处理事件。为此使用标志。
private bool _changing;
private void ScrollingFromTextBoxToScrollBar()
{
if (!_changing) {
_changing = true;
try {
vScrollBar1.Minimum = textBox1.VerticalScroll.Minimum;
vScrollBar1.Maximum = textBox1.VerticalScroll.Maximum;
vScrollBar1.Value = textBox1.VerticalScroll.Value;
} finally {
_changing = false;
}
}
}
private void ScrollingFromScrollBarToTextBox()
{
if (!_changing) {
_changing = true;
try {
textBox1.VerticalScroll.Minimum = vScrollBar1.Minimum;
textBox1.VerticalScroll.Maximum = vScrollBar1.Maximum;
textBox1.VerticalScroll.Value = vScrollBar1.Value;
} finally {
_changing = false;
}
}
}
然后在加载时应用那些。我会从FastColoredTextBox
中获取值,因为它内部有自己的逻辑。
private void Form1_Load(object sender, EventArgs e)
{
ScrollingFromTextBoxToScrollBar();
}
private void textBox1_Load(object sender, EventArgs e)
{
ScrollingFromTextBoxToScrollBar();
}
观看滚动条
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
ScrollingFromScrollBarToTextBox();
}
并且您还必须注意编辑时FastColoredTextBox
的滚动,以适应滚动条的滚动位置
private void textBox1_Scroll(object sender, ScrollEventArgs e)
{
ScrollingFromTextBoxToScrollBar();
}