将字符串从类发送到Form1

时间:2010-03-25 18:35:09

标签: c# winforms instantiation

虽然有一些类似的问题,但我很难找到如何从课堂上接收表格数据的答案。

我一直在尝试阅读有关实例化的内容,它实际上是对我有用的少数事情之一:)但如果我要实例化我的表单,我会不会有两个表单对象?

为了简化操作,假设我在Class1中有一些数据,我想将一个字符串传递给Form1上的标签。实例化另一个form1是合法的吗?当尝试这样做时,我看起来可以访问label1.Text,但标签没有更新。我唯一能想到的是需要重新绘制表单或者有一些我不知道的线程问题。

非常感谢您提供的任何见解。

编辑:添加了一些代码来帮助

class Class1
{
    public int number { get; set; }

    public void Counter()
    {
        for (int i = 0; i < 10; i++)
        {
            number = i;
            System.Threading.Thread.Sleep(5000);
        }
    }
}

和表格:

    Class1 myClass = new Class1();

    public Form1()
    {
        InitializeComponent();
        label1.Text = myClass.number.ToString();
    }

4 个答案:

答案 0 :(得分:3)

我认为您的表单需要访问Class1的引用才能使用中的可用数据,而不是

非常数据类应该显示一个显示表单以显示它所提供的内容。

如果该类是单例的数据访问层,则表单应引用该类,而不是相反。

答案 1 :(得分:1)

没有理由不能拥有表单的多个实例,但在这种情况下,您希望将Class1实例传递给您拥有的表单。最简单的方法是向Form1添加一个属性,并更新标签文本:

public class Form1 : Form
{
    public Class1 Data
    {
        set
        {
            this.label.Text = value.LabelText;
        }
    }
}

答案 2 :(得分:1)

根据您原始帖子中的评论,以下是一些方法: (此代码基于VS2010中的Winform应用程序,有1个表单,名为“ValueLabel”的Label,名为“NewValueTextBox”的文本框和按钮。

此外,还有一个“MyClass”类,它表示要发布更改的类。

这是该课程的代码。请注意,我实现了INotifyPropertyChanged并且我有一个事件。您不必实现INotifyPropertyChanged,并且只要属性发生变化,我就会引发一个事件。

您应该能够运行表单,在文本框中键入内容,单击按钮,然后查看标签更改的值。


    public class MyClass: System.ComponentModel.INotifyPropertyChanged
    {
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        public string AValue
        {
            get
            {
                return this._AValue;
            }
            set
            {
                if (value != this._AValue)
                {
                    this._AValue = value;
                    this.OnPropertyChanged("AValue");
                }
            }
        }
        private string _AValue;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if(this.PropertyChanged != null)
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }

示例1:标签和值之间的简单数据绑定: 所以这次,我们将标签绑定到您的类的实例。我们有一个文本框和按钮,您可以使用它来更改MyClass中的属性值。当它改变时,数据绑定将导致标签自动更新:

注意:确保将Form1_Load连接为hte表单的加载事件,并将UpdateValueButton_Click连接为按钮的单击处理程序,否则什么都不起作用!


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private MyClass TheClass;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.TheClass = new MyClass();
            this.ValueLabel.DataBindings.Add("Text", this.TheClass, "AValue");
        }

        private void UpdateValueButton_Click(object sender, EventArgs e)
        {
            // simulate a modification to the value of the class
            this.TheClass.AValue = this.NewValueTextBox.Text;
        }
    }

示例2 现在,让我们将class的值直接绑定到文本框。我们已经在按钮单击处理器中注释掉了代码,我们已将文本框和标签绑定到对象值。现在,如果您只是远离文本框,您将看到您的更改......


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private MyClass TheClass;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.TheClass = new MyClass();
            // bind the Text property on the label to the AValue property on the object instance.
            this.ValueLabel.DataBindings.Add("Text", this.TheClass, "AValue");
            // bind the textbox to the same value...
            this.NewValueTextBox.DataBindings.Add("Text", this.TheClass, "AValue");
        }

        private void UpdateValueButton_Click(object sender, EventArgs e)
        {
            //// simulate a modification to the value of the class
            //this.TheClass.AValue = this.NewValueTextBox.Text;
        }


示例3 在这个例子中,我们根本不会使用数据绑定。相反,我们将在MyClass上挂钩属性更改事件并手动更新。

注意,在现实生活中,您可能有一个比Property更改的更具体的事件 - 您可能更改了AValue,只有在该属性更改时才会引发。


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private MyClass TheClass;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.TheClass = new MyClass();
            this.TheClass.PropertyChanged += this.TheClass_PropertyChanged;
        }

        void TheClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "AValue")
                this.ValueLabel.Text = this.TheClass.AValue;
        }

        private void UpdateValueButton_Click(object sender, EventArgs e)
        {
            // simulate a modification to the value of the class
            this.TheClass.AValue = this.NewValueTextBox.Text;
        }
    }

答案 3 :(得分:0)

您想在此处使用事件和代理。既然你把它标记为初学者,我将把实现作为练习:),但这里是一般的想法:

  1. 在数据类中声明事件/委托对。
  2. 在表单中创建一个方法以绑定到该事件。
  3. 当您的数据类中出现“某事”时,触发事件
  4. 将调用表单的方法来处理事件并适当更新小部件
  5. 祝你好运。