一个textBox类:按任意键时将其显示 [英] a textBox class : when press any key show it

查看:66
本文介绍了一个textBox类:按任意键时将其显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class aDecimal1 : TextBox
{
      //.......
}



你好.
我要
一个textBox类:按任意键时显示它
可能吗???
怎么样???
:)



hello.
i want
a textBox class : when press any key show it
is it possible???
how???
:)

推荐答案

OriginalGriff Solution 1中所指出的,每次按下键时都会显示MessageBox TextBox中键入文本非常困难.而是创建一个Label 并显示在该Label 上按下的键,如下所示:


As already pointed out in Solution 1 by OriginalGriff, showing a MessageBox every time a key is pressed makes it very difficult to type the text in TextBox. Instead create a Label and show the key pressed on that Label as shown below:


using System;
using System.Windows.Forms;
using System.Drawing;

namespace KeyPressDisplayTextBox {
    public partial class Form1 : Form {
        private TextBox textBox1;
        private Label label1;

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            textBox1 = new TextBox();
            textBox1.Location = new Point(10,10);
            textBox1.KeyPress += textBox1_KeyPress;
            Controls.Add(textBox1);

            label1 = new Label();
            label1.Location = new Point(10, 40);
            label1.BorderStyle = BorderStyle.FixedSingle;
            label1.Font = new Font("Arial", 14);
            Controls.Add(label1);
        }

        void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
            label1.Text = e.KeyChar.ToString();
        }
    }
}


要运行上述代码,请在C#中用名称KeyPressDisplayTextBox创建一个Windows Forms application,在设计器窗口中双击Form1 ,这将打开代码文件,然后用上述代码替换Form1.cs的内容并运行该应用程序.


To run the above code, create a Windows Forms application in C# with the name KeyPressDisplayTextBox , double click on Form1 in the designer window which opens the code file, then replace the contents of Form1.cs with the above code and run the application.


处理TextBox.KeyPress事件-然后,您可以使用它进行所需的操作.

但是,如果您在每次按键时都开始显示MessageBox,则您的用户可能会追捕您并杀死您! :laugh:
Handle the TextBox.KeyPress event - you can then do what you want with it.

But if you start showing a MessageBox on every key press, your users will probably hunt you down and kill you! :laugh:


这篇关于一个textBox类:按任意键时将其显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆