|
-
Dec 23rd, 2009, 05:32 PM
#1
Thread Starter
Fanatic Member
[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:
-
Dec 23rd, 2009, 06:12 PM
#2
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)
-
Dec 23rd, 2009, 06:18 PM
#3
Thread Starter
Fanatic Member
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.
-
Dec 23rd, 2009, 06:33 PM
#4
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)
-
Dec 23rd, 2009, 06:53 PM
#5
Thread Starter
Fanatic Member
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.
-
Dec 23rd, 2009, 07:01 PM
#6
Thread Starter
Fanatic Member
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.
-
Dec 23rd, 2009, 07:39 PM
#7
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)
-
Dec 24th, 2009, 03:38 PM
#8
Thread Starter
Fanatic Member
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.
-
Dec 24th, 2009, 04:11 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|