Solved

How to pass data from Listbox to a textbox

Posted on 2012-04-04
8
313 Views
Last Modified: 2012-04-04
Hello,

I am using the code below to pass data from my testbox to a list box, How do I pass the data from my listbox to a textbox separated by a (;).

C1NSN.Text = dtNSN.Rows(0).Item("NSN")
        Dim tmp() As String = C1NSN.Text.split(";")
        For i As Integer = 0 To tmp.Length - 1
            ListBox1.Items.Add(tmp(i))
         Next


Thanks,

Victor
0
Comment
Question by:vcharles
  • 3
  • 3
  • 2
8 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 37805570
Try something like this:

C1NSN.Text = ""
For i = 0 to listbox.items.count-1
   C1NSN.Text = C1NSN.Text & ";" & ListBox1.Items(0).Text
next i

Open in new window


Hope it helps
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37805602
Do:
For x = 0 To LBox.Items.Count - 1
    C1NSN.Text = If(String.IsNullOrEmpty(C1NSN.Text.ToString.Trim), LBox.Items(x), C1NSN.Text & ";" & LBox.Items(x))
Next

Open in new window

0
 

Author Comment

by:vcharles
ID: 37805720
Thank you, I will try them and get back you. How do I accomplish the same with all the rows in a  GridView?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vcharles
ID: 37805786
Hi,

I tried both of them but it is still not working, the following code:

C1NSN.Text = ""
For i = 0 to listbox.items.count-1
   C1NSN.Text = C1NSN.Text & ";" & ListBox1.Items(0).Text
next i


Gives me the data of the first row multiple times, for example if my list box contains

AAAAA
BBBBBB
CCCCCC

My textbox shows:
AAAAA
AAAAA
AAAAA


The following code:

For x = 0 To LBox.Items.Count - 1
    C1NSN.Text = If(String.IsNullOrEmpty(C1NSN.Text.ToString.Trim), LBox.Items(x), C1NSN.Text & ";" & LBox.Items(x))
Next

Give me error message:

Operator '&' is not defined for types 'String' and 'System.Web.UI.WebControls.ListItem'.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37805862
get the listbox item text instead
For x = 0 To ListBox1.Items.Count - 1
    C1NSN.Text = If(String.IsNullOrEmpty(C1NSN.Text.ToString.Trim), ListBox1.Items(x).Text, C1NSN.Text & ";" & ListBox1.Items(x).Text)
Next

Open in new window

0
 
LVL 15

Accepted Solution

by:
gplana earned 500 total points
ID: 37805885
Sorry, instead of Items(0) put Items(i):

C1NSN.Text = ""
For i = 0 to listbox.items.count-1
   C1NSN.Text = C1NSN.Text & ";" & ListBox1.Items(0).Text
next i
C1NSN.Text = Mid(C1NSN.Text,2) 'This is to eliminate first ";"

Open in new window

0
 

Author Comment

by:vcharles
ID: 37805905
It worked! Thanks!

Do you know how to also do the same with a gridwiew?

Victor
0
 
LVL 15

Expert Comment

by:gplana
ID: 37806012
Happy to know it worked. Sorry, I haven't work with gridview. I suggest to open another question for this and add the code you have on this new question.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question