文本框接受输入/返回

时间:2012-09-19 10:03:31

标签: 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;
       }
    }
}

2 个答案:

答案 0 :(得分:8)

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

public Form1()
{
    InitializeComponent();
    this.AcceptButton = button1;
}

答案 1 :(得分:5)

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

private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
            button1_Click(sender, e);
    }
相关问题