i trying to do the following.
listboxitem1: Word1 word2 word3
listboxitem1: Word4 word5 word6
listboxitem1: Word7 word8 word9
listboxitem1: Word10 word11 word12 Word13
and so on
Then display in another listbox
Listboxitem2: Word1, Word2
Listboxitem2: Word4, Word5
Listboxitem2: Word7, Word8
Listboxitem2: Word10, Word11
So basically i want to loop through the listbox 1 take every 2 words from each listbox item and add them to listbox2.
Any idea how i can do this?
I'm not sure how much you care about optimization but you can just loop through element as string until space and add it to 2nd listbox. I don't have access to anything I can code in atm but if you're stuck I'll try to help more.
DO NOT DEAL WITH THIS MEMBER
Jun 28, 2017 12:16 PM
(This post was last modified: Jun 28, 2017 12:18 PM by zeroexp.)
Code For Each item As String In ListBox1.Items
Dim items = item.Split(" "C)
ListBox2.Items.Add(items(0) + ", " + items(1))
Next
Jun 28, 2017 12:27 PM
(This post was last modified: Jun 28, 2017 12:30 PM by Mr. Money™.)
(Jun 28, 2017 12:16 PM)zeroexp Wrote: Code For Each item As String In ListBox1.Items
Dim items = item.Split(" "C)
ListBox2.Items.Add(items(0) + ", " + items(1))
Next
the index is outside of array bounds
At this line. ListBox2.Items.Add(items(0) + ", " + items(1))
Post source if you can. Also if you can't make it work try to use regex.
(Jun 28, 2017 12:32 PM)SIM-574702 Wrote: Post source if you can. Also if you can't make it work try to use regex.
There is no source lol. Just added some words to listbox and wrote this code in the button1 field.
Did you create listbox1 and listbox2 ? I'll be home soon to check it out.
(Jun 28, 2017 12:35 PM)SIM-574702 Wrote: Did you create listbox1 and listbox2 ? I'll be home soon to check it out.
Yup created 2 listbox listbox1 contains the words listbox 2 is emty and a button with the code
The only way I got it to crash was to have input without space in it.
Here's what I have. http://prntscr.com/fpasam
Jun 28, 2017 12:45 PM
(This post was last modified: Jun 28, 2017 12:47 PM by HFDone.)
For Each i In ListBox1.Items
ListBox2.Items.Add(i.Split(" ")(0) + ", " + i.Split(" ")(1))
Next
Edit - if you have space at the begining then the indexs are 1 and 2
Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
His code should work, and considering you're getting out of bounds when only the 0,1 index's are being used i'm guessing one of the items in the listbox doesn't contain a space or two words?
Jun 28, 2017 12:48 PM
(This post was last modified: Jun 28, 2017 12:57 PM by Mr. Money™.)
(Jun 28, 2017 12:45 PM)HFDone Wrote: For Each i In ListBox1.Items
ListBox2.Items.Add(i.Split(" ")(0) + ", " + i.Split(" ")(1))
Next
Edit - if you have space at the begining then the indexs are 1 and 2
Gives same error as the other code
(Jun 28, 2017 12:46 PM)Specs Wrote: His code should work, and considering you're getting out of bounds when only the 0,1 index's are being used i'm guessing one of the items in the listbox doesn't contain a space or two words?
it does have spaces the words are as following.
Word 1 has 8 characters and word 2 has 8 to so
ABCDEFGH ABCDEFGH
Some listbox items contain up to 16 words per item.
The only other problem I can think of is the way you're entering data. I assume you're using copy paste or load from file so did you check if space is regular space and not something similar. If all else fails copy paste from spoiler into new project. Just add 2 listbox , 2 buttons and 1 text box.
Usage: enter 1 line into textbox, press button 1. Enter line 2, press button 1. After all lines are entered press button 2
Code Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item As String In ListBox1.Items
Dim items = item.Split(" "c)
ListBox2.Items.Add(items(0) + ", " + items(1))
Next
End Sub
End Class
Silly question: How are you filling listbox1? (From file? From WebRequest? Manually?)
(Jun 28, 2017 01:58 PM)Zilog80 Wrote: Silly question: How are you filling listbox1? (From file? From WebRequest? Manually?)
From file. :P
|