You have to declare your own event in the UserControl and fire the event when necessary. Using your example:
public event EventHandler ButtonClicked;
protected override OnButtonClicked(EventArgs e)
{
var hand = ButtonClicked;
if (hand != null)
hand(this, e);
}
private void CButton_Click(Object sender, EventArgs e)
{
txt = "C";
OnButtonClicked(new EventArgs());
}
Then you can subscribe to the UserControl's ButtonClicked
event from other code.
A better way, however, might be to make your own EventArgs and pass the string in the event itself instead of just storing the last keypress in a field. Something like the following:
public KeyboardEventArgs : EventArgs
{
public KeyboardEventArgs()
:base()
{
}
public KeyboardEventArgs(char Key)
:this()
{
this.Key = Key;
}
char Key {get; set;}
}
public event EventHandler<KeyboardEventArgs> ButtonClicked;
protected override OnButtonClicked(KeyboardEventArgs e)
{
var hand = ButtonClicked;
if (hand != null)
hand(this, e);
}
private void CButton_Click(Object sender, EventArgs e)
{
OnButtonClicked(new KeyboardEventArgs("C"));
}