1

我有两个做同样事情的轨迹条,除了一个用于左侧执行器,一个用于右侧执行器。我想在我的 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




    }

}

我感谢您的帮助!

4

1 回答 1

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;
于 2012-09-24T22:03:28.613 回答