Search code examples
c#desktop-application

Create button that type in textbox "start()" and then press "Enter key"


C# Desktop application.

In my application i want to create a button that automatically type in textbox start() and then press Enter Key of keyboard.

If I want to start process in my application I need to type in textbox "start()" then press "Enter Key" , the button should do this process when pressed. What will be code for this?


Solution

  • Here is what you asking

    private void txtConsoleIn_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode==Keys.Enter)
        {
            // Execute command - your code
            MessageBox.Show("Start()"); // this line just for test
        }
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        txtConsoleIn.Text = "start()";
        txtConsoleIn.Focus();
        SendKeys.Send("{ENTER}");
    }