KeyDown对单独的textBox响应不同

时间:2018-04-28 19:49:41

标签: c# events textbox keydown

如果奇怪它会发生在我身上。

我正在编写一个C#Windows窗体应用程序(VS 2010),它包含多个文本框以及所有文本框的多个标签。除文本框名称外,操作都是相同的(复制并粘贴,然后替换正确的TextBox名称。它们都包含TextChange事件和KeyDown事件.KeyDown事件是捕获Return键并处理数据。

市集部分是第一部分(区域工作频率)正常工作。所有其他人永远不会进入KeyDown事件。我查看并比较了所有TextBoxes的属性,我看到没有区别。我已经尝试移动这些部分以查看它是否是代码中的顺序。没有什么变化。工作频率始终有效。没有别的。我已经尝试过我能想到的一切。我甚至尝试过从KeyDown到KeyUp没有任何区别;

有没有人有任何想法?

这是代码。我只包括前两个文本框,其余的基本相同。

    // Constants
    public static readonly double rad = Math.PI / 180, DEG = 180 / Math.PI;   // PI to RAD
    public const double solM = 299792458;            // Speed of light in meters/sec
    public const double solMm = solM / 1000;         // Speed of light in mega meters/sec
    public const double solFt = 983571056.43;       // Speed of light in feet/sec
    public const double ft2mtr = 0.3048;               // Convert Feet to Meters

    // Parameters
    public static double D = 60;                            // Loop diameter
    public static double C = D*Math.PI;                  // Loop Circumfrence
    public static double conductorD = 0.375;          // Conductor diameter
    public static double RL = 0;                            // Added loss resistance 
    public static double xmitP = 25;                      // RF xmitter power
    public static double freq = 14.1;                     // Frequence


    public MagLoopAntenna()
    {
        InitializeComponent();

        // Start off with some default parameter values
        tbFreq.Text = "14.1";                        // Target Frequency
        tbLoopDia.Text = "60";                      // Antenna Loop Diameter
        tbUnitsLoopDia.Text = "in";             
        tbConductorDia.Text = "0.5";             // Conductor Diameter
        tbUnitsConductorDia.Text = "in";        // Conductor Diameter Units
        tbAddedLossR.Text = "0";                 // Added Loss in ohms
        tbRfPower.Text = "25";                     // RF Power in Watts
    }

    private bool nonNumberEntered = false;  // Command Key Flag

    #region Operating Frequency
    private void tbFreq_TextChanged(object sender, EventArgs e)
    {
        int test;
        test = 0;           // place for breakpoint
    }
    private void tbFreq_KeyDown(object sender, KeyEventArgs e)
   {
        // Initialize the flag to false.
        nonNumberEntered = false;
        // Determine whether the keystroke is a number from the top of the keyboard.
        if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
       {
            // Determine whether the keystroke is a number from the keypad.
            if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
            {
                // Determine whether the keystroke is a backspace.
                if (e.KeyCode != Keys.Back)
                {
                    // A non-numerical keystroke was pressed.
                    // Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = true;
                }

                if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
               {
                   // Note: may want calculate everything at this point.
                   // We got here now move on to the next textbox 
                   freq = Convert.ToDouble(tbFreq.Text);
                   tbLoopDia.Focus();
                }
            }
         }
    }
    #endregion

    #region Loop Diameter
    private void tbLoopDia_TextChanged(object sender, EventArgs e)
    {
        int test;
        test = 0;   // Just a place to put a breakpoint
    }
    private void tbLoopDia_KeyDown(object sender, KeyEventArgs e)
    {
        // Initialize the flag to false.
        nonNumberEntered = false;
        // Determine whether the keystroke is a number from the top of the keyboard.
        if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
        {
            // Determine whether the keystroke is a number from the keypad.
            if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
            {
                // Determine whether the keystroke is a backspace.
                if (e.KeyCode != Keys.Back)
                {
                    // A non-numerical keystroke was pressed.
                    // Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = true;
                }
                if (e.KeyCode == Keys.Enter)
                {
                    int gothere;
                    gothere = 1;        // Just for a breakpoint

                    // Note: may want calculate everything at this point.
                    // We got here now move on to the next textbox 
                    //tbConductorDia.Focus();          // will implement this once its' working   
                }
            }
        }
    }

0 个答案:

没有答案