C# 对于不同的文本框,按键响应不同

C# 对于不同的文本框,按键响应不同,c#,events,textbox,keydown,C#,Events,Textbox,Keydown,如果这是奇怪的,它会发生在我身上 我正在编写一个C#Windows表单应用程序(VS2010),它由多个文本框以及多个标签组成。除Textbox名称外,所有操作都是相同的(复制和粘贴,然后替换正确的Textbox名称)。它们都包括TextChange事件和KeyDown事件。KeyDown事件用于捕获返回键并处理数据 集市部分为第一部分(区域运营频率)工作正常。所有其他人都不会进入KeyDown事件。我查看并比较了所有文本框的属性,没有发现任何差异。我试着移动各个部分,看看它是否符合代码中的顺序

如果这是奇怪的,它会发生在我身上

我正在编写一个C#Windows表单应用程序(VS2010),它由多个文本框以及多个标签组成。除Textbox名称外,所有操作都是相同的(复制和粘贴,然后替换正确的Textbox名称)。它们都包括TextChange事件和KeyDown事件。KeyDown事件用于捕获返回键并处理数据

集市部分为第一部分(区域运营频率)工作正常。所有其他人都不会进入KeyDown事件。我查看并比较了所有文本框的属性,没有发现任何差异。我试着移动各个部分,看看它是否符合代码中的顺序。没有任何变化。工作频率始终有效。没有其他变化。我尝试了我能想到的一切。我en试着从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   
                }
            }
        }
    }
//常数
公共静态只读双rad=Math.PI/180,DEG=180/Math.PI;//PI到rad
public const double solM=299792458;//光速(米/秒)
public const double solMm=solM/1000;//光速,单位为兆米/秒
public const double solFt=983571056.43;//光速(英尺/秒)
公共常数双ft2mtr=0.3048;//将英尺转换为米
//参数
公共静态双D=60;//环直径
公共静态双C=D*Math.PI;//循环周长
公共静电双导体ORD=0.375;//导体直径
公共静态双RL=0;//增加了损耗电阻
公共静态双xmitP=25;//射频xmitter功率
公共静态双频=14.1;//频率
公共天线
{
初始化组件();
//从一些默认参数值开始
tbFreq.Text=“14.1”//目标频率
tbLoopDia.Text=“60”//天线环路直径
tbUnitsLoopDia.Text=“in”;
tbConductorDia.Text=“0.5”//导线直径
tbUnitsConductorDia.Text=“in”//导线直径单位
tbAddedLossR.Text=“0”//增加的损耗,单位为欧姆
tbRfPower.Text=“25”//射频功率(瓦特)
}
private bool nonNumberEntered=false;//命令密钥标志
#区域工作频率
私有无效tbFreq_TextChanged(对象发送方,事件参数e)
{
智力测验;
test=0;//断点的位置
}
私有void tbFreq_KeyDown(对象发送方,KeyEventArgs e)
{
//将标志初始化为false。
非numberrentered=false;
//确定击键是否为键盘顶部的数字。
如果(e.KeyCodeKeys.D9)
{
//确定击键是否为键盘上的数字。
if(e.KeyCodeKeys.NumPad9)
{
//确定击键是否为退格。
if(e.KeyCode!=Keys.Back)
{
//按下了非数字按键。
//将标志设置为true并在按键事件中计算。
非numberrentered=真;
}
如果(e.KeyCode==键。输入| | e.KeyCode==键。选项卡)
{
//注意:此时可能需要计算所有内容。
//我们到了现在转到下一个文本框
freq=Convert.ToDouble(tbFreq.Text);
tbLoopDia.Focus();
}
}
}
}
#端区
#区域环直径
私有void tbLoopDia_TextChanged(对象发送方,事件参数e)
{
智力测验;
test=0;//只是放置断点的位置
}
私有void tbLoopDia_KeyDown(对象发送方,KeyEventArgs e)
{
//将标志初始化为false。
非numberrentered=false;
//确定击键是否为键盘顶部的数字。
如果(e.KeyCodeKeys.D9)
{
//确定击键是否为键盘上的数字。
if(e.KeyCodeKeys.NumPad9)
{
//确定击键是否为退格。
if(e.KeyCode!=Keys.Back)
{
//按下了非数字按键。
//将标志设置为true并在按键事件中计算。
非numberrentered=真;
}
如果(e.KeyCode==Keys.Enter)
{
int到达这里;
gothere=1;//仅用于断点
//注意:此时可能需要计算所有内容。
//我们到了现在转到下一个文本框
//tbConductorDia.Focus();//将在其“正常工作”后实现它
}
}
}
}

除了“按下键”之外,您是否还可以添加一些额外的标记?“复制并粘贴”肯定会导致该问题。这会使您忘记订阅活动对于那些其他控件。你不必使用设计器来订阅事件,在表单构造函数中编写一行代码也可以。Hans,我怀疑你完全疯了。我决定重新开始,以避免任何被忽略的步骤。目前我有一个包含TextChange和KeyDown事件的文本框,我甚至无法获得该文本框