使用表单上的元素数组(拥有 class)

Work with an array of elements on the form (own class)

下午好。 我在这里开始讨论这个问题,但我认为这个话题值得一个单独的问题,因为我无法“从滑动”中找到答案。 由于这一决定,对 class (new full code) 进行了更改。

现在class修改后的类型如下:

    class Seasonality_ProgressBar : Control

 #region --События--
        public delegate void OnValueChangedEvent(int value);
        public event OnValueChangedEvent OnValueChanged;

#endregion

Stopwatch st = new Stopwatch();
MouseButtons mb = MouseButtons.None;

public int Value
        {
            get => _value;
            set
            {
                if (value >= ValueMinimum && value <= ValueMaximum)
                {
                    _value = value;
                    Invalidate();
                }
                else
                {
                    value = _value;
                    Invalidate();
                }
                OnValueChanged?.Invoke(_value);
            }
        }

public Seasonality_ProgressBar()
{
}

protected override void OnMouseDown(MouseEventArgs e)
{
   // base.OnMouseDown(e);
    if (!st.IsRunning)
    {
        mb = e.Button;
        st.Start();

        if (e.Button == mb)
        {
            x = e.X;
            y = e.Y;
            timer.Elapsed += OnTimedEvent;
            timer.Start();
        }
    }
    base.OnMouseDown(e);
}


 private void OnTimedEvent(Object source, ElapsedEventArgs e)
 {
     timer.Stop();
     //MessageBox.Show("Сработало");
     float reultation = x - y;
     if (reultation > 0)
     {
         Value = "A";
     }
     else
     {
         Value = "B";
     }
 }

现在您可以在表单上订阅活动:

label2.Text = (-seasonality_ProgressBar1.Value).ToString();
seasonality_ProgressBar1.OnValueChanged += SomeEvent;

而SomeEvent方法的种类是:

private void SomeEvent(int value)
{
    //Use Invoke here because your event is called from another thread
    Invoke(new Action(() =>
    {
        label2.Text = (-value).ToString();
    }));
    //File.AppendAllText("log.txt", "seasonality_ProgressBar1_MouseUp\r\n");
}

@M。 Artem,感谢您的帮助。

我的问题是这样的。 关于这个class的表格元素很多(12个)。他们每个人都应该在单独的标签上显示自己的价值。我正在尝试学习如何在表单上创建相同类型的元素数组,而它们是自己的 class 或标准(据我了解,这很重要),即您需要 2 个解决方案,为它们赋值,从非标准字段读取值(我的 class 有 "author" 个字段)。

朋友们,帮我学习,PLIZ。 准备回答所有其他问题。

准备发送任何附加组件。

我是新来的,批评,贬低,支配。

尝试这样的事情

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int ProgressBar_Width = 100;
        const int ProgressBar_Height = 200;
        const int Label_Width = 10;
        const int Label_Height = 20;
        const int Number_Bars = 10;
        const int MARGIN = 20;

        List<Seasonality_ProgressBar> bars = new List<Seasonality_ProgressBar>();

        public Form1()
        {
            InitializeComponent();

            for(int i = 0; i < Number_Bars; i++)
            {
                Seasonality_ProgressBar bar = new Seasonality_ProgressBar();
                bar.Left = MARGIN + i * (ProgressBar_Width + MARGIN);
                bar.Top = MARGIN + MARGIN + Label_Height;
                bar.Width = ProgressBar_Width;
                bar.Height = ProgressBar_Height;
                bar.Name = "Bar_" + i.ToString();
                this.Controls.Add(bar);
                bars.Add(bar);

                bar.highLabel = new Label();
                bar.highLabel.Left = MARGIN + i * (ProgressBar_Width + MARGIN) + (ProgressBar_Width / 2);
                bar.highLabel.Top = MARGIN;
                bar.highLabel.Width = Label_Width;
                bar.highLabel.Height = Label_Height;
                bar.highLabel.Text = i.ToString();
                bar.highLabel.BackColor = Color.Red;
                this.Controls.Add(bar.highLabel);

                bar.lowLabel = new Label();
                bar.lowLabel.Left = MARGIN + i * (ProgressBar_Width + MARGIN) + (ProgressBar_Width / 2);
                bar.lowLabel.Top = MARGIN + MARGIN + ProgressBar_Height + Label_Height + MARGIN;
                bar.lowLabel.Width = Label_Width;
                bar.lowLabel.Height = Label_Height;
                bar.lowLabel.Text = i.ToString();
                bar.lowLabel.BackColor = Color.LightBlue;
                this.Controls.Add(bar.lowLabel);

            }


        }
    }
    public class Seasonality_ProgressBar : ProgressBar
    {
        public Label highLabel { get; set; }
        public Label lowLabel { get; set; }
        public string name { get; set; }



    }
}

您可以将此任务包含在您的自定义控件中,以便在 selected 控件中更改值时显示该值。

➤ 添加 Control 类型的新 属性,例如:

class Seasonality_ProgressBar : Control
{
//...
    public Control ValueControl { get; set; }
//...
}

➤ 在Value 属性 的setter 中,检查ValueControl 是否有值并赋值给Value 属性它是 Text 属性。您需要在此处使用 Invoke 方法,因为您使用的是 System.Timers.Timer 而不是 System.Windows.Forms.Timer!

public int Value
{
    get => _value;
    set
    {
        if (value >= ValueMinimum && value <= ValueMaximum)
        {
            _value = value;
            Invalidate();
        }
        else
        {
            value = _value;
            Invalidate();
        }
        if (ValueControl != null) Invoke(new Action(() => ValueControl.Text = value.ToString()));
        OnValueChanged?.Invoke(_value);
    }
}

➤ 重建。

➤ 在表单中,select Seasonality_ProgressBar 的实例并切换到属性 window,您将看到新的 属性 列为 ValueControl.

➤ Select 从下拉列表中选择要将其与此实例连接以显示其值的控件。说 label1。或者,您可以在代码中设置 ControlValue 属性:

seasonality_ProgressBar1.ValueControl = lable1; //or textBox1 ...etc.

➤ 运行 并尝试。