Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have in my project multiple forms, In the main form i have an instance of an UserControl, a button and a label, when I press the button is open the second form and set in a label a value from the database, press OK from form2, the value in passed to a label on form 1.

How i can get the same value in a label in my instance of usercontrol?
Posted
Updated 17-Jun-11 14:37pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Jun-11 20:19pm    
Add a tag: Forms, so experts new it's not WPF, ASP.NET or something else.
...OK, I've done it for you; next time, always tag such things.

--SA

1 solution

You need to add an event to your UserControl and fire this event when a value is set and confirmed. Let's call this event ValueSet.

C#
public class ValueSetEventArgs : System.EventArgs {
    internal ValueSetEventArgs(string value) { this.fValue = value; }
    public string FValue { get {return fValue; } }
    string fValue;
} //class ValueSetEventArgs

//...

public class MyUserControl {

    //...

    public System.EventHandler<ValueSetEventArgs> ValueSet;
    void SetValue(string value) {
        //do what you already did...
        if (this.ValueSet != null)
            this.ValueSet.Invoke(this, new ValueSetEventArgs(value));
    } //SetValue

} //class MyUserControl


You form #1 should add an event handler to this instance of your MyCustomControl. This event can set the value of your label or whatever else:

C#
class MyForm Form {
    
    public MyForm {
        InitializeComponent();
        MyUserControl.ValueSet += (sender, eventArgs) => {
            MyLabel.Text = eventArgs.Value;
            // or whatever else
        } //MyUserControl.ValueSet
    } //MyForm

    //...

    // normally called in InitializeComponent,
    // I just show it as manual code:
    Label MyLabel = new Label();
    MyUserControl MyUserControl = new MyUserControl();

} //class MyForm


Problem solved.

—SA
 
Share this answer
 
v3
Comments
aciobanita 18-Jun-11 10:32am    
I create a class
<pre>class ClientActiv
{
public static string NumeClient;
public static string ClientID;

}</pre>

On my form2, on accept button

<pre>System.Data.DataTable Table = new System.Data.DataTable();
Program.Connection.CommandText = "select * from DateClientiAmanet where ClientId=" + ClientID.Text;
Program.Connection.FillDataTable(Table, true);

ClientActiv.ClientID = Table.Rows[0][0].ToString();

Program.Connection.ExecuteNonQuery();

this.Close();</pre>

The problem is:

i can update the label on my usercontrol.
how i do?
Sergey Alexandrovich Kryukov 20-Jun-11 1:34am    
This is a different question and it also needs clarification, because -- where is your User Control code?
Will you ask a separate one?
--SA
Sandeep Mewara 19-Jun-11 16:29pm    
My 5+!

:)
Sergey Alexandrovich Kryukov 20-Jun-11 1:34am    
Thank you, Sandeep.
--SA

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