Search code examples
c#keypress

Send keypress as parameters in function


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, ??????)

Solution

  • 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
    }