如何将数据从事件传递到其他形式 [英] How to passing data from Event to other form

查看:156
本文介绍了如何将数据从事件传递到其他形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我创建了一个"UserControl"库,该库具有一些接口按钮,每个按钮都包含一些作为属性的数据,并且所有按钮都使用一个事件,有人认为它显示消息框内容是存储的数据在界面按钮中.

现在,我想将此数据从"Usercontrol"传递到在其上运行"UserControl"的主应用程序.

我该怎么做?

接口按钮代码:

Hi guys

I create a "UserControl" library that''s have some interfaced buttons each button content some data as propertys and all buttons use one event that''s do one think it''s show message box content the data that''s stored in interfaced buttons.

Now i want to passing this data from "Usercontrol" to Main application that''s "UserControl" run on it.

How could i do this ??

Interfaced button code:

public class IButtons : System.Windows.Forms.Button
{

    #region Private data memory

    private Guid _Id;
    private Product_data _iproduct;

    #endregion

    private Size defualt_Bsize = new Size(156, 68); //Defualt button size

    public Guid Button_id { get { return _Id; } set { _Id = value; } }

    public Product_data IProduct { get { return _iproduct; } set { _iproduct = value; } }

    public IButtons(Guid Id, Color Button_color, Size Button_size, Product_data Product)
    {
        //Set button Text property as the product name
        this.Text = Product.Name;

        //Button color
        Color color = Button_color;
        if (color == null || color.IsEmpty)
            this.BackColor = Color.FromName("Control");
        else
            this.BackColor = color;

        //Button size
        Size b_size = Button_size;
        if (b_size == null || b_size.IsEmpty)
            this.Size = defualt_Bsize;
        else
            this.Size = b_size;

        //Button Id
        if (Id == null || Id == Guid.Empty)
            _Id = Guid.NewGuid();
        else
            _Id = Id;

        _iproduct = Product; //Load product to private memory

        this.FlatStyle = System.Windows.Forms.FlatStyle.Flat; //Convert button style from standard to flat

        this.Click += new EventHandler(ClickOnButton_event); //Load click event to button
    }

    private void ClickOnButton_event(object sender, EventArgs e)
    {
        System.Windows.Forms.MessageBox.Show(string.Format("Produt Id {0} / Name {1} / Retail Price {2}", _iproduct.Id, _iproduct.Name, _iproduct.Price));
    }

    private void InitializeComponent()
    {
        this.SuspendLayout();
        this.ResumeLayout(false);
    }

}



我想用传递数据的代码替换下面的代码



I want to replace the code below with passing data code

System.Windows.Forms.MessageBox.Show(string.Format("Produt Id {0} / Name {1} / Retail Price {2}", _iproduct.Id, _iproduct.Name, _iproduct.Price));

推荐答案

这是有关表单协作的流行问题.严格来说,它与事件本身无关,而关乎事件处理程序的作用.

最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [ http://en.wikipedia.org/wiki/Accidental_complexity [ http://en.wikipedia.org/wiki/Loose_coupling [
This is the popular question about form collaboration. Strictly speaking, it is unrelated to event themselves, it is all about what your event handler does.

The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


由于该问题很受欢迎,而且我以前的答案通常不太清楚,可能不够清晰,所以我决定写提示/技巧文章,其中包含详细的代码示例和解释:许多问题已回答一次— Windows窗体或WPF Windows之间的协作.

—SA
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA



在主应用程序中声明一个属性,然后通过在用户控件中设置此属性的值,将数据传递给MainApplication.

类MainApllication
{
公共GetData {get;设置;}
}

类Usercontrol
{
MainApplication obj = new MainApplication();
obj.GetData =需要传递给MainApplication的数据";
}
Hi,
Declare one Property in your main application and pass your data to MainApplication by setting value this property in your user control.

Class MainApllication
{
public GetData {get; set;}
}

Class Usercontrol
{
MainApplication obj = new MainApplication();
obj.GetData = "Data that needs to be passed to MainApplication";
}


这篇关于如何将数据从事件传递到其他形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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