如何从 C# 中的控制数组读取数据

发布于 2024-09-28 07:58:00 字数 4026 浏览 6 评论 0原文

我有以下轨迹栏控件,已添加到选项卡页“tab1”中:

               TrackBar[] tbs = new TrackBar[nooftbsneeded];


               // 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";
                    }


                };

                tbs[i] = tbx;
                tab1.Controls.Add(tbs[i]);

现在如何获取轨迹栏的各个值?

我已经尝试了我能想到的所有调用组合。我只是不知道如何引用单独的轨迹栏控件。

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

               TrackBar[] tbs = new TrackBar[nooftbsneeded];


               // 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";
                    }


                };

                tbs[i] = tbx;
                tab1.Controls.Add(tbs[i]);

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身骄傲 2024-10-05 07:58:00

确保这是在您的类级别声明的:

TrackBar[] tbs;

然后,在您的方法中,只需初始化它(无需重新声明):

tbs = new TrackBar[nooftbsneeded];
// ... Create track bars

此时,您稍后可以通过以下方式读取值:

var value = tbs[3].Value;

Make sure that this is declared at your class level:

TrackBar[] tbs;

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

tbs = new TrackBar[nooftbsneeded];
// ... Create track bars

At that point, you can later read values via:

var value = tbs[3].Value;
为你鎻心 2024-10-05 07:58:00
foreach(var control in tab1.Controls){

    if(control.GetType() == typeof(TrackBar)){
       object value = ((TrackBar)control).Value; 
    }
}
foreach(var control in tab1.Controls){

    if(control.GetType() == typeof(TrackBar)){
       object value = ((TrackBar)control).Value; 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文