首页 > 解决方案 > KeyDown 对单独的文本框的响应不同

问题描述

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

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

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

有没有人有任何想法?

这是代码。我只包含了前 2 个文本框,因为其余的文本框基本相同。

    // 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   
                }
            }
        }
    }

标签: c#eventstextboxkeydown

解决方案


推荐阅读