Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing listbox items into two strings 1

Status
Not open for further replies.

xtremeLogic

Programmer
Dec 19, 2003
53
CA
Hi,

I have a listbox that has the following template of values for each row: "Company, Person"
Note: A comma separates the two values

I would like to go through all items in the listbox and store each type of value (only 2 types) in a string separated by a ';'

For example if I had the following listbox entries:
"abc, 123"
"def, 345"
"ghi, 678"

I would like to have it store the information like:
strCompany contents: "abc; def; ghi"
strPerson contents: "123; 456; 789"

Any help on how to get this done would be greatly appreciated. Thanks.
 
That can be done with a For..Each loop:
Code:
Dim sbCompany As New StringBuilder
Dim sbPerson As New StringBuilder
Dim i as Integer

For Each lbi As String in ListBox.Items
  i = lbi.IndexOf(",");
  sbCompany.Append(lbi.SubString(0, i - 1) & "; ")
  sbPerson.Append(lbi.SubString(i) & "; ")
Next

BTW, you did know that you can store an instance of a class in a listbox? Try a class like this:
Code:
Public Class lbiClass
  Public Company As String
  Public Person As String

  Public Override Function ToString() As String
    ToString = Me.Company & ", " & Me.Person
  End Function

End Class
The class will then store your company & person information. The override of ToString is called by the Listbox to display info to the user. When it comes time to build your company and person strings, the code then becomes:
Code:
Dim sbCompany As New StringBuilder
Dim sbPerson As New StringBuilder

For Each lbi As lbiClass in ListBox.Items
  sbCompany.Append(lbi.Company & "; ")
  sbPerson.Append(lbi.Person & "; ")
Next
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks chiph!

The code looks great. Since I am new to vb.net I have some small questions regarding how to send those string builder value to the db:

I have an upload button on my form which sends all of the form values to database. In my db I have the two columns "Company" and "Person" and I would like to store the newly created strings to the respective colums.

I was wondering what would be the best way to do this. I was thinking of calling the above sub in the onclick handler of the upload button. How do I return the two string builder values in the sub and send them as string parameters to my db?

Thanks for the help, I really appreciate it.
 
StringBuilders have a .ToString method (like almost every other class in .NET), and you'd pass that along to your ADO.NET parameter call.

One thing I forgot to mention is after building up the company and person strings, you'll need to remove the extra semi-colon and space from the end. You can set the .Length property to do this:
Code:
  If sbCompany.Length > 2 Then
    sbCompany.Length = sbCompany.Length - 2
  End If
  If sbPerson.Length > 2 Then
    sbPerson.Length = sbPerson.Length -2
  End If
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top