Question

I'm wondering what event/events is/are being used in counting the vowels while your typing the sentence in the textbox.

I'm not quite sure if it's something to do with "KeyPress" or "KeyUp".

I would appreciate your help.

=====

This is now where I'm stucked:

  private void btnCount_Click(object sender, EventArgs e)
    {
        string yourSentence;
        yourSentence = textBox1.Text.ToLower().Trim();

        char ch1 = 'a';
        char ch2 = 'e';
        char ch3 = 'i';
        char ch4 = 'o';
        char ch5 = 'u';

        int counta = 0;
        int counte = 0;
        int counti = 0;
        int counto = 0;
        int countu = 0;

        int j = counta + counte + counti + counto + countu;



    foreach (char v in yourSentence)
    {
        if (v == ch1) { counta++; j++; }

        else if (v == ch2) { counte++; j++; }

        else if (v == ch3) { counti++; j++; }

        else if (v == ch4) { counto++; j++; }

        else if (v == ch5) { countu++; j++; }
    }



      listBox1.Items.Add("There are " + counta.ToString().Trim() + " a's in the sentence");
        listBox1.Items.Add("There are " + counte.ToString().Trim() + " e's in the sentence");
        listBox1.Items.Add("There are " + counti.ToString().Trim() + " i's in the sentence");
        listBox1.Items.Add("There are " + counto.ToString().Trim() + " o's in the sentence");
        listBox1.Items.Add("There are " + countu.ToString().Trim() + " u's in the sentence");
        listBox1.Items.Add("All in all there are " + j.ToString().Trim() + " vowels in the sentence");


  private void textBox1_KeyDown(object sender, EventArgs e)
     {
          string yourSentence;
          yourSentence = textBox1.Text.ToLower().Trim();

         //I think I have to add the codings here. But what will happened to the 
         //btnCount_Click?

     }
Was it helpful?

Solution

Well there is a TextChanged event in the TextBox that triggers whenever a character is typed or deleted. This could be used to count vowels in real-time.

Edit:

I combined your code from textBox1_keyDown and btnCount_Click and put it in the TextChanged event, and it worked almost perfectly. I only had to add one line:

listBox1.Items.Clear();

...just before items are added to the listbox. This way, the listbox is emptied before adding the counts.

This is the result:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string yourSentence;
        yourSentence = textBox1.Text.ToLower().Trim();

        char ch1 = 'a';
        char ch2 = 'e';
        char ch3 = 'i';
        char ch4 = 'o';
        char ch5 = 'u';

        int counta = 0;
        int counte = 0;
        int counti = 0;
        int counto = 0;
        int countu = 0;

        int j = counta + counte + counti + counto + countu;



        foreach (char v in yourSentence)
        {
            if (v == ch1) { counta++; j++; }

            else if (v == ch2) { counte++; j++; }

            else if (v == ch3) { counti++; j++; }

            else if (v == ch4) { counto++; j++; }

            else if (v == ch5) { countu++; j++; }
        }


        listBox1.Items.Clear();
        listBox1.Items.Add("There are " + counta.ToString().Trim() + " a's in the sentence");
        listBox1.Items.Add("There are " + counte.ToString().Trim() + " e's in the sentence");
        listBox1.Items.Add("There are " + counti.ToString().Trim() + " i's in the sentence");
        listBox1.Items.Add("There are " + counto.ToString().Trim() + " o's in the sentence");
        listBox1.Items.Add("There are " + countu.ToString().Trim() + " u's in the sentence");
        listBox1.Items.Add("All in all there are " + j.ToString().Trim() + " vowels in the sentence");

    }

I don't need any other code to make this work.

OTHER TIPS

I built a search box that acts something like an AJAX text box - it does the search basd on what has been typed "so far" in the text box. There's no need to type in the text and then click an associated button. It searches as you type. I don't know if there's a name for this UI pattern, there oughta be.

The dynamic search is hooked to the TextChanged event. But the key was, I didn't it want to search while the text was being actively changed, as the user was typing. I wanted to search when the changes were complete, when the typing stopped.

This may be interesting for you too.

The hueristic I used: if 600ms elapses after the last textchange event, then typing has stopped, and that's when the search should run. But how do you get code to run 600ms AFTER a TextChange event. It's not possible to Thread.Sleep within the event handler. This just causes UI delay.

The solution I came up with was this: use Threadpool.QueueUserWorkItem in the TextChange event to queue up a worker method, called MaybeDoSearch. In that worker, do a Thread.Sleep for the delay interval (600ms). When the Sleep completes, check the elapsed time since the prior TextChanged event. If that time exceeds 600 ms, then actually do the Search.

It looks like this

    System.DateTime _lastChangeInSearchText;
    private const int DELAY_IN_MILLISECONDS = 600;

    private void tbSearch_TextChanged(object sender, EventArgs e)
    {
        _lastChangeInSearchText = System.DateTime.Now;
        string textToFind = tbSearch.Text;
        if ((textToFind != null) && (textToFind != ""))
            System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(MaybeDoSearch), textToFind);
        else 
        {
            // clear the ListView that contains the search results 
            this.ListView2.Items.Clear();
        }
    }

    private void MaybeDoSearch(object o)
    {
        System.Threading.Thread.Sleep(DELAY_IN_MILLISECONDS);
        System.DateTime now = System.DateTime.Now;
        var _delta = now - _lastChangeInSearchText;
        if (_delta >= new System.TimeSpan(0,0,0,0,DELAY_IN_MILLISECONDS))
        {
            // actually do the search
            ShowSearchResults();
        }
    }

Mine is a WinForms app. Because the MaybeDoSearch() runs on a worker thread, not the UI thread, then in the ShowSearchResults(), UI update must be protected with InvokeRequired.

During normal typing, a person will type 2 or 3 characters in 600ms. The result is there are 2 or 3 work items queued and running (mostly Sleeping), on separate threadpool threads, at any given moment while typing is actively occurring.

Someone suggested earlier that you will have to re-scan all of the text for vowels with every change. It's redundant work at runtime, which for efficiency purposes you'd normally want to avoid. But the code is so much simpler that way. I had the same issue, and decided to not try to figure out just how the text changed with the current TextChange event. It changed, so the code does a full new search. The search completes quicker than the delay interval (600ms). In your case, counting vowels, it will be even faster.

Let's see. This can be done in all of these, but some of the events are more likely for it.

KeyDown: In KeyDown, the key still is represented as an integer usually. You would have to map the vowels yourself. Repeated values only fire once. So you might loose some vowels.

KeyPressed: here you get the key in it's right representation. Add one if a vowel is pressed. But do I have to subtract one if del or backspace is pressed? You do not know which key was deleted.

KeyUp: Again, the representation is an integer. Repeated values only fire ones.

TextChanged: This is triggered whenever the text changes. In here you do not know exactly what the last key pressed was (it could be womewhere within the text), so you ve to recount the number of vowels from start.

I would do that somewhat like this:

private void textBox1_TextChanged(object sender, EventArgs e)
 {
      btnCount_Click(sender, e);
 }

This way the calculation is done everytime the text changes, and in addition can be done pressing the button too. If you do not like to call an event function directly, make a function that is called from both places.

And maybe you want to do this only from time to time (see other answers) in TextChanged.

Keypress is fired when the key is pressed. (not sure about it) key UP is fired when the Key rises up (released) (remove ur finger off the keyboard)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top