我有两个跟踪条做同样的事情,除了一个用于左执行器,一个用于右边。我想在我的GUI中添加同步轨迹栏的选项,以便用户可以选择以相同的方式控制两个执行器。现在,问题是轨道栏会在我第一次尝试时同步,但随后两个句柄都会冻结。这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace GUI1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.slider1.Minimum = 0;
this.slider1.Maximum = 9;
this.slider2.Minimum = 0;
this.slider2.Maximum = 9;
}
//ends public form1
private void Form1_Load(object sender, EventArgs e)
{
}
//starts first slider
private void slider1_Scroll(object sender, EventArgs e)
{
System.Windows.Forms.TrackBar slider1;
slider1 = (System.Windows.Forms.TrackBar)sender;
textBox1.Text = "" + slider1.Value.ToString();
if (syncOption.Checked == true)
{
slider1.Value = Convert.ToInt32(slider2.Value);
textBox1.Text = slider1.Value.ToString();
}
}//ends first slider
//starts second slider
private void slider2_Scroll(object sender, EventArgs e)
{
System.Windows.Forms.TrackBar slider2;
slider2 = (System.Windows.Forms.TrackBar)sender;
textBox2.Text = "" + slider2.Value.ToString();
if (syncOption.Checked == true)
{
slider2.Value = Convert.ToInt32(slider1.Value);
textBox2.Text = slider2.Value.ToString();
}
}//ends second slider
}
}
感谢您的帮助!
答案 0 :(得分:0)
您的问题很简单,当您移动slider1时,您将其值设置为代码中slider2的值;相反,你应该移动slider1 然后设置slider2的值;
private void slider1_Scroll(object sender, EventArgs e)
{
System.Windows.Forms.TrackBar slider1;
slider1 = (System.Windows.Forms.TrackBar)sender;
textBox1.Text = "" + slider1.Value.ToString();
if (syncOption.Checked == true)
{
slider2.Value = Convert.ToInt32(slider1.Value);
textBox2.Text = slider2.Value.ToString();
}
}//
Slider2相同:
private void slider2_Scroll(object sender, EventArgs e)
{
System.Windows.Forms.TrackBar slider2;
slider2 = (System.Windows.Forms.TrackBar)sender;
textBox2.Text = "" + slider2.Value.ToString();
if (syncOption.Checked == true)
{
slider1.Value = Convert.ToInt32(slider2.Value);
textBox1.Text = slider1.Value.ToString();
}
}
你的同步逻辑只是错误的方式!
使用Winforms,VS2010进行测试。
修改强>
请注意,您实际上可以为两个滑块使用单个事件处理程序,例如:
private void Slider_Scroll(object sender, EventArgs e)
{
TrackBar sourceSlider;
TextBox sourceText;
TrackBar targetSlider;
TextBox targetText;
sourceSlider = sender == trackBar1 ? trackBar1 : trackBar2;
targetSlider = sender == trackBar1 ? trackBar2 : trackBar1;
sourceText = sender == trackBar1 ? textBox1 : textBox2;
targetText = sender == trackBar1 ? textBox2 : textBox1;
sourceText.Text = Convert.ToString(sourceSlider.Value);
if (syncOption.Checked)
{
targetSlider.Value = sourceSlider.Value;
targetText.Text = Convert.ToString(targetSlider.Value);
}
}
您可以在设计器中或在代码中(在构造函数或Load事件中)连接,如:
trackBar1.Scroll += Slider_Scroll;
trackBar2.Scroll += Slider_Scroll;