Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send keypress as parameters in function

Tags:

c#

keypress

I have a combobox, if the "Enter" key is pressed will do something. but i want to call this function manually but how i send "ENTER" key as parameter?

 private void carga_todos(object sender, KeyPressEventArgs e)
    { 
        if (e.KeyChar == (char)13)
        {
           do something
        }

im trying this:

carga_todos(null, ??????)
like image 900
wozzarvl Avatar asked Jun 08 '26 14:06

wozzarvl


2 Answers

Don't call events manually like that.

Move your logic into a separate method, which you can call whenever you like:

private void carga_todos(object sender, KeyPressEventArgs e)
{ 
    if (e.KeyChar == (char)Keys.Enter)
    {
       DoSomething();
    }
}

private void AnotherFunctionThatNeedsToDoSomethingToo()
{
    DoSomething();
}

private void DoSomething()
{
    // stuff to do
}
like image 56
Grant Winney Avatar answered Jun 10 '26 03:06

Grant Winney


I agree with Grant Winney. However, if your requirements are to call the handler directly then the following should work:

KeyPressEventArgs kpea = new KeyPressEventArgs((char)Keys.Enter);
carga_todos(null, kpea);

Good luck J

like image 23
JanuszB Avatar answered Jun 10 '26 02:06

JanuszB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!