Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
in my User Control i want to add 2 label ,2 text box,2 button.
when i used this control in my application. i want use value of this 2 text box and also i want use 2 button click event in my application means i want 2 use click event of 2 button in my application.is that possible then please give some related example for that.so i can understand how i can make my control for my application.
Posted
Comments
thatraja 8-Aug-11 11:53am    
I think you clicked Vote 1 instead of Vote 5 accidentally, Please re-vote these answers by clicking Vote-5.

You can expose your constituent control properties as properties of the user control, like this:
C#
public string Text1 {
    get {
        return textBox1.Text;
    }
    set {
        textBox1.Text = value;
    }
}


Similarly, you can expose events the textbox as event of the UserControl
C#
public event EventHandler Button1Click;
protected internal void OnButton1Click(EventArgs args) {
    if (Button1Click != null) {
        Button1Click(this, args);
    }
}

//This event handles Button1's Click event
private void Button1_Click(object sender, EventArgs args) {
    OnButton1Click(args);
}
 
Share this answer
 
Comments
Dharmessh12 6-Aug-11 5:07am    
thanks for giving solution. i got some basic idea now .sow now i will do according to your solution
thatraja 6-Aug-11 7:33am    
I think you clicked Vote 1 instead of Vote 5 accidentally, Please re-vote these answers by clicking Vote-5.
[no name] 6-Aug-11 5:11am    
Anyone wants to own up the downvote and explain ?
Your question is in two parts, so I'll answer it in two parts:
Set up two public Properties in your control: one for each textbox. Give them sensible names, which reflect the data they contain, rather than the fact that they are textboxes, such as "Usename" and "Password".
C#
/// <summary>
/// Gets and sets the Username
/// </summary>
public string Username
    {
    get { return textboxUsername.Text; }
    set { textboxUsername.Text = value; }
    }
These will then appear in the Designer as Properties that can be initialized.

Create two events in your control, one for each button - again, give them sensible names such as "OK" and "Cancel":
C#
/// <summary>
/// Signals that the OK button was pressed
/// </summary>
public event EventHandler OK;
private void button1_Click(object sender, EventArgs e)
    {
    EventHandler eh = OK;
    if (eh != null)
        {
        eh(this, e);
        }
    }
 
Share this answer
 
Comments
Dharmessh12 6-Aug-11 5:07am    
thanks for giving solution. i got some basic idea now .sow now i will do according to your solution

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