C# C语言中的textboxgui编程

C# C语言中的textboxgui编程,c#,winforms,user-interface,textbox,C#,Winforms,User Interface,Textbox,我正在尝试学习C语言中的GUI编程,关于C语言中文本框的默认代码,我有以下问题: 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 WindowsFormsAp

我正在尝试学习C语言中的GUI编程,关于C语言中文本框的默认代码,我有以下问题:

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 WindowsFormsApplication34
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
         // Textbox programming goes here
        }
    }
}
现在,当我想尝试一些与TexBox不同的东西时,可以编写一些类似于此代码的东西

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 WindowsFormsApplication20
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        //
        // Detect the KeyEventArg's key enumerated constant.
        //
        if (e.KeyCode == Keys.Enter)
        {
        MessageBox.Show("You pressed enter! Good job!");
        }
        else if (e.KeyCode == Keys.Escape)
        {
        MessageBox.Show("You pressed escape! What's wrong?");
        }
    }
    }
}

我无法运行代码,因为文本框的状态为

textBox1_KeyDown
而不是默认的

textBox1_TextChanged
现在我的问题是,如何将TextBox事件处理程序从默认事件处理程序更改为其他事件处理程序?

KeyDown和TextChanged是不同的事件


与其双击文本框输入事件代码,不如选择属性中的“事件”选项卡,然后双击要为其编写代码的事件。

我想您要查找的是OnPreviewKeyDown事件。。。它告诉你接下来会发生什么。如果要绕过它的活动,请将Handled属性设置为true

protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
{
   var ue = e.OriginalSource as FrameworkElement;

   if (e.Key == Key.Enter)
   { 
      MessageBox.Show("You pressed enter! Good job!");
      e.Handled = true;   // to tell event stack you've already taken care of this condition
   }
   else if (e.KeyCode == Keys.Escape)
      MessageBox.Show("You pressed escape! What's wrong?");
}

你在说什么?你会遇到什么错误?我无法运行代码,因为文本框的状态是?这意味着什么?你实际上想做什么?只是一种预感,但也许你在visual studio designer中双击了文本框。这创建了一个默认的TextChanged事件。要为文本框注册其他事件,请使用“属性”面板的“事件”窗格,双击要创建的事件。文本框的状态如何?是否有任何错误?单击“属性”窗口中的闪电图标。双击“向下键”。