Hack Forums
[.NET] Take first 2 words from listbox item? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Take first 2 words from listbox item? (/showthread.php?tid=5657679)

Pages: Pages 1 2


Take first 2 words from listbox item? - Mr. Money™ - Jun 28, 2017

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?


* - SIM-574702 - Jun 28, 2017

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.


* - zeroexp - Jun 28, 2017

Code
For Each item As String In ListBox1.Items
      Dim items = item.Split(" "C)
      ListBox2.Items.Add(items(0) + ", " + items(1))
Next



* - Mr. Money™ - Jun 28, 2017

(Jun 28, 2017 12:16 PM)Wops 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))


* - SIM-574702 - Jun 28, 2017

Post source if you can. Also if you can't make it work try to use regex.


* - Mr. Money™ - Jun 28, 2017

(Jun 28, 2017 12:32 PM)matemisic 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.


* - SIM-574702 - Jun 28, 2017

Did you create listbox1 and listbox2 ? I'll be home soon to check it out.


* - Mr. Money™ - Jun 28, 2017

(Jun 28, 2017 12:35 PM)matemisic 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


* - SIM-574702 - Jun 28, 2017

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


* - HFDone - Jun 28, 2017

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


* - Specs - Jun 28, 2017

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?


* - Mr. Money™ - Jun 28, 2017

(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.


* - SIM-574702 - Jun 28, 2017

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)


* - Zilog80 - Jun 28, 2017

Silly question: How are you filling listbox1? (From file? From WebRequest? Manually?)


* - Mr. Money™ - Jun 29, 2017

(Jun 28, 2017 01:58 PM)Zilog80 Wrote:  Silly question: How are you filling listbox1? (From file? From WebRequest? Manually?)

From file. :P