Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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
C#
System.Windows.Forms.MessageBox.Show(string.Format("Produt Id {0} / Name {1} / Retail Price {2}", _iproduct.Id, _iproduct.Name, _iproduct.Price));
Posted

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
 
Share this answer
 
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
 
Share this answer
 
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";
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900