2017-03-27 65 views
0

我試圖讓應用程序導航到Web瀏覽器組件中的某個URL,一旦ENTER鍵被按下(條形碼閱讀器將數據輸入到條形碼文本框並編程爲添加每次掃描結束時按「ENTER」)。然後條形碼號碼通過URL發送到服務器進行驗證,結果顯示在Web瀏覽器中。C#Keydown事件不起作用

在閱讀Stack Overflow之後,我得出結論:this.KeyPreview尚未設置 - 但是,即使設置爲True,它仍然不會。

我希望它做的是當在Form2上的任何地方按下ENTER時,它應該將WebBrowser組件URL更改爲Barcode文本框中包含的數據。

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; 

namespace SUBWAY_deliverX_2017_POSlink 
{ 
    public partial class Form2 : Form 
    { 
     public Form2() 
     { 
      InitializeComponent(); 
      this.ActiveControl = Barcode; 
     } 

     private void Form2_Load(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Close(); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 

     } 

     private void Barcode_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void Barcode_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode == Keys.Enter) 
      { 
       webBrowser1.Navigate("https://10.0.0.142/deliverXPOSquery.php?barcode=" + Barcode.Text); 
      } 
     } 
    } 
} 

而且設計文件:

namespace SUBWAY_deliverX_2017_POSlink 
{ 
    partial class Form2 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form2)); 
      this.button1 = new System.Windows.Forms.Button(); 
      this.Barcode = new System.Windows.Forms.TextBox(); 
      this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
      this.webBrowser1 = new System.Windows.Forms.WebBrowser(); 
      this.button2 = new System.Windows.Forms.Button(); 
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 
      this.SuspendLayout(); 
      // 
      // button1 
      // 
      this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.button1.Location = new System.Drawing.Point(177, 241); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(249, 79); 
      this.button1.TabIndex = 0; 
      this.button1.Text = "Return to SubShop"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      // 
      // Barcode 
      // 
      this.Barcode.BackColor = System.Drawing.SystemColors.Control; 
      this.Barcode.BorderStyle = System.Windows.Forms.BorderStyle.None; 
      this.Barcode.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.Barcode.Location = new System.Drawing.Point(178, 323); 
      this.Barcode.Name = "Barcode"; 
      this.Barcode.Size = new System.Drawing.Size(249, 24); 
      this.Barcode.TabIndex = 1; 
      this.Barcode.TextChanged += new System.EventHandler(this.Barcode_TextChanged); 
      // 
      // pictureBox1 
      // 
      this.pictureBox1.Image = global::SUBWAY_deliverX_2017_POSlink.Properties.Resources.delxstoreheader; 
      this.pictureBox1.Location = new System.Drawing.Point(0, -2); 
      this.pictureBox1.Name = "pictureBox1"; 
      this.pictureBox1.Size = new System.Drawing.Size(589, 102); 
      this.pictureBox1.TabIndex = 3; 
      this.pictureBox1.TabStop = false; 
      // 
      // webBrowser1 
      // 
      this.webBrowser1.Location = new System.Drawing.Point(12, 106); 
      this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); 
      this.webBrowser1.Name = "webBrowser1"; 
      this.webBrowser1.Size = new System.Drawing.Size(563, 120); 
      this.webBrowser1.TabIndex = 4; 
      this.webBrowser1.Url = new System.Uri("https://10.0.0.142/deliverXPOSquery.php", System.UriKind.Absolute); 
      // 
      // button2 
      // 
      this.button2.Location = new System.Drawing.Point(471, 256); 
      this.button2.Name = "button2"; 
      this.button2.Size = new System.Drawing.Size(75, 23); 
      this.button2.TabIndex = 5; 
      this.button2.Text = "button2"; 
      this.button2.UseVisualStyleBackColor = true; 
      this.button2.Click += new System.EventHandler(this.button2_Click); 
      // 
      // Form2 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(587, 346); 
      this.ControlBox = false; 
      this.KeyPreview = true; 
      this.Controls.Add(this.button2); 
      this.Controls.Add(this.webBrowser1); 
      this.Controls.Add(this.pictureBox1); 
      this.Controls.Add(this.Barcode); 
      this.Controls.Add(this.button1); 
      this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); 
      this.MaximumSize = new System.Drawing.Size(603, 385); 
      this.MinimumSize = new System.Drawing.Size(603, 385); 
      this.Name = "Form2"; 
      this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide; 
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
      this.Text = "deliverX 2017 - Order Validation"; 
      this.TopMost = true; 
      this.Load += new System.EventHandler(this.Form2_Load); 
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.Button button1; 
     private System.Windows.Forms.TextBox Barcode; 
     private System.Windows.Forms.PictureBox pictureBox1; 
     private System.Windows.Forms.WebBrowser webBrowser1; 
     private System.Windows.Forms.Button button2; 
    } 
} 
+0

您應該只包含重現問題所需的最短代碼。不要在此發佈所有程序。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。請從問題 –

回答

0

在您的代碼:

this.Barcode.BackColor = System.Drawing.SystemColors.Control; 
      this.Barcode.BorderStyle = System.Windows.Forms.BorderStyle.None; 
      this.Barcode.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.Barcode.Location = new System.Drawing.Point(178, 323); 
      this.Barcode.Name = "Barcode"; 
      this.Barcode.Size = new System.Drawing.Size(249, 24); 
      this.Barcode.TabIndex = 1; 
      this.Barcode.TextChanged += new System.EventHandler(this.Barcode_TextChanged); 

在任何時候你有鏈接的的keydown代碼,你認爲你做了控制。所以沒有它通常。

個人在我自己的條形碼閱讀應用程序中,我使用了更改後的文本,並找到符合規格的條形碼,然後自動提交了它..但我的是用於磁帶掃描,因此它們可以掃描250多個磁帶,並且不會想要每次按一個按鈕

+0

中刪除所有不必要的代碼感謝 - 我仍然無法鏈接到鏈接到控件文本框需要做什麼。我試過'this.Barcode.KeyDown + = new System.EventHandler(this.Form2_KeyDown);'但是這仍然不起作用 – Rick

+0

,因爲你有Barcode_KeyDown作爲你的方法...... – BugFinder

+0

啊,我看到謝謝! – Rick