Jump to content
Xtreme .Net Talk

From ListBox to TextBox


Recommended Posts

Guest AnakinVB
Posted (edited)

Hello,

As you can see if you try out my code (below) I am able to populate a list box from whatever the user enters into a text box. However, I haven't quite gotten it to work in the reverse direction.

 

How can I populate a text box by single or double-clicking an item on a list box?

Here's what I've got so far:

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       TextBox2.Text = Cstr(ListBox1.Items())

   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       ListBox1.Items().Add(TextBox1.Text)
   End Sub

Any .NET suggestions?

thx,

Anakin

Edited by Derek Stone
Guest AnakinVB
Posted (edited)

Thanks divil

 

I will try to use the VB tags in this post. I'm guessing that I should use them like HTML tags. Feel free to show me the correct way if I get it wrong.

 

Anyway, now that I can populate my text box from my list box, I am trying to add in some error checking. If the list is empty, and you click on the listbox, my program crashes. I have made several different attempts to fix this. Now, I just get the message box no matter what.

 

**How can I fix this so that

a) the message box pops up only when the listbox is empty?

and

b) the text box populates when it is supposed to - i.e. the user has clicked on a list item?

 

Here is my latest .NET code:

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       ListBox1.Items().Add(TextBox1.Text)
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       If ListBox1.SelectedIndex > Nothing Then
           TextBox2.Text = CType(ListBox1.Items(ListBox1.SelectedIndex), String)
       Else
           MsgBox("Items must be there to select them")
       End If

 

thx,

Anakin

Edited by Derek Stone
Guest AnakinVB
Posted

Thanks divil, that worked. For the sake of testing the vb tags, I'm going to paste my working code here:

 

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       ListBox1.Items().Add(TextBox1.Text)
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       If ListBox1.SelectedIndex() = -1 Then
           MsgBox("Items must be there to select them")
       Else
           TextBox2.Text = CType(ListBox1.Items(ListBox1.SelectedIndex), String)
       End If

 

thx!

Anakin

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...