0

i got a WinForm Project in C# 4.0.

i want to when the user click the button enter then it call onclick event of this button.

my code:

 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);
            }
        }

i have tried to put this line:

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

in constructor and onLoad From event but still not work. when the user click the button it nothing happen. i have to clic it with the mouse.

user1958628
  • 409
  • 4
  • 7
  • 18
  • I think there is not need for any extra call. What about just remove the line and run your code. And click the button. The click event should be fired when you click it. – jhyap Nov 14 '13 at 08:24
  • 1
    you want to invoke button_Valider_Click() function whenever user press Enter Key on Windows Form right? – Sudhakar Tillapudi Nov 14 '13 at 08:24
  • try removing it from the Main_Load event. what happens? – Ahmed ilyas Nov 14 '13 at 08:25
  • 1
    I guess @Sudhakar is correct, he want the Enter Key using keyboard – jhyap Nov 14 '13 at 08:25
  • Refer to this link, it should solve your problem: http://stackoverflow.com/questions/15089513/how-to-call-a-keyboard-key-press-programmaticly – jhyap Nov 14 '13 at 08:29

1 Answers1

1

You need to set KeyPreview Property of your Form to True. and then write a KeyDown Event to Handle Enter Key on Form as Below:

 private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button_Valider_Click(sender,e);
            }
        }
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67