我有一个非常小的具体问题。我正在尝试在 Trackbar 和我的 NumericUpDown 控件之间建立双向关系。基本上移动跟踪栏应更新 NumericUpDown 控件中的值,反之亦然,以防用户想要更直接的输入。
根据问题的上下文,这是我的代码类型:
//Updates Stamp Position Upon Scrolling Trackbar
private void xSlider_Scroll(object sender, EventArgs e)
{
double temp = (xSlider.Value*(paperPanel.Width-stampPreview.Width))/100;
int newX = (int)temp;
stampPreview.Location = new System.Drawing.Point(newX, stampPreview.Location.Y);
System.Diagnostics.Debug.WriteLine(xSlider.Value);
System.Diagnostics.Debug.WriteLine(stampPreview.Location.X);
this.Invalidate();
xSpinner.Value = xSlider.Value;
}
//Updates Stamp Position Upon Scrolling Trackbar
private void ySlider_Scroll(object sender, EventArgs e)
{
double temp = (ySlider.Value * (paperPanel.Height - stampPreview.Height)) / 100;
int newY = (int)temp;
stampPreview.Location = new System.Drawing.Point(stampPreview.Location.X, newY);
System.Diagnostics.Debug.WriteLine(ySlider.Value);
System.Diagnostics.Debug.WriteLine(stampPreview.Location.Y);
this.Invalidate();
ySpinner.Value = ySlider.Value;
}
//Updates Stamp Position when NumericUpDown Control is Altered
private void xSpinner_ValueChanged(object sender, EventArgs e)
{
xSlider.Value = (int)xSpinner.Value;
this.Invalidate();
}
//Updates Stamp Position when NumericUpDown Control is Altered
private void ySpinner_ValueChanged(object sender, EventArgs e)
{
ySlider.Value = (int)ySpinner.Value;
this.Invalidate();
}
问题是,当移动任何一个X方向控件时,控件的表现都很完美。但是,即使Y方向具有完全相同的代码, NumericUpDown 控件也不会反映轨迹栏上的更改,因此不会更新对象的位置。但是,移动Y方向轨迹栏会移动对象。我还注意到我对两者都使用相同的事件,那么我错过了什么以使该触发器失败?
答案 0 :(得分:0)
经过更严格的审查后,事实证明使用“ Scroll ”事件并不是处理这种情况的理想方式。基本上递增数字会更改跟踪栏的值,但不会滚动它,因此代码无法正常执行。奇怪的是,X-Position工作正常,因为我实际上使用了一个 ValueChanged 事件,巧合(并且错误地)被标记为 xSlider_Scroll 。