Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Take first 2 words from listbox item?

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.

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: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

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
[Image: bitcoin.png] 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: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.

Spoiler (Click to View)

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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Natus12345Dec 13, 2015 11:06 AM 10 320 Dec 13, 2015 11:06 AM
Last Post: Natus12345
[.NET] by Mr. Money™Dec 3, 2015 01:03 PM 12 422 Dec 3, 2015 01:03 PM
Last Post: Odus
[C#] by GeekieNov 3, 2015 11:37 AM 3 121 Nov 3, 2015 11:37 AM
Last Post: Geekie