how do I read data from a control array in C# – Education Career Blog

I have the following trackbar controls which I have added to a tabpage ‘tab1’:

               TrackBar tbs = new TrackBarnooftbsneeded;


               // Add TrackBars
                TrackBar tbx = new TrackBar();
                tbx.Location = new Point(28, 150 + (i * 200));
                tbx.Size = new Size(686, 45);
                tbx.Minimum = 0;
                tbx.Maximum = 16;
                tbx.SmallChange = 1;
                tbx.LargeChange = 2;
                tbx.Value = 8;

                // create events (using a lambda expression) for each trackbar to change values.
                tbx.Scroll += (o, a) =>
                {
                    // Update text values
                    if (tbx.Value == 0)
                    {
                        label3x.Text = "Extremely Better";
                        label4x.Text = "";
                    }
                    if (tbx.Value == 1)
                    {
                        label3x.Text = "Extremely Better";
                        label4x.Text = "";
                    }
                    if (tbx.Value == 2)
                    {
                        label3x.Text = "Very Strongly Better";
                        label4x.Text = "";
                    }
                    if (tbx.Value == 3)
                    {
                        label3x.Text = "Very Strongly Better";
                        label4x.Text = "";
                    }
                    if (tbx.Value == 4)
                    {
                        label3x.Text = "Strongly Better";
                        label4x.Text = "";
                    }
                    if (tbx.Value == 5)
                    {
                        label3x.Text = "Strongly Better";
                        label4x.Text = "";
                    }
                    if (tbx.Value == 6)
                    {
                        label3x.Text = "Moderately Better";
                        label4x.Text = "";
                    }
                    if (tbx.Value == 7)
                    {
                        label3x.Text = "Moderately Better";
                        label4x.Text = "";
                    }
                    if (tbx.Value == 8)
                    {
                        label3x.Text = "Equal";
                        label4x.Text = "Equal";
                    }
                    if (tbx.Value == 9)
                    {
                        label3x.Text = "";
                        label4x.Text = "Moderately Better";
                    }
                    if (tbx.Value == 10)
                    {
                        label3x.Text = "";
                        label4x.Text = "Moderately Better";
                    }
                    if (tbx.Value == 11)
                    {
                        label3x.Text = "";
                        label4x.Text = "Strongly Better";
                    }
                    if (tbx.Value == 12)
                    {
                        label3x.Text = "";
                        label4x.Text = "Strongly Better";
                    }
                    if (tbx.Value == 13)
                    {
                        label3x.Text = "";
                        label4x.Text = "Very Strongly Better";
                    }
                    if (tbx.Value == 14)
                    {
                        label3x.Text = "";
                        label4x.Text = "Very Strongly Better";
                    }
                    if (tbx.Value == 15)
                    {
                        label3x.Text = "";
                        label4x.Text = "Extremely Better";
                    }
                    if (tbx.Value == 16)
                    {
                        label3x.Text = "";
                        label4x.Text = "Extremely Better";
                    }


                };

                tbsi = tbx;
                tab1.Controls.Add(tbsi);

How do I now get the individual values of the trackbars?

I have tried every combination of call I can think of.. I just don’t know how to reference the separate trackbar controls.

,

Make sure that this is declared at your class level:

TrackBar tbs;

Then, in your method, just initialize it (without redeclaring it):

tbs = new TrackBarnooftbsneeded;
// ... Create track bars

At that point, you can later read values via:

var value = tbs3.Value;

,

foreach(var control in tab1.Controls){

    if(control.GetType() == typeof(TrackBar)){
       object value = ((TrackBar)control).Value; 
    }
}

Leave a Comment