2

我有下面的代码,允许用户写入可执行文件(即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;
       }
    }
}
4

2 回答 2

8

AcceptButton表单设置为您的按钮。那你就不需要了AcceptsReturn,因为Enter会自动触发按钮。

public Form1()
{
    InitializeComponent();
    this.AcceptButton = button1;
}
于 2012-09-19T10:08:14.150 回答
6

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

private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
            button1_Click(sender, e);
    }
于 2012-09-19T10:12:52.330 回答