predictive RichTextBox vb.net 2010

Hello Experts,

I'm trying to write a winform app - using a RichTextBox and when I type in a specific word and press ENTER, it will detect... for example. If I type in the word "Blue"- message = "Y" else "N"

The problem is: once "Blue" is detected, no matter what else I type, after pressing ENTER... Y always appears. How can resume back to normal typing. I use "N" to test it's working.

--code--------------------------------------------
 If e.KeyCode = Keys.Enter Then
            Dim net As Boolean = RichTextBox1.Text.Contains("Blue")
            If e.KeyCode = Keys.Enter Then
                If net Then
                    MsgBox("Y")
                Else
                    If Not net Then
                        MsgBox("N")
                    End If
            End If
            End If
        End If
RobertoFreemanoAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi Roberto;

Below is your code modified as I stated in a earllyer post. This code does the three steps you outlined in your post, note the comments in the code.

What else do you want the code to do that it is not doing now which you want it to do?

Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown

    If e.KeyCode = Keys.Enter Then
        Dim net As Boolean = RichTextBox1.Text.Contains("Blue")
        If net Then
            '' 2. if Key word is detected - do something on ENTER
            MessageBox.Show("Key word was detected, doing something here")
        Else
            '' 1. type any word press enter (ignore if not key word)
            MessageBox.Show("Key word is not found in RichTextBox Text Property ")
        End If
        '' 3, next line, return to normal and type any word - press enter.
        MessageBox.Show("Returning to normal RichTextBox processing")
    End If

End Sub

Open in new window

0
 
Fernando SotoRetiredCommented:
What Event Handler do you have the code in? What behavior do you want this functionality to produce.
0
 
RobertoFreemanoAuthor Commented:
Hi, this is on RTB kytdown....
And if I type in a specific word (which there will be a collection of), then (example) if I type "tom"... result would perhaps center align the word "tom"
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Fernando SotoRetiredCommented:
Hi Roberto;

Please read the comments I placed in your code. To your statement, "The problem is: once "Blue" is detected, no matter what else I type, after pressing ENTER... Y always appears." Y will continue to appear after every time you press the Enter Key because Blue is still in the RichTextBox text property.

To your statement, "How can resume back to normal typing. I use "N" to test it's working.", After the code leaves the KeyDown event you will be back to normal typing untill you press the Enter Key again at which time the message box will appear with Y again because Blue is still in the Text property. The only way at this point to get the message box to display N is to delete the word Blue and press Enter Key.

'' This line of code test to see if the Enter key was entered in the RichTextBox
If e.KeyCode = Keys.Enter Then
    '' Test to see if the word Blue eas entered anywhere in the text box.
    Dim net As Boolean = RichTextBox1.Text.Contains("Blue")
    '' This If statement is not needed because we know that the Enter key was pressed
    '' as already tested in the above If statement
    If e.KeyCode = Keys.Enter Then
        '' Test to see if Blue is in the RichTextBox anywhere
        If net Then
            '' It was and display message box
            MsgBox("Y")
        Else
            '' This If statement is also not needed because if we made it here
            '' then net is false. So need to test to see if it is false
            If Not net Then
                MsgBox("N")
            End If
        End If
    End If
End If

Open in new window

0
 
RobertoFreemanoAuthor Commented:
Thanks Fernando, your comments help me understand the issue, but I'm still a bit puzzled. I'm at basic level in vb.
I added "RichTextBox1.Text.Remove("Blue")" - which I'm sure you'll laugh at. But I don't want to remove it as such, just carriage return & continue on next line.
0
 
RobertoFreemanoAuthor Commented:
I thought about trying to change the Boolean... net = False, but this did not work :(
0
 
Fernando SotoRetiredCommented:
The Remove method does not take a string, it takes an integer.

I am sorry but I an not sure what funtion this is to do, what functionality do you want this to do?
0
 
RobertoFreemanoAuthor Commented:
I guess I want it to:
1. type any word press enter (ignore if not key word)
2. if Key word is detected - do something on ENTER
3, next line, return to normal and type any word - press enter.
0
 
RobertoFreemanoAuthor Commented:
Wow Fernando - Excellent job my friend. Thanks very much.

I lose the formatting in the RichTextBox once I press Enter and return to normal RichTextBox processing... but I guess that's something I can look into.

Cheers,
Roberto
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.