问题描述:

I have the below code that allows a user to write in a executable (i.e. notepad.exe) and then clicking the start button it will start the process.

However, how do I make the textbox accept enter/return key? I put in AcceptsReturn=true but it is not doing anything. I have also set in Visual Studio the property Accept Return = True - still nothing.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

namespace process_list

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

string text = textBox1.Text;

Process process = new Process();

process.StartInfo.FileName = text;

process.Start();

}

private void textBox1_TextChanged(object sender, EventArgs e)

{

textBox1.AcceptsReturn = true;

}

}

}

网友答案:

Set the AcceptButton of the form to your button. You don't need AcceptsReturn then, because Enter automatically triggers the button.

public Form1()
{
    InitializeComponent();
    this.AcceptButton = button1;
}
网友答案:

add keydown event method to textBox1 and inside the method do this

private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
            button1_Click(sender, e);
    }
相关阅读:
Top