繁体   English   中英

将按钮与Enter键关联

[英]associate button with enter key

我在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"];

在构造函数和onLoad From事件中,但仍然无法正常工作。 当用户单击按钮时,什么也没有发生。 我必须用鼠标来说明。

您需要将Form的KeyPreview属性设置为True 然后编写一个KeyDown事件以处理如下所示的Form上的Enter键:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM