re-editing a text box (VB.NET)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: re-editing a text box (VB.NET)

  1. #1
    Join Date
    Jun 2006
    Posts
    5

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

  2. #2
    Join Date
    May 2006
    Posts
    40
    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:

    '----> input

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

    textbox1.focus()

    obviously you may not want to do them in this exact order

    Hope this helps,
    J JJ

  3. #3
    Join Date
    Jun 2006
    Posts
    5
    Thanks, John.

    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.

  4. #4
    Join Date
    May 2006
    Posts
    40
    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)
    try
    ----> Code to parse text
    catch
    ----> error catching code
    finally
    if textbox carrot position <> textbox end then
    currentsub(param1, param2)
    end if
    end try

    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?
    J JJ

  5. #5
    Join Date
    Jun 2006
    Posts
    5
    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.

    Regards, Paul.

  6. #6
    Join Date
    May 2006
    Posts
    40
    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

    string.remove(start, length)
    This removes the section of text from character position start for the length.

    string.replace(string, string)
    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?
    J JJ

  7. #7
    Join Date
    Jun 2006
    Posts
    5
    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 paul.whittaker20@ntlworld.com

Similar Threads

  1. Text box validate on Access Please help
    By benz84 in forum VB Classic
    Replies: 2
    Last Post: 11-14-2005, 04:38 AM
  2. Replies: 4
    Last Post: 09-18-2005, 03:10 PM
  3. Pass a text selected on listbox to a text box
    By folbabe in forum VB Classic
    Replies: 1
    Last Post: 08-23-2002, 06:41 AM
  4. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM
  5. Hiding the text in Rich Text Box
    By Javaid Ahmad in forum VB Classic
    Replies: 0
    Last Post: 04-13-2000, 11:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center