Results 1 to 14 of 14

Thread: copy a specific item from listbox a textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Exclamation copy a specific item from listbox a textbox

    Hello! I have a Real problem!
    I would like do thisretrieve the text)
    Textbox4.text= listbox1.items(1)
    But it dont work ?!!
    I looked for in the web a way but i failed every time!
    So i Ask here some help🥺 !
    Thank you in advance for your answer!❤️

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: copy a specific item from listbox a textbox

    "it won't work" could mean any number of things. You need to actually explain the issue in detail, including what actually happens. If there's an error message, tell us what it is. The code you have would work under certain circumstances and not others, but you've not bothered to tell us what the actual circumstances are here. Most importantly, how EXACTLY was the ListBox populated in the first place and what EXACTLY do you expect to happen. You know, the information relevant to the issue. We could guess at a solution and we might even guess right, but we might also guess wrong. Just don't make us guess at all.

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: copy a specific item from listbox a textbox

    writing this

    Quote Originally Posted by danzey View Post
    Textbox4.text= listbox1.items(1)
    means you have option strict = off else it gives to you an error ("implicit conversion from object to string") so you should write :
    Code:
    Textbox4.text= listbox1.items(1).tostring
    second, do you have at least 2 items in your listbox ?
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: copy a specific item from listbox a textbox

    Quote Originally Posted by Delaney View Post
    you should write :
    Code:
    Textbox4.text= listbox1.items(1).tostring
    That may or may not work, depending on how the list was populated and what the items actually are. If it was bound to a DataTable then that won't be all that useful.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: copy a specific item from listbox a textbox

    second, do you have at least 2 items in your listbox ?
    i have at least one item but this item(s) are names in a simple list.

    after, i have to try this :
    Code:
    TextBox4.Text = ListBox1.GetItemText(ListBox1.Items(1))
    edit: this code above doesn't work!

    option strict = off , must i change on ON! this setting is important?
    Last edited by dday9; Aug 18th, 2021 at 10:09 AM.

  6. #6
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: copy a specific item from listbox a textbox

    We need the description of the error to know exactly what happens not just "it doesn't work".

    yes you should put option strict = on, that will force you to write correct code and do the correct conversions yourself and not expecting the machine to do it for you.
    If you have only one item
    Code:
    Textbox4.text= listbox1.items(1).tostring
    will generate an error as the list start at 0
    Last edited by Delaney; Aug 18th, 2021 at 09:56 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: copy a specific item from listbox a textbox

    The ListBox's Text property (documentation) may be what you're wanting, though admittedly I am making several assumptions.

    Try using the following code:
    Code:
    TextBox4.Text = ListBox1.Text
    Does this provide you with the expected result?

    Also, I wanted to mention that if you want to keep your code formatted then please use the [CODE] BB tag. There is more information in my signature on code tags.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: copy a specific item from listbox a textbox

    Quote Originally Posted by dday9 View Post
    The ListBox's Text property (documentation) may be what you're wanting, though admittedly I am making several assumptions.

    Try using the following code:
    Code:
    TextBox4.Text = ListBox1.Text
    Does this provide you with the expected result?

    Also, I wanted to mention that if you want to keep your code formatted then please use the [CODE] BB tag. There is more information in my signature on code tags.
    i would like to write one item from listbox to textbox .
    example: if the listbox.item(1) = franck the textox.text = "frank"
    or if the listbox.item(5) = Sarah the textox.text = "Sarah"
    it looks simple but in real, it is a bit hard!
    i am trying to uest this by Delaney:
    Textbox4.text= listbox1.items(1).tostring
    wait and see...

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: copy a specific item from listbox a textbox

    I don't think you took any of my advice ranging from using the Text property to proper forum etiquette by using code tags. If you cannot follow those simple instructions then I apologize, but I must remove myself from this thread.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: copy a specific item from listbox a textbox

    Quote Originally Posted by dday9 View Post
    I don't think you took any of my advice ranging from using the Text property to proper forum etiquette by using code tags. If you cannot follow those simple instructions then I apologize, but I must remove myself from this thread.
    Yes i Saw it ! Don't worry! I am working now! I can't test every answer !👍thank you for your help! I will glance later!👍❤️

  11. #11
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: copy a specific item from listbox a textbox

    so if I understand well, you have a listbox with names. When you select a name in the listbox, you want to show it in a textbox, right ?
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  12. #12

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: copy a specific item from listbox a textbox

    Quote Originally Posted by Delaney View Post
    so if I understand well, you have a listbox with names. When you select a name in the listbox, you want to show it in a textbox, right ?
    Yes that 's it!
    Quote Originally Posted by dday9 View Post
    I don't think you took any of my advice ranging from using the Text property to proper forum etiquette by using code tags. If you cannot follow those simple instructions then I apologize, but I must remove myself from this thread.
    Yes i Saw it ! Don't worry! I am working now! I can't test every answer !👍thank you for your help! I will glance later!👍❤️

  13. #13
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: copy a specific item from listbox a textbox

    Create a new project with a form, a texbox and a listbox

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim list_name As New List(Of String) From {"Pierre", "Paul", "Jaques"} 'just a way to create the list of name, there are plenty other way
            ListBox1.DataSource = list_name 'bind the list to the listbox 
        End Sub
    
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim lb As ListBox = DirectCast(sender, ListBox)
           
            If lb.SelectedIndex > -1 Then
                TextBox1.Text = lb.Items(lb.SelectedIndex).ToString
            End If
        End Sub
    End Class
    Last edited by Delaney; Aug 18th, 2021 at 03:12 PM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  14. #14

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: copy a specific item from listbox a textbox

    Quote Originally Posted by Delaney View Post
    Create a new project with a form, a texbox and a listbox

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim list_name As New List(Of String) From {"Pierre", "Paul", "Jaques"} 'just a way to create the list of name, there are plenty other way
            ListBox1.DataSource = list_name 'bind the list to the listbox 
        End Sub
    
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim lb As ListBox = DirectCast(sender, ListBox)
           
            If lb.SelectedIndex > -1 Then
                    Dim lb As ListBox = DirectCast(sender, ListBox)
           
            If lb.SelectedIndex > -1 Then
                TextBox1.Text = lb.Items(lb.SelectedIndex).ToString
            End If
            End If
        End Sub
    End Class
    THANK YOU very much! i see what i must do hence my code is inside a "timer". So, the question is "what term must i move out "lb.selecteindex" to have the same result? the listbox1_selectedindex is useless in my case(the index is not unknown).i suggest to put :
    Dim lb As ListBox = DirectCast(sender, ListBox)

    If lb.SelectedIndex > -1 Then
    TextBox1.Text = lb.Items(listbox1.items(0)).ToString
    End If

    other try:

    Dim t As Integer
    Dim name(t) As String

    For t = 1 To t = 13
    name(t) = ListBox7.Items(t)
    Next
    TextBox1.Text = name(0)

    i thank you again! we advance quickly!
    Last edited by danzey; Aug 18th, 2021 at 05:10 PM.

Tags for this Thread

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