|
-
Mar 17th, 2009, 08:17 AM
#1
Thread Starter
Hyperactive Member
deleting specified text from textbox
ok i have 2 textbox's and 1 button i have 1 big textbox that has a buch of number's in it and the number i want to delete is in the top textbox how can i delete number 2 ? from the big textbox ?
-
Mar 17th, 2009, 08:22 AM
#2
Re: deleting specified text from textbox
You can use IndexOf to find the location of the desired text in the TextBox. You can then set the SelectionStart and SelectionLength properties to select that text. You can then set the SelectedText property to replace the currently selected text. To remove it you would replace it with an empty string or Nothing.
-
Mar 17th, 2009, 08:25 AM
#3
Thread Starter
Hyperactive Member
Re: deleting specified text from textbox
k i dident under stand what you just said can you just post the code cause im a noobie
-
Mar 17th, 2009, 08:36 AM
#4
Re: deleting specified text from textbox
All you have to do is call methods and set properties and you already know how to do that because you've already done it. Get the Text property of the TextBox to get a String. Call the IndexOf method of that String to get the location of the desired substring. Set the SelectionStart property of the TextBox to set the caret position. Set the SelectionLength property of the TextBox to select the desired text. Set the SelectedText property to replace the selected text. It's just a series of steps and each one is very simple. Try to implement them one by one and if you get stuck on one then post back with what you've done so far.
Alternatively, you can just wait for someone to do it for you but, believe me, you'll learn more by trying things for yourself first.
-
Mar 17th, 2009, 08:52 AM
#5
Re: deleting specified text from textbox
You're never going to stop being a noobie if you expect people to just post code for you.
If you just want to delete the first instance of the word, then it is pretty straightforward. We are going to search the Text of the big TextBox for the IndexOf the first occurence of the Text in your search TextBox. Then we are going to Remove the correct number of characters from the big TextBox, starting from the IndexOf the first occurence.
Code:
Dim strToEdit, strSearch, strEditedText As String
Dim intFirstIndex, intLength As Integer
strToEdit = bigTextBox.Text
strSearch = smallTextBox.Text
intFirstIndex = strToEdit.IndexOf(strSearch)
intLength = strSearch.Length
strEditedText = strToEdit.Remove(intFirstIndex, intLength)
bigTextBox.Text = strEditedText
This is the long-winded version, which should give you an understanding of what is going on. The short version would be:
Code:
bigTextBox.Text = _
bigTextBox.Text.Remove( _
bigTextBox.Text.IndexOf( _
smallTextBox.Text _
), _
smallTextBox.Text.Length _
)
-
Mar 17th, 2009, 09:32 AM
#6
Thread Starter
Hyperactive Member
Re: deleting specified text from textbox
MaximilianMayrhofer & jmcilhinney thank you thank you it works but how do i fix it so it doesn't do this
ALT
ARE
ART
ATE
EAR
EAT
ERA
ETA
HAE
HAM
HEM
HER
LAM
LAT
LEA
MAE
MAR
MAT
MEL
RAH
RAM
REM
RET
TAM
TAR
TEL
THE
ALME
EARL
EATH
HAET
how can i remove the
?
i need it like this
TAM
TAR
TEL
THE
ALME
EARL
EATH
HAET
etc thanks
Last edited by tweaker99; Mar 17th, 2009 at 09:36 AM.
-
Mar 17th, 2009, 09:39 AM
#7
Re: deleting specified text from textbox
think about what you are asking? you can probably fix it by adding
& Environment.Newline
to one of the statements you were given.
-
Mar 17th, 2009, 09:50 AM
#8
Thread Starter
Hyperactive Member
Re: deleting specified text from textbox
like this ?
Code:
bigTextBox.Text = _
bigTextBox.Text.Remove( _
bigTextBox.Text.IndexOf( _
smallTextBox.Text _
), _
smallTextBox.Text.Length & Environment.NewLine)
casue that doesn't work ?
Last edited by tweaker99; Mar 17th, 2009 at 10:07 AM.
-
Mar 17th, 2009, 10:16 AM
#9
Thread Starter
Hyperactive Member
Re: deleting specified text from textbox
-
Mar 17th, 2009, 11:32 AM
#10
Re: deleting specified text from textbox
max gave you 8 lines. if it doesn't work on one, try another.
FYI - pm'ing someone with code questions, unless they have stated to do so, is frowned upon.
one other thing, this and some of the other problems you have posted might have been done better (easier?) with a listbox.
Last edited by dbasnett; Mar 17th, 2009 at 12:28 PM.
-
Mar 17th, 2009, 03:59 PM
#11
Re: deleting specified text from textbox
Tweaker, try:
Code:
Dim yourCharacter as string
bigTextBox.Text = bigTextBox.Text.Replace(yourCharacter & ControlChars.NewLine, "")
You can set yourCharacter to whatever you want and then the code Replace will find it (along with the line below it) and replace it with "", in otherwords nothing, so it will delete it.
Also, dbasnett has a point with using listboxes. What exactly are you trying to do with this list of numbers?
-
Mar 17th, 2009, 10:19 PM
#12
Re: deleting specified text from textbox
That method will work if you want to get rid of every occurence of the search string. The example I gave was to specifically get rid of the first occurence. Now the OP can choose what he's actually looking for 
Other than that, I would suggest using Environment.NewLine rather than ControlChars.NewLine.
I'm also going to throw my two cents in there and ask why the OP isn't using a ListBox.
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
|