views:

130

answers:

1

hello, ive created a user control keyboard.. when you click on abutton in the keyboard the button_click event is thrown and a string is get a parameter example when clickin on button C CButton_Click { txt="C"; }

i need to catch that event outside the user control... so that when clickn on the button a textbox is filled with the string... giving the impression the texbox is fillig up while the user clicks on the buttons..

this is for a touchscreen application..

+3  A: 

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"));
}
lc
+1 for the custom eventargs approach
Fredrik Mörk
Good answer. :-)
Cerebrus
Thanks, i did as you told.. and cath the string directly.. saved some memory.. and now it works
Dabiddo