-
Aug 18th, 2021, 03:21 AM
#1
Thread Starter
Lively Member
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!❤️
-
Aug 18th, 2021, 03:29 AM
#2
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.
-
Aug 18th, 2021, 04:58 AM
#3
Re: copy a specific item from listbox a textbox
writing this
Originally Posted by danzey
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)
-
Aug 18th, 2021, 05:01 AM
#4
Re: copy a specific item from listbox a textbox
Originally Posted by Delaney
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.
-
Aug 18th, 2021, 07:47 AM
#5
Thread Starter
Lively Member
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.
-
Aug 18th, 2021, 09:51 AM
#6
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)
-
Aug 18th, 2021, 10:11 AM
#7
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.
-
Aug 18th, 2021, 10:30 AM
#8
Thread Starter
Lively Member
Re: copy a specific item from listbox a textbox
Originally Posted by dday9
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...
-
Aug 18th, 2021, 10:40 AM
#9
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.
-
Aug 18th, 2021, 11:40 AM
#10
Thread Starter
Lively Member
Re: copy a specific item from listbox a textbox
Originally Posted by dday9
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!👍❤️
-
Aug 18th, 2021, 01:02 PM
#11
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)
-
Aug 18th, 2021, 02:47 PM
#12
Thread Starter
Lively Member
Re: copy a specific item from listbox a textbox
Originally Posted by Delaney
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!
Originally Posted by dday9
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!👍❤️
-
Aug 18th, 2021, 03:07 PM
#13
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)
-
Aug 18th, 2021, 03:36 PM
#14
Thread Starter
Lively Member
Re: copy a specific item from listbox a textbox
Originally Posted by Delaney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|