Link to home
Start Free TrialLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

What Event Handler do you have the code in? What behavior do you want this functionality to produce.
Avatar of RobertoFreemano

ASKER

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

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.
I thought about trying to change the Boolean... net = False, but this did not work :(
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?
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.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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