-4

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Rocky
  • 11
  • 3

1 Answers1

0

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}");
}
  • https://prnt.sc/lsn8z6 Check link, shows error at private, sender and KeyEventArgs and 1st code only shows "Can't recognize command" when i press button – Rocky Dec 09 '18 at 12:07
  • start() is just a simple text i type in txtConsoleIn box. Let's just consider i want type in textbox start() and then press Enter Key the button should do this process when pressed! – Rocky Dec 09 '18 at 12:25
  • @Rocky There is error because you put my whole second code sample inside your button2_click. But you must put it inside root of your Form class. And rename my button1_Click to your button2_Click. My textBox1_KeyUp to your txtConsoleIn_KeyUp. Method ExecuteCommands must be in root of Form class as it is – Leonid Dudnik Dec 09 '18 at 15:07
  • @Rocky here is how it should look, plus all your other code somewhere [link](http://prntscr.com/lsoz8l) – Leonid Dudnik Dec 09 '18 at 15:19
  • Yes but what is ExecuteCommand(string command) – Rocky Dec 10 '18 at 03:38
  • I modified my question please check again earlier i said i want to type command in textbox just forget that part i want to type text start() and Enter Key by button. – Rocky Dec 10 '18 at 03:41
  • @Rocky "ExecuteCommands(string command)" - it's a method where you can determine command entered by user inside your textbox and execute your code for that command. You want button to type text inside textbox and pass "Enter Key" to textbox? But why? If it's your application, you can put text inside textbox by simple txtConsoleIn.Text="start()"; and just call needed method from button, without need to pass "Enter" key pressed. There is not enough information in your question, it's hard to understand and help – Leonid Dudnik Dec 10 '18 at 11:56
  • yeah but i also required to type other commands too for start i want button! – Rocky Dec 11 '18 at 09:52
  • my question is to just type text and press enter that code i want for button, just forget that it is command i want to type simple text in textbox then enter – Rocky Dec 11 '18 at 10:03
  • @Rocky Look at this, it might be what you're looking for [link](https://stackoverflow.com/questions/1264227/send-keystroke-to-other-control) – Leonid Dudnik Dec 11 '18 at 10:07
  • yes i think that's what i want can you please make code for me i don't know much in coding. i request you! that's why i m here asking – Rocky Dec 11 '18 at 11:19
  • @Rocky I updated answer. If you already have txtConsoleIn_KeyUp in your code with some logic, you should leave it as it is, but I don't know it so it's all on you to decide – Leonid Dudnik Dec 12 '18 at 06:33
  • Leonid Dudnik Sir thanks a lot it worked! :) i used 2nd method. – Rocky Dec 14 '18 at 05:01
  • now i want to paste code by clicking button into textbox, what will be the code for it and i also tried txtConsoleIn.Text = " my whole code "; but getting error on my c# application form page :P – Rocky Jan 27 '19 at 14:05