Hello,
I have a number NumericUpDown controls, each with its matching TrackBar control.
I need them to work together, meaning changing one will set the other.
If I use explicit control names for both, I can easily make it work.
I want now to use the control names as variables (to enable the use of one method for several control pairs by foreach iteration through their names).
When I use variable strings as the names, the TrackBar does not have the Value property.
It seems as if the the program does not know the Control in the foreach loop is a trackbar, refering to it as a general Control.
Does anyone know how to make it work?
Attached is a short working code example.
To make it work, build a form with one NumericUpDown named NumericUpDown and one trackbar named TrackbarUpDown.
Thanks
Sam
using System; using System.Windows.Forms; //To make it work, build a form with one NumericUpDown named NumericUpDown //and one trackbar named TrackbarUpDown. namespace Trackbar { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { NumericUpDown numUpDn = (NumericUpDown)sender; string tBarName = numUpDn.Name.Replace("Numeric", "Trackbar"); foreach (Control tbar in Controls) { if (tbar.GetType() == typeof(TrackBar)) { if (tbar.Name == tBarName) { //The next will NOT work. The tbar does not have the Value property! tbar.Value = numUpDn.Value; } } } } private void trackBar1_Scroll(object sender, EventArgs e) { TrackBar tbar = (TrackBar)sender; string numUpDnName = tbar.Name.Replace("Trackbar","Numeric"); //Changing names foreach (Control numupdn in Controls) { if (numupdn.GetType() == typeof(NumericUpDown)) { if (numupdn.Name == numUpDnName) { numupdn.Text = tbar.Value.ToString(); } } } } } }
|