Link to home
Create AccountLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

NotePad App = detect key word if typed... VB.NET 2010

Hi Experts,

My App is simple textBox (notepad) app...

I want to type away like NopePad... but if i type in special words... e.g. BEEP, then message box BEEP will appear... i use this as a simple example...

Type> something
Do> something else

I must be able to sent-up many key words and they'll perform what i need.

Thanks,
Roberto
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

          If TextBox1.Text = "beep" Then
            MsgBox("beep")

        End If

    End Sub

Open in new window

Avatar of Evan Cutler
Evan Cutler
Flag of United States of America image

ok,
Start with this....
and add from here.
This case looks for keystrokes and highlights the word based on logic.
You can expand from here.

It's JQUERY, so it runs in the client.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>


<script>
var dbList = new Array("kite", "mills", "rose");


$(document).ready(function() {
$("#inputField").keyup(function(e) {


code = (e.keyCode ? e.keyCode : e.which);


// this code is the ascii value of key pressed,
// you can use it to filter only alphanumeric keys
// like
/* 
if (code == 13)
{


} 
*/


if(  (code>96 && code <123) ||   (code>64 && code <91) )
{


	var typedText = $("#inputField").val();
	
	     for (var i = 0; i < dbList.length; i++) {
	
	           if (typedText != dbList[i]) {
		//	alert("no match");
	               $("#inputField").css('color', 'red');
	           }
	
	           if (typedText == dbList[i]) {
		//	alert("match");
	               $("#inputField").css('color', '#00FF00');
                        return;
	           }
	     }
}
});


});


</script>


</head>
<body>
<div
enter the text to search
<input type=text id=inputField />
<br>
</div>
</body>
</html>

Open in new window

Avatar of RobertoFreemano

ASKER

Arcee123,

Do u have VB code please?

Thanks,
Roberto
I guest this event would have t be triggered by ENTER keydown
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        If e.KeyCode = Keys.Enter Then
            Console.WriteLine("Enter")
            MsgBox("yes")
        End If

    End Sub

Open in new window

I GOT IT WORKING - WHOOTS!
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        If e.KeyCode = Keys.Enter Then
            Console.WriteLine("Enter")
            If TextBox1.Text.IndexOf("beep") > 0 Then
                ToolStripStatusLabel1.Text = "Word Found"
                MsgBox("Word Found")
            Else
                ToolStripStatusLabel1.Text = "Word Not Found"
            End If

        End If
    End Sub

Open in new window

How do I add more words?

I know this example doesn't work.
If TextBox1.Text.IndexOf("beep" or "something else" or "another word") > 0 Then

Open in new window

Ok, i found a bug... can an Expert please help?

If I launch the app... type in beep, press ENTER - nothing happends...

If I launch the app... type in anything, press ENTER, then type in beep - msg("BEEP") appears.
Avatar of Nasir Razzaq
Are you using the code from http:#37108741
Hi CC,

i found code from random google searches.
What code are you using now?
I GOT IT WORKING - WHOOTS! - this bit above...
<ElseIf> is the only other way i can think of detecting multiple words and acting on it.
If e.KeyCode = Keys.Enter Then
            Console.WriteLine("Enter")
            If TextBox1.Text.IndexOf("beep") > 0 Then
                ToolStripStatusLabel1.Text = "Word Found"
                MsgBox("Word Found")
            ElseIf TextBox1.Text.IndexOf("bing") > 0 Then
                ToolStripStatusLabel1.Text = "Word Found"
                MsgBox("Word Found")
            Else
                ToolStripStatusLabel1.Text = "Word Not Found"
            End If

Open in new window

One option is to use a list of strings and then a loop to see if any of the words in list match with the textbox content.
HEADING: Ok, i found a bug... can an Expert please help?

A workaround was to simpley popluate the Textbox at startup, then, once ENTER is hit, words seems to ping up once detected.

Anyone got any further ideas?

Thanks,
Rob
Hi CC,

I've intergrated another piece of code (which you resolved previously); which is where this project is leading. My issue here is with:

txt = InputBox("Enter IP_Address")

and

.Arguments = "/C ping" & txt

No ping results returned from input box.

IF I SET TO YOUR ORIGINAL EXAMPLE, IT WORKS.
.Arguments = "/C ping xx.xx.xx.xx"

I'm boosting Points on this!!!!!!
If e.KeyCode = Keys.Enter Then
            Console.WriteLine("Enter")
            If TextBox1.Text.IndexOf("ping") > 0 Then
                ToolStripStatusLabel1.Text = "Word Found"
                REM MsgBox("Word Found")
                txt = InputBox("Enter IP_Address")

                ' ping experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_26931696.html
                Dim ps As New Process
                With ps.StartInfo
                    .FileName = "cmd.exe"
                    .Arguments = "/C ping"  & txt
                    ToolStripStatusLabel1.Text = txt
                    .CreateNoWindow = True
                    .UseShellExecute = False
                    .RedirectStandardOutput = True
                End With
                ps.Start()
                ps.WaitForExit()
                Dim output As String = ps.StandardOutput.ReadToEnd()
                ps.Close()
                TextBox1.Text &= (output)

Open in new window

>.Arguments = "/C ping" & txt

Could it be because you are missing a space?

.Arguments = "/C ping " & txt
Hi CC,

Yes, that was it - thanks man.

I wonder if you know how to stop the action... so i:

1. type in ping
2. press enter
3. inputbox
4. ping results pop into textbox.
5. IF I TYPE IN ANYTHING ELSE (RANDOM) & PRESS ENTER >  I DON'T WANT IT TO PING.
> CANCEL INPUTBOX ON ENTER-KEY

I'm guessing it's something to do with the process? but ps.close already happens.

Thanks,
Roberto
> CANCEL INPUTBOX ON ENTER-KEY - unless i purposely type ping again.

is this a loop?
Not sure I understand you question. Do you only want to run code if you type ping in textbox?
Yes, that's it!

Type PING> enter key
Start process
Result
End process
Your code is inside
If TextBox1.Text.IndexOf("ping") > 0 Then

so it should not execute if anything other than ping is typed.
Sorry CC,

I'll sort this out this weekend - promise... been on holiday :)
No problem. Hope you enjoyed your holidays.
Hi CC,

Sadly it's still not working...

1. I type PING
2. Inputbox... type =  IP Address xx.xx.xx.xx
3. result
4. if i type in anything (i.e. Boo)... press enter... I get the Inputbox

:(
>If TextBox1.Text.IndexOf("ping") > 0 Then

This should not have worked. If ping is the only work then IndexOf will return 0. If not found, it returns -1.

I have just tested this and it works.
Can i ping the other VM - yes I think so - yes
oops- ignore - lol
Hi CC,

Sorry about the wait... I've wonder if you could take a look at this vid I put together and see where I'm going wrong?

Thanks,
Roberto
ping-test-notepad.zip
Hmm. You did not mention this before that its a multiline text box.

You would need to do something like below


If TextBox1.Text.EndsWith("ping") Then
WOW - thanks CC, that's done the trick - sorry I should have mentioned multiline TB...

i last thing if i may...
have the type cursor move to the bottom, ready to type again... currently, it stays on the top line.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thank you sooooo much CC

;)