Well I have a small problem. I'm currently working on a project and I want to check if the user has pressed the space.

Let's say I have

string Txt = "Hello, daniweb";

and I have a textbox which the user is writing on it.

I have made a foreach loop to check each letter in the event of the Textbox1_textChanged.

foreach (char letter2 in lblRichText.Text)

Then inside this loop i've put this condition.

if (char.IsWhiteSpace(letter2))
{
//message
}

But it doesn't work.

Recommended Answers

All 14 Replies

Is that control a label or a Rich Text Box?
Do you have some other type of character set in use?
Did you loop through all of the characters and show the ASCII values of what it did find?

I just tried this and it worked (with a Rich Text Box):

using System;
using System.Windows.Forms;

namespace DW_414498_CS_FORM
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         richTextBox1.Text = "this is neat\tand so is this\r2\nand that";
      }

      private void bnCheckEm_Click(object sender, EventArgs e)
      {
         int intCount = 0;
         foreach (char c in richTextBox1.Text)
         {
            if (char.IsWhiteSpace(c))
            {
               MessageBox.Show((++intCount).ToString());
            }
         }
      }
   }
}

...but it just looks like you are trying to prevent whitespace. Is that correct?

If you want to check for a white space in runtime then you have to use KeyPress event handler:

private void textBox1_KeyPress(object sender, KeyPressEventArgs)
{
    if(char.IsWhiteSpace(e.KeyChar))
    {
        MessageBox.Show("Space key has beed pressed!");
    }
}

@Mitja Bonca I agree with you but, I want it in the event of Textchanged.

Why? Is there any particular reason?

If you insist this is how it has to be done.
NOTE: You have to check for LAST inserted character, do not do any loops, like you tried.

code:

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string word = textBox1.Text;
            char lastLetter = Convert.ToChar(word.Substring(word.Length - 1, 1));
            if (char.IsWhiteSpace(lastLetter))
            {
                MessageBox.Show("Last character inserted was whitespace!");
            }
        }

Well I have done that, checking for the whitespace but now, I tried to figure out how to check for the Delete key in the same event which is text_changed.

k.KeyCode == Keys.Delete)

So of course it gives me error so I've tried to change the method of the textbox textchanged event to

private void textBox1_TextChanged(object sender, EventArgs e, KeyEventArgs k)

I have just added this

KeyEventArgs k

This is how I was thinking but it gives negative results as usual.

Is there a way to check for the delete key in the event of text changed of the textbox?

Hmm, strange coding you have there....
You cannot use TextChanged event and KeyPressEventArgs argument.

Take a look at here, what you CAN have:

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string word = textBox1.Text;
            char lastLetter = Convert.ToChar(word.Substring(word.Length - 1, 1));    
            if (char.IsWhiteSpace(lastLetter))
            {
                MessageBox.Show("Last character inserted was whitespace!");
            }
        }
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                MessageBox.Show("Delete was pressed.");
            }
        }

You see the difference? These 2 events you NEED if you want to do it like you want to, event if I would only use KeyPress or KeyDown event for getting keys pressed -a and not TextChanged (this is means for some other purposes, not getting keys pressed).
Hope it helps,
bye

Thank you for helping. I know it is strange a bit but my goal is to check the delete key while the person is writing in the textbox. I know that I can not add 2 events like textchanged and keypressed but I just gave it a try.

Sure you can add, you can add as many events as you like. You can have both of them and even more.

I mean, while textchange event is working it also checks for the delete button, how can this be done?

I think you didn't get my idea.

I got you, but I am affraid you did get me!!

Some event fires before another. They never fire at the same time (one after another).
Events how they follow rising:
FOR DELETE KEY: 1st KeyDown, 2nd KeyUp(no KeyUp and no KeyPress event here)!!
FOR WHITESPACE KEY: 1st is KeyDown, 2nd KeyPress, 3rd TextChanged, 4th KeyUp

You got it know?

Yes I got it. Do you have any idea how can I code my idea?

You know what... if you want to learn programming, you better start doing the code by your self, we are here to help, not doing the work instead of you, I know you will agree with me, won`t you?!

You simply dont understand the basic concept of how to use events, not event specific events for keys.
And I have already explained you well in the upper posts.

Best to start if to put ALL the events of textBox (all 4 important ones - even there are a lot more):
textChanged
keyDown
keyPress
keyUp

to the form, put some code into each, and you will see whats going on.

About your question of this thread, I think you got plents of answers, so you can close this thread.
I hope you understand.
best regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.