|
-
Sep 6th, 2010, 11:27 AM
#1
Thread Starter
New Member
Splitting Text from a TextBox into an Array
Hi all,
I'm hoping some of you can help me.
Im writing a program where im trying to split some text imported from a textfile into a textbox into an array.
The array is undefined and i would like each piece of text to be an element in the array. I would then like to sort this information into alphabetical order.
This is the code I have at present; I apologise if any appears to be out of sequence or whatever, VB is a rather puzzling language to myself!
PS i have the code behind a button as you can tell from the code.
Thank you all for your time and patience.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim textInput As String
textInput = TextBox1.Text
Dim textArray() As String = TextBox1.Text.Split(","c)
Array.Sort(textArray)
Dim i As Integer
For i = LBound(textArray) To UBound(textArray)
TextBox2.Text = (textInput)
Next
End Sub
-
Sep 6th, 2010, 11:39 AM
#2
Re: Splitting Text from a TextBox into an Array
Just out of curiosity, why are you even putting it into the textbox if you are just going to follow that up by putting it into an array? You then go ahead and put lines into a second textbox, and I would guess that has at least something to do with your error, as it is currently doing something particularly useless.
A textbox makes the user think that they have the right to edit something. If you put the text from the file into a textbox, then the user might change the text, which will mean that the text in the textbox will become different from the text in the array. A label would be a better place to put text for display but not editing.
As for that second loop, it loads each line into a textbox...then immediately overwrites it with the next line from the array. What should end up in the textbox is whatever is in the last line of the array, which is probably not what you want. If it IS what you want, there is a better way to get there.
Other than that, what problems are you having with the code?
My usual boring signature: Nothing
 
-
Sep 6th, 2010, 11:45 AM
#3
Re: Splitting Text from a TextBox into an Array
You declare a string variable called Textinput, and you assign it the value of the textbox.text, but then you never use that variable again until you do your loop at the end of the code.
If your goal is to split the textbox text using a comma as a delimiter, and then you wish to output the resulting array back to a second textbox then you just need to do this.
Code:
'SPLIT THE TEXT INTO AN ARRAY AT THE COMMAS
Dim textArray() As String = TextBox1.Text.Split(","c)
'ALPHA SORT THE ARRAY
Array.Sort(textArray)
'JOIN THE ARRAY BACK INTO A STRING USING A NEWLINE AS THE DELIMITER
TextBox2.Text = String.Join(Environment.NewLine, textArray)
if you don't use a delimiter to join the string back together in textbox2, then all the text would be together, for example if textbox 1 contained "B,C,D,A", after sorted and put into textbox2, it would look like "ABCD". My sample code uses environment.newline to connect the string back together which would result in textbox2 looking like (assuming the textbox is set to multiline)
A
B
C
D
but if you wanted something different, you can just change environment.newline to whatever string you do want to be in between each sorted string. If you wanted no space at all, use string.empty, to produce "ABCD"
-
Sep 6th, 2010, 11:48 AM
#4
Thread Starter
New Member
Re: Splitting Text from a TextBox into an Array
Shaggy Hiker,
Thanks for your reply.
Allow me to explain a little more detail.
The form has two textboxes. I have set up buttons on the form which allow a user to open an exsisting text file and place text into a textbox on the left hand side of the form. (TextBox1)
I have also set up a save button, where a user can type text into the box on the left hand side and save this text as a text file. While i completely appreciate the merits of having a label...it probably wont serve my purposes...to the best of my knowledge.
I then would like the text from textbox1 to be broken into individual words, which become elements of an array, which are then alphabestised and displayed (with a line break for each word) in TextBox2 which is located on the right of the form.
Im sorry if im missing the point or details, i havent been coding in VB for more than 3 days!
Thanks again!
-
Sep 6th, 2010, 12:02 PM
#5
Re: Splitting Text from a TextBox into an Array
So the user CAN edit the text that is put into Textbox1? In that case, every time they change the text, the information in the array, and therefore the information in Textbox2, becomes invalid. For this reason, I would suggest that you just load the text into TextBox1 in the button click event. You then would move the part that breaks it into the array and loads textbox2 into a different sub that you call from the button click event. The reason for doing so is that you would ALSO call that sub from the TextBox1 TextChanged event handler so that whenever the user changes the text in TextBox1 you update both the array and the other textbox.
Now for Textbox2. Is the user going to alter that? Of what value would it be for them to alter a list of words? If they were to alter the list of words, should that change what is in Textbox1? I suspect that you don't expect the user to alter the list of words. Based on that, I would suggest that you should replace Textbox2 with a ListBox. You would be able to load the listbox from the array with a pretty simple statement:
ListBox1.Items.AddRange(textArray)
You also wouldn't need to sort the array. Instead, just set the Listbox.Sorted property to True.
This change would be much easier overall. If you do want the user to be able to edit the array of words, then the Listbox would probably still be somewhat better, but the editing would be more complicated.
My usual boring signature: Nothing
 
-
Sep 6th, 2010, 12:15 PM
#6
Thread Starter
New Member
Re: Splitting Text from a TextBox into an Array
Shaggy Hiker & kleinma
Thanks again!
Sound advice on using the event handler for the editorial part of the app.
The second textbox must be a text box, as for the second part of this piece of work im undertaking involves sorting the values collected from the area into an alphabetic list which also contains what line each word from the text imported from TextBox.
I.E.
Apple 2
Toast 4
Etc.
I tried using a listbox earlier on however it doesnt seem to have the capacity to fulfill the latter stages of the program.
I've dabbled in C++ years ago at University but i must say its been a while and VB does confuse me slightly!
Thanks!
-
Sep 6th, 2010, 12:40 PM
#7
Thread Starter
New Member
Re: Splitting Text from a TextBox into an Array
Guys,
Thanks to you both. I adapted the code supplied by kleinma to split the text from TextBox1 up into seperate words.
Now i just have to figure out how to count what line each word appears on...
If you have any ideas it would be helpful to have them to compare to my own.
-
Sep 7th, 2010, 08:13 AM
#8
Re: Splitting Text from a TextBox into an Array
I can think of 2 similar options.
1) use the lines property of the textbox. This propery is an array thats elements contain each line of text in the textbox. If you ended up using my code which puts each item on its own line, then this would work.
2) just use the original array, which is basically the same thing as the lines property of the textbox.
-
Sep 7th, 2010, 09:42 AM
#9
Thread Starter
New Member
Re: Splitting Text from a TextBox into an Array
kleinma,
OK I was thinking along the lines that I would have to write a sequence of code that would count the total number of words and lines in the textbox.
Create an array of these results, then run a string comparison to identify the unique words from the array i.e. singular instances of a word etc
Then create a two dimensional array of the words and the instances where these words pop up...
My head kind of hurts and im tired! Maybe you know of a simpler way to count what line each word comes up on from textbox1?
Thanks again!
-
Sep 7th, 2010, 01:10 PM
#10
Re: Splitting Text from a TextBox into an Array
So do you want to get a count of how many times each word appears, what line each word appears in, or a combination of both?
-
Sep 8th, 2010, 12:42 AM
#11
Thread Starter
New Member
Re: Splitting Text from a TextBox into an Array
kleinma,
Thats right, i would like to get a count of what line each word appears on.
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
|