re-editing a text box (VB.NET)
I'm parsing input in a multi-line text box from within a Keypress Event. How do I re-edit that text without exiting the event routine (keeping the users caret in the original place, etc) and loop back to re-parse the line?
(VB.NET - Visual Studio 2002)
Any ideas gratefuly accepted...
I'm not quite sure if I've got the jist of what you are trying to do, but create a routine (sub) to perform the passing etc and use the text_onkeypress event to call it.
Then this code will do what you want:
' Gets position
cursorPosition = TextBox1.SelectionStart()
' Gets text from line
currentLineText = TextBox1.Lines(TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart))
-----> change cursor position
' Sets cursor back
TextBox1.SelectionStart = cursorPosition
and if a button is clicked or something that causes the textbox to use focus just use:
obviously you may not want to do them in this exact order
Hope this helps,
The problem is not the actual editing or keeping the original caret position after reloading text back into the box, but re-calling the sub I'm already inside without leaving it, if you see what I mean, effectively using a goto_the_start_of_the_sub but having the user change the text after his/her input has been found to contain a syntax error, for example - To clarify:
User inputs text.
In MyParseSub, parse the text.
OnError re-edit and continue the parse from the caret (which will now point to the error position...) without leaving the MyParseSub.
Can the AddressOf function help in any way by finding My.MyParseSub?
It isn't just a recursive call to the same routine, though, as parameters may or may not need to be remembered (statically). Theoretically this re-start might need to apply for every character in the input text (hundreds of times, possibly, in a worst-case scenario).
This is all hypothetical, by the way, as I found a way to avoid the issue, however (you know how it is...) I've got somewhat obsessed with the concept.
That was a somewhat mind boggling post.
I think I know where you are coming from and it's ok for the sub to be exited once it has parsed all the text? and it be recalled on a textbox_keypressed event?
I think you are trying to build some sort of code editor?
If what I said above is true can you not use the textbox_keypressed event to call the method again?
And to keep it running till the end of the textbox use
sub currentsub(param1, param2)
----> Code to parse text
----> error catching code
if textbox carrot position <> textbox end then
This is perhaps more pseudo code but gets the point accross. Theoretically, it recurses unless the all of the text has been parsed. Obviously you need to store the current carat position then after it has all been parsed move it back.
but if this needs to occur hundreds of times before it ends, you may suffer from outofmemory exceptions.
But is this what you are looking for?
Aha, Jon! You've hit the perverbial nail on it's noggin.
I was not aware of 'sub currentsub(param1, param2)' and this sounds like exactly what's needed but this is still recursive.
You're nearly correct in your supposition that it's 'some sort of code editor'. It's actually a natural language parser (initially to parse text adventure games for test purposes) but the principle is the same.
If you're curious this is what it does (so far)...
User inputs a command (like 'Quietly open the large green box on the table then take all the money out of it and then give the money to the monkeys')
The input text is then spell-checked (very basic) and gramatically corrected (adding any punctuation as required and correcting quotations, etc).
Then the input is broken up into statements (complete sentences) and passed, one at a time through the syntactic analyzer and parsed as you would expect.
Throughout this process the input text has to be able to be reverse-engineered so the input can be reconstructed for editing by the user if the parser can't understand the intent. But - and this is the tricky bit - any complete statements that are parsed without errors can be discarded from the built list of statements AND from the input without disturbing the rest of the input text that may follow.
This is achieved by initially correcting the text and converting every part (word, punctuation, number, etc) into atoms.
Normally these atoms would be converted into tokens early on in the chain of events but in this system the conversion is left as late as possible in order that fewer errors can occur - indeed it can handle unknown words (words that don't appear in any of its ten type-dictionaries - nouns, adjectives, prounouns, etc, etc - by examining the context and making assumptions about what type of word it's expecting so making reasonable responces without alerting the user to what words the parser knows or does not know.
It's kind of a cross between a traditional text adventure parser, a code editor and the famous Eliza psychotherapy prog.
Thanks again for all your input, Jon.
That last bit about parsing it all and then leaving the input area with the rest of the un-understood parts.
You should be able to assign the contents to a variable and parse through the input.
Remove any parsed section from the variable and then assign the variable value to the input.
there are a couple of string functions that can do this or help do this
This removes the section of text from character position start for the length.
this replaces all instances the first string with the second string
string.indexof(string[, start][, length])
this returns the position of the first instance of the string within as an integer, and can be set to start at a specified position and stop at a specified length.
so you could use it like this:
input = "recognised commands unrecognised commands recognised commmands"
dim chr as integer = input.indexof("recognised commands")
dim chr2 as integer = input.indexof("unrecognised commands")
input = input.remove(chr,chr2)
or like this:
input = "recognised commands unrecognised commands recognised commmands"
input = input.replace("unrecognised commands", "")
Hope this answers it?
Currently the entire input is collected in a linked list of linked lists - each list of lists represent a statement within which each link points to an atom of text. For this I've written a few LISP-inspired functions...
Public Sub GetFirst(ByRef s As String) As String
This returns the first element (atom) of a string (a number, word, punctuation or EOL). The element is also (conveniently) removed from the string s.
Public Sub GetLast(ByRef s As String) As String
Public Sub ButFirst(ByRef s As String) As String
Public Sub ButLast(ByRef s As String) As String
These last three do what you'd expect.
All these (and others) ignore leading and trailing whitespace and simplify text manipulation tremendously without further resort to the normal 'low-level' string functions. They also take into account that the parameter strings can be Nothing, so never produce exceptions.
If you're interested in playing with any of these functions I'd be pleased to forward the code to you (or anyone else browsing this) along with any other relevant routines. Just send me a note to firstname.lastname@example.org
By benz84 in forum VB Classic
Last Post: 11-14-2005, 04:38 AM
By jobartley515 in forum VB Classic
Last Post: 09-18-2005, 03:10 PM
By folbabe in forum VB Classic
Last Post: 08-23-2002, 06:41 AM
Last Post: 08-30-2001, 11:45 AM
By Javaid Ahmad in forum VB Classic
Last Post: 04-13-2000, 11:46 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL