Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
public class aDecimal1 : TextBox
{
      //.......
}


hello.
i want
a textBox class : when press any key show it
is it possible???
how???
:)
Posted
Comments
Sandeep Mewara 16-May-12 12:08pm    
Not clear. Please use 'Improve question' link and re-phrase.
Clifford Nelson 16-May-12 12:09pm    
Do you mean that when on the form, and a key is pressed, you want a text box to appear, or something else. I assume you are using WinForm

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:


C#
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();
        }
    }
}

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.
 
Share this answer
 
v2
Comments
Wendelius 16-May-12 13:09pm    
Nice example, 5.
VJ Reddy 16-May-12 13:17pm    
Thank you, Mika :)
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:
 
Share this answer
 
Comments
VJ Reddy 16-May-12 12:18pm    
Good point 5!
faezun 16-May-12 13:17pm    
:):):)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900