Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将按钮与回车键关联_C#_Button_Enter - Fatal编程技术网

C# 将按钮与回车键关联

C# 将按钮与回车键关联,c#,button,enter,C#,Button,Enter,我在C#4.0中得到了一个WinForm项目 我想在用户单击按钮时输入,然后调用此按钮的onclick事件 我的代码: public XtraForm_Main() { InitializeComponent(); ... this.AcceptButton = (Button)this.Controls["button_Valider"]; } private void Main_Load(object sender, Event

我在C#4.0中得到了一个WinForm项目

我想在用户单击按钮时输入,然后调用此按钮的onclick事件

我的代码:

 public XtraForm_Main()
        {
            InitializeComponent();

...
this.AcceptButton = (Button)this.Controls["button_Valider"];
        }

 private void Main_Load(object sender, EventArgs e)
        {
            this.AcceptButton = (Button)this.Controls["button_Valider"];
        }

  private void button_Valider_Click(object sender, EventArgs e)
        {
            try
            {
                using (var connectionWrapper = new Connexion())
                {
                    var connectedConnection = connectionWrapper.GetConnected();
                    string SqlSyntax = "SELECT * FROM ORDRE WHERE REF_EXPED = @REFERENCE";
                    SqlCommand comm_InsUpt = new SqlCommand(SqlSyntax, connectionWrapper.conn);
                    comm_InsUpt.Parameters.AddWithValue("@REFERENCE", textEdit_ref.Text);
                    SqlDataAdapter adapt_SelectAll = new SqlDataAdapter();
                    adapt_SelectAll.SelectCommand = comm_InsUpt;
                    DataSet dSet_SelectAll = new DataSet();
                    adapt_SelectAll.Fill(dSet_SelectAll, "BON_ETIKET");

                    var xtraReport_Pricipal = new Zebra_Web();

                    xtraReport_Pricipal.Parameters["Count_Ordre"].Value = 1;
                    xtraReport_Pricipal.Parameters["IdPacket"].Value = 1;
                    xtraReport_Pricipal.DataSource = dSet_SelectAll;
                    xtraReport_Pricipal.DataMember = dSet_SelectAll.Tables[0].TableName;
                    xtraReport_Pricipal.CreateDocument();

                    xtraReport_Pricipal.PrintingSystem.ShowMarginsWarning = false;
                    xtraReport_Pricipal.PrintingSystem.ContinuousPageNumbering = true;
                    //xtraReport_Pricipal.ShowPreviewDialog();
                    xtraReport_Pricipal.Print(Properties.Settings.Default.Zebra);

                    dSet_SelectAll.Dispose();
                    adapt_SelectAll.Dispose();
                }
            }
            catch (Exception excThrown)
            {
                throw new Exception(excThrown.Message, excThrown);
            }
        }
我试着说:

this.AcceptButton = (Button)this.Controls["button_Valider"];
在构造函数中,从事件加载,但仍不工作。
当用户单击按钮时,不会发生任何事情。我必须用鼠标点击它。

您需要将表单的
KeyPreview
属性设置为
True
。 然后编写一个
KeyDown
事件来处理
Enter
Key on
Form
详情如下:

 private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button_Valider_Click(sender,e);
            }
        }

我想没有必要再打电话了。那就删除这行代码吧。然后单击按钮。单击时应触发单击事件。要在用户按Windows窗体右侧的Enter键时调用button_Valider_click()函数吗?请尝试将其从主加载事件中删除。发生了什么?我猜@Sudhakar是正确的,他希望使用键盘输入键参考此链接,它应该可以解决您的问题: