将值从类传递到窗体控件 [英] Pass value from Class to Form control

查看:68
本文介绍了将值从类传递到窗体控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真让我发疯!

Form1.cs =按btnClass并启动Class1.cs

This is driving me nuts!

Form1.cs = Press btnClass and it starts Class1.cs

public partial class Form1 : Form
{
    private readonly Class1 cls = new Class1();







private void btnClass_Click(object sender, EventArgs e)
{
    Thread clsThread = new Thread(StartClass1);
    clsThread.Start();
}
private void StartClass1()
{
    // Class1.cs
    cls.BeginClass();
}




Class1.cs =发生事情,然后更新label.Text位于Form1中

我尝试使用委托,它可以工作,但是我需要那些委托才能将值从Class1发送到Form1标签. :doh:




Class1.cs = stuff happens then updates label.Text located in Form1

I''ve tried using delegates and it works, but the problem I need those delegates to send the value from Class1 to Form1 label. :doh:

class Class1
{
    public void BeginClass()
    {
        // Needs to pass value back to label created in Form1
        // ClassDone(); - start events
    }
    public event ClassDoneEventHandler ClassDoneEvent;
    private void ClassDone()
    {
        ClassDoneEvent(new ClassDoneEventArgs());
    }
    # region ClassDone
    public delegate void ClassDoneEventHandler(ClassDoneEventArgs e);
    public class ClassDoneEventArgs : EventArgs
    {
    }
    # endregion
}




任何帮助表示赞赏! ;)




Any Help Appreciated!! ;)

推荐答案

您可以将值放入ClassDoneEventArgs对象

You can put the values into the ClassDoneEventArgs object

public class ClassDoneEventArgs : EventArgs    
{    
    public object Value { get; set; }
    public ClassDoneEventArgs(object value)
    {
        this.Value = value;
    }
}



而当您致电活动海报时,请执行以下操作:



And the when you call your event poster, do this:

private void OnMyEvent()
{
   MyEvent(this, new ClassDoneEventArgs(myValue));
}


Genius John Simmons,

你不知道你救了我多少时间.这是所有的代码.

Genius John Simmons,

You have no idea how much time you just saved me. Here''s the code all put together.

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;
using System.Threading;




Form1.cs




Form1.cs

namespace PassData
{
    public partial class Form1 : Form
    {
        private readonly Class1 cls = new Class1();
        public Form1()
        {
            InitializeComponent();
            cls.ClassDoneEvent += new PassData.Class1.ClassDoneEventHandler(class1_ClassDoneEvent);
        }
        private void class1_ClassDoneEvent(PassData.Class1.ClassDoneEventArgs e)
        {
            //e.Value will be SomeValue
            label1.Invoke(new UpdateLabelTextDelegate(UpdateLabelText), e.Value.ToString());
        }
        private delegate void UpdateLabelTextDelegate(string text);
        private void UpdateLabelText(string text)
        {
            // Actual Label updated with text.
            label1.Text = text;
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void btnClass_Click(object sender, EventArgs e)
        {
            Thread clsThread = new Thread(StartClass1);
            clsThread.Start();
        }
        private void StartClass1()
        {
            // Class1.cs
            cls.BeginClass();
        }
    }
}



Class1.cs



Class1.cs

namespace PassData
{
    class Class1
    {
        public void BeginClass()
        {
            ClassDone();
        }
        public event ClassDoneEventHandler ClassDoneEvent;
        private void ClassDone()
        {
            ClassDoneEvent(new ClassDoneEventArgs());
        }
        public delegate void ClassDoneEventHandler(ClassDoneEventArgs e);
        public class ClassDoneEventArgs : EventArgs
        {
            public string Value
            {
                get
                {
                    // The value to be passed to Form1.cs
                    return "SomeValue";
                }
            }
        }
    }
}



apaka,还要感谢您的回应,因此必须对此进行调查.



apaka, appreciate the response as well, going to have to look into that.


dotnetperls.com/backgroundworker尝试使用此功能.
dotnetperls.com/backgroundworker Try using this.


这篇关于将值从类传递到窗体控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆