Replacing character is character is different

This is a discussion on Replacing character is character is different within the C# Programming forums, part of the General Programming Boards category; Replacing character if character is different Hi guys, im new here.. I am doing a wireless transmission with C#. i ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Replacing character is character is different

    Replacing character if character is different

    Hi guys, im new here..

    I am doing a wireless transmission with C#.

    i am now using a textbox, with a max length of 1.
    example upon running the program, i press A. And when I press B, it produces a sound "ding", which i believe that it tells me that the length is full, i have to backspace it and retype the B.

    How can I type this program so that like first I type A, it shows A in the textbox.
    and if the next moment i type something different, it shows the new character, replacing the new one. but if its the same, the old character remains..

    Thanks in advance..
    Last edited by s10062971; 11-04-2010 at 08:38 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Capture the textbox's OnKeyDown event.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    hi.. can i teach me how do i do that?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    How about something like this?
    Code:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                TextBox textbox = new TextBox();
                textbox.Location = new Point(0, 0);
                textbox.MaxLength = 1;
                textbox.KeyDown += new KeyEventHandler(textbox_KeyDown);
                Controls.Add(textbox);
            }
    
            void textbox_KeyDown(object sender, KeyEventArgs e)
            {
                if (!Char.IsControl((char)e.KeyCode))
                    (sender as TextBox).Text = "";
            }
        }
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Quote Originally Posted by itsme86 View Post
    How about something like this?
    Code:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                TextBox textbox = new TextBox();
                textbox.Location = new Point(0, 0);
                textbox.MaxLength = 1;
                textbox.KeyDown += new KeyEventHandler(textbox_KeyDown);
                Controls.Add(textbox);
            }
    
            void textbox_KeyDown(object sender, KeyEventArgs e)
            {
                if (!Char.IsControl((char)e.KeyCode))
                    (sender as TextBox).Text = "";
            }
        }

    HEY! a million thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  3. LoadFromFile() from a txt
    By Snoopy104 in forum C++ Programming
    Replies: 6
    Last Post: 03-14-2006, 10:05 AM
  4. Any help would be appreciated.....
    By SprinterSteve in forum C Programming
    Replies: 6
    Last Post: 05-01-2003, 10:25 AM
  5. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21