Results 1 to 9 of 9

Thread: [RESOLVED] Converting Mid() to .Substring

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Resolved [RESOLVED] Converting Mid() to .Substring

    Am working on converting my VB6 mid() functions to .net .substring.

    I cannot figure out this part of it.

    Here's my VB6 code:

    Code:
    txtInput.Text =Left(txtInput.Text, txtInput.SelStart) & strtext & Mid(txtInput.Text, txtInput.SelStart + 1)
    My new .Net is like this:

    Code:
    Dim Y As String
    Y = txtInput.Text
    
    strtext = TypedLetter.Text
    'TypedLetter is the sender button.
    
    txtInput.Text = Y.Substring(0, txtInput.SelectionStart) & strtext & Y.Substring(txtInput.SelectionStart + 1, ???)
    Where the ??? are in the .net code I don't know what to put there.

    I want it to pick up characters from "txtInput.SelectionStart + 1" to the end of the character string that is inside txtInput textbox.

    "txtInput.SelectionStart + 1" is the cursor position after strtext is entered into txtInput.Text

    Can someone please help on this. I feel like it should be an easy answer!!
    I tried using something like:

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Converting Mid() to .Substring

    Not sure what your question is, but...

    Code:
            Dim ts As String = "ABCDEFGH"
            Dim tsF As String = "DEF"
            Dim ns As String = ""
            ns = ts.Substring(0, ts.IndexOf(tsF)) & tsF & ts.Substring(ts.IndexOf(tsF) + tsF.Length)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Converting Mid() to .Substring

    I have an onscreen keyboard that I am making.

    Each letter is in the text of the button.

    I have all the letters linked to the same event handler: TypeEvent.

    As the letters are selected I want them to insert into txtInput where the cursor is positioned.

    So if txtInput reads: "Chrs"

    And if I move the cursor between R and S and select the "i" button, txtInput will now read "Chris".

    ... as an example.

    I was trying to use the substring method to accomplish this insert.

    Unless it is ok to use the Strings.Mid functions.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Converting Mid() to .Substring

    For that type of situation, I would use string.insert(), which requires you pass an index in the the string where to start inserting, and also pass in a string to insert at that point.

    To take your example from above (and yes this is all hard coded, but you should get the idea to adapt to your program)

    Code:
            Dim myString As String = "chrs"
            myString = myString.Insert(3, "i")
            MessageBox.Show(myString)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Converting Mid() to .Substring

    I have this:

    Code:
    Dim Y as txtInput.Text
    Dim strtext = TypedLetter.Text   'the text from the sender button
    txtInput.Text = Y.Insert(txtInput.SelectionStart, strtext)
    It inserts the letter, but still not working properly. After inserting the letter it only keeps the first letter after the inserted letter in txtInput.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Converting Mid() to .Substring

    AHA!

    Code:
      txtInput.Text = Y.Substring(0, txtInput.SelectionStart) & strtext & txtInput.Text.Substring(txtInput.SelectionStart)
    Got it.

    I would still be curious to know why my insert method didn't work... as posted above. Thanks.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Converting Mid() to .Substring

    The line:

    Dim Y as txtInput.Text

    is not valid syntax. If you wanted a string variable y that contained txtInput.Text you would write

    Dim Y = txtInput.Text (if you have option strict off or option infer on)
    or
    Dim Y as String = txtInput.Text (which is valid in all scenarios)

    you likely only would need this for your program

    Code:
    txtInput.Text = txtInput.Text.Insert(txtInput.SelectionStart, TypedLetter.Text)

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Converting Mid() to .Substring

    Yes kleinma! Perfect, thanks.

    I am not sure why I wrote: Dim Y as txtInput.Text in my post, because in my app. I had what you declared also. Typed too fast maybe on here.

    Thanks so much!

    txtInput.Text = txtInput.Text.Insert(txtInput.SelectionStart, TypedLetter.Text)

    That is the easiest way, although my way works too. I like learning another routine.

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] Converting Mid() to .Substring

    Often times in programming there are multiple ways to do the same task. Generally what it comes down to when picking the best way for the job, is to determine performance differences (how it runs, benefit to the end user) and maintenance differences (how easy it is to modify later, benefit to the developer).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width