473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Adding all items to a list box with each item separated by a ;

I have a listbox that the user can add to through a textbox. I would like to string all items in the listbox into a single variable separated by a ;
Dec 17 '15 #1
2 1436
Luk3r
300 256MB
I've included some very basic code with comments. Hope this helps!

Expand|Select|Wrap|Line Numbers
  1.         'testString is the string you wish to build which includes the semicolon
  2.         Dim testString As String
  3.         'endInteger is simply the last listbox item index
  4.         Dim endInteger As Integer = ListBox1.Items.Count - 1
  5.  
  6.         'The for loop creates an integer starting at zero (the first items index), and loops
  7.         'until it reaches endInteger, or the last listbox item index.  We assume that we don't
  8.         'need a semicolon on the last item, hence the need for the If..Else statement.  If we are
  9.         'on the last item, do not include a semicolon.
  10.         For i As Integer = 0 To endInteger
  11.             If i <> endInteger Then
  12.                 testString += ListBox1.Items(i) & ";"
  13.             Else
  14.                 testString += ListBox1.Items(i)
  15.             End If
  16.         Next
  17.  
Dec 21 '15 #2
IronRazer
83 64KB
Use a For Next loop to iterate through the ListBox items and append them to a String. If it is not the last item then append a semi-colon to it too.

As a side note, ListBox Items are Object types and need to be converted to String types. You can use the CStr() function or the ToString method to do that.

Also, you should always use the & character when joining Strings together instead of the + character.

Expand|Select|Wrap|Line Numbers
  1.         Dim SomeString As String = ""
  2.  
  3.         For i As Integer = 0 To ListBox1.Items.Count - 1
  4.             SomeString &= ListBox1.Items(i).ToString
  5.             If i < ListBox1.Items.Count - 1 Then SomeString &= ";"
  6.         Next
  7.  
Dec 28 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: R.G. Vervoort | last post by:
Not sure if it is a php question but since i am working in php here it is. Ho can i add items to (the item label=visual text and the value=index number) a list object on a website. I am having...
2
by: R.G. Vervoort | last post by:
Ho can i add items to (the item label=visual text and the value=index number) a list object on a website. I am having a list with 4 items and i would like to add the text from a textfield...
0
by: hpi | last post by:
Hi, I have 2 listboxes (and some other stuff) on a form The first listbox has an onclick event which, when fired, adds an item to the second list box and sets the selected item to the last item...
3
by: vikram | last post by:
How can i add a user define attribute to each checkbox item in a checkbox list control. I want to add a attribute "Tag". Is it possible
2
by: Robin Tucker | last post by:
Is it possible to embed controls within a listbox item? I'm currently owner-drawing a list box - but I would like to have controls embedded in each list item. Is this possible, or am I in cloud...
1
by: Abhishek | last post by:
I am creating a Datagrid at runtime and I have to create all the columns of grid at runtime... I am facing problem in the item template column. Here I want to place more than one control... For...
0
by: Smokey Grindle | last post by:
This is just a wierd one... I am trying to draw the sub items in the drawitem event of the list view in owner drawn mode (because of documented W32 rendering bugs) instead of in two seperate items...
1
by: Priya | last post by:
I have to display a checboxlist with someitems when each item in a listbox is selected. I have a listbox with a list of categories and when i click each category from the listbox, the checkbox list...
2
myanmaraladin
by: myanmaraladin | last post by:
i recently add two combo boxes in my form. the first combo box contains the categories of movies. and the other one is the list of movies sorted according to the category of movie. i just wondering...
6
by: jase7678 | last post by:
I have a database containing an ItemTypesColors table which has a 1:Many relationship with an ItemDescription table which has a 1:Many relationship with an ItemVendor table. The ItemTypesColors...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.