文本框接受输入/返回

lar*_*400 2 c# winforms

我有下面的代码,允许用户写入可执行文件(即notepad.exe),然后单击开始按钮,它将启动该过程.

但是,如何让文本框接受输入/返回键?我投入AcceptsReturn=true但它没有做任何事情.我还在Visual Studio中设置了属性Accept Return = True- 仍然没有.

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;
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

Bot*_*000 8

AcceptButton表单设置为按钮.AcceptsReturn那时你不需要,因为Enter自动触发按钮.

public Form1()
{
    InitializeComponent();
    this.AcceptButton = button1;
}
Run Code Online (Sandbox Code Playgroud)


th1*_*ey3 6

将keydown事件方法添加到textBox1并在方法内部执行此操作

private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
            button1_Click(sender, e);
    }
Run Code Online (Sandbox Code Playgroud)