I have two trackbars which do the same thing, except one is for the left actuator and one is for the right. I want to add to my GUI the option to sync the trackbars so that the user can choose to control both actuators the same way. Right now, the problem is that the trackbars will sync the first time I try, but then both of the handles freeze. Here is my code:
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
}
}
I appreciate your help!
Your problem is simply that when you are moving slider1, you are setting it’s value to be the value of slider2 in your code; instead, you should move slider1 then set the value of slider2;
Same for Slider2:
You just have your synchronization logic the wrong way round!
Tested using Winforms, VS2010.
EDIT:
Note you could actually use a single event handler for both sliders, something like:
which you can wire up in the designer, or in code (in the constructor or Load event) like: