【问题标题】:Work with an array of elements on the form (own class)使用表单上的元素数组(自己的类)
【发布时间】:2020-06-13 11:10:58
【问题描述】:

下午好。 我开始在这里讨论这个问题,但我认为这个话题值得单独提出一个问题,因为我无法“从滑动中”找到答案。 Panee was discussed at StaskOverflov 多亏了这个决定,对类进行了更改 (new full code)。

现在类的改变类型如下:

    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,感谢您的帮助。

我的问题是这样的。 此类元素的形式很多(12 件)。它们中的每一个都应在单独的标签上显示自己的值。我正在尝试学习如何在表单上创建相同类型的元素数组,而它们属于自己的类或标准(据我了解,这很重要),即您需要 2 个解决方案,将值分配给他们,从非标准字段中读取值(我的班级有“作者”字段)。

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

准备发送任何附加组件。

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

【问题讨论】:

  • 您可以将现有的 ProgressBar 添加到 List : List bar = new List() {progressBar1, progressBar2, progressBar3};

标签: c# .net forms class frameworks


【解决方案1】:

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

➤ 添加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);
    }
}

➤ 重建。

➤ 在窗体中,选择Seasonality_ProgressBar 的一个实例并切换到属性窗口,您将看到列为ValueControl 的新属性。

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

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

➤ 运行并尝试。

【讨论】:

  • 非常感谢!你是最棒的!
  • @КириллГуреев 最欢迎的朋友,你是最善良的。
【解决方案2】:

试试这样的

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



    }
}

【讨论】:

  • 非常感谢您提供的示例。从创建表单时的代码来看,您将所有组件都放在上面。我的问题是:如果已经有一个使用表单设计器渲染的表单,那么在启动时,是否可以将其上的组件读入数组并在循环中更改它们的属性,就像数组的组件一样?
  • 是:列表 条=新列表() { bar1, bar2, bar3};
猜你喜欢
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 2023-03-20
  • 2015-05-28
  • 2021-05-12
  • 1970-01-01
  • 2013-09-22
相关资源
最近更新 更多