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 separate 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.
 
Here's an idea:
Code:
		<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
			<asp:ListBox ID=&quot;lst&quot; Runat=&quot;server&quot; SelectionMode=&quot;Multiple&quot; Rows=&quot;6&quot;>
				<asp:ListItem Value=&quot;aaa,111&quot;>aaa,111</asp:ListItem>
				<asp:ListItem Value=&quot;bbb,222&quot;>bbb,222</asp:ListItem>
				<asp:ListItem Value=&quot;ccc,333&quot;>ccc,333</asp:ListItem>
				<asp:ListItem Value=&quot;ddd,444&quot;>ddd,444</asp:ListItem>
				<asp:ListItem Value=&quot;eee,555&quot;>eee,555</asp:ListItem>
				<asp:ListItem Value=&quot;fff,666&quot;>fff,666</asp:ListItem>
			</asp:ListBox>
			<br>
			<asp:Button ID=&quot;btn&quot; Runat=&quot;server&quot; Text=&quot;Ok&quot;></asp:Button>
		</form>

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
        Dim strCompany As String = &quot;&quot;
        Dim strPerson As String = &quot;&quot;
        Dim lstItem As ListItem
        Dim x As Integer
        Dim y As Integer
        For Each lstItem In lst.Items
            If lstItem.Selected Then
                x = lstItem.Value.IndexOf(&quot;,&quot;)
                y = lstItem.Value.Length
                strCompany = strCompany & lstItem.Value.Substring(0, x) & &quot;;&quot;
                strPerson = strPerson & lstItem.Value.Substring(x + 1) & &quot;;&quot;
            End If
        Next
        If Not strCompany = &quot;&quot; Then
            strCompany = strCompany.Remove(strCompany.Length - 1, 1)
        End If
        If Not strPerson = &quot;&quot; Then
            strPerson = strPerson.Remove(strPerson.Length - 1, 1)
        End If
        Response.Write(strCompany & &quot;<br>&quot; & strPerson)
    End Sub

I hope this helps!

[yawn] [morning] [yawn]
 
Thanks Joulius, excellent code, it really helped!
I got another related question, but first let me explain the scenario:

In my form I have two textboxes: Company, Person and a sendList listbox. What happens is after u enter values in the textboxes u press an add button which places the values joined as a string in the listbox. So the listbox might have the following item values:
&quot;abc, 123&quot;
&quot;def, 456&quot;
&quot;ghi, 789&quot;
Now I am able to extract the values in the listbox into two separate strings so I can save them to table in the appropriate columns.

My question is, how can I repopulate the listbox based on the values in my table columns: Company, Person.
For example, the above list item values would be stored in the columns as:
Company: &quot;abc; def; ghi;&quot;
Person: &quot;123; 456; 789;&quot;
So I would like these values to be put back into the list item for editing / deleting.

Any ideas would be greatly appreciated, Thanks!
 
I need to know what is the structure and type of your database. Does one record look like this:
Company Person
rec 1 abc 123
rec 2 def 456
.....

or like this:

Company Person
rec 1 abc;def;ghi 123;456;789
.....
?
 
Thanks Joulius for all the suggestions u've given:

I actually solved my problem of populating the listbox with data from the database. My db structure was such that the columns had the following style of data:
For each Record:
Company: c1; c2; c3
Person; p1; p2 p3

Here's the sub I used to populate the listbox based on these values, hopefully it'll help someone out:
Code:
'Populate Listbox with values from two separate strings
Private Sub fillToList(ByVal strToCompany As String, ByVal strToPerson As String)
  'General Algorithm:
  'Create 2 String Array to store lists
  'Split strings into arrays
  'Iterate through 1st array and append to each row,
   the corresponding row value of second array.
   'Add 1st array elements into listbox 
            Dim i As Integer
            Dim companyList() As String
            Dim personList() As String

            companyList = strToCompany.Split(&quot;; &quot;)
            personList = strToPerson.Split(&quot;; &quot;)

            For i = 0 To companyList.Length - 1
                companyList(i) = companyList(i) + &quot;, &quot; + 
                                 personList(i)
                lbToCompany.Items.Add(companyList(i).
                                      ToString)
            Next i
        End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top