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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.