Results 1 to 7 of 7

Thread: ListBoxes

  1. #1

    Thread Starter
    Member Comn8u's Avatar
    Join Date
    Jul 2004
    Posts
    35

    Red face ListBoxes

    How would I take results from a database and make ListItems out of them?

    What I am trying to do is populate a listbox with database results. I can populate the listbox no problem, but when I try to add a value from listbox1 into listbox2, it's not going through. It has no index to go along with the value. How would I go about repairing this?

  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    post some code. are you using listBox.items.item(index) yadda yadda? how are you going about copying the values?

  3. #3

    Thread Starter
    Member Comn8u's Avatar
    Join Date
    Jul 2004
    Posts
    35

    Listbox code

    Dim conPubs As SqlConnection
    Dim cmdSelectAuthors As SqlCommand
    Dim dtrAuthors As SqlDataReader
    conPubs = New SqlConnection(ConfigurationSettings.appSettings("STDConnect"))
    conPubs.Open()
    cmdSelectAuthors = New SqlCommand("Select * from Email", conPubs)
    dtrAuthors = cmdSelectAuthors.ExecuteReader()
    While dtrAuthors.Read()
    Listbox1.Items.Add(dtrAuthors("email"))
    End While
    dtrAuthors.Close()
    conPubs.Close()

    Would I need to add a index value along with this? Because right now, the values populate inside the listbox, but can't take the values from this listbox and add them to another listbox.

  4. #4
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    VB Code:
    1. ListBox2.Items.Add(ListBox1.Items.Item(index))

    Yeah, you need to specify which item in box 1 to copy into box 2.

    i hope that's what you're looking for.

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    one other thing. You're not using the .Item property of the reader to add the items. I didn't know you could do that. Or is that a typo?

    I've always done it like this:
    VB Code:
    1. reader.item("Something")

  6. #6

    Thread Starter
    Member Comn8u's Avatar
    Join Date
    Jul 2004
    Posts
    35

    Reader.item

    Actually, what you did just worked. I added Item to the reader, which worked.

    I also had my code in the body then on top because I was able to get Sub Page_Load to work. It turned out that I had to delete the top highlighted yellow "Page language=VB" for it to work and it did.

    Thanks for your help!!

    Now, how would I get all of the items out of the listbox to store it into one variable?

    I would like to use the selected e-mails to store into one variable so I can add it to the e-mail function.

  7. #7
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    you can try this:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         CheckedListBox1.Items.Add("hello", CheckState.Checked)
    3.         CheckedListBox1.Items.Add("goodbye", CheckState.Checked)
    4.  
    5.     End Sub
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         Dim emails As New ArrayList
    9.         emails.AddRange(CheckedListBox1.CheckedItems)
    10.         For Each s As String In emails
    11.             Console.WriteLine(s)
    12.  
    13.         Next
    14.     End Sub
    try that out by creating a new project, add a checked list box and abutton to the blank form and run it. that should give you an idea of how to do that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width