14 Replies - 50026 Views - Last Post: 07 April 2008 - 09:31 AM Rate Topic: -----

#1 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

listbox items to textbox

Posted 04 April 2008 - 09:24 AM

in regard to the post below this one, if anyone could tell me how to display a listboxes selected items in a textbox.
there will be multiple listbox items selected as i have an select-all function.
Is This A Good Question/Topic? 0
  • +

Replies To: listbox items to textbox

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: listbox items to textbox

Posted 04 April 2008 - 10:16 AM

Next time you may not want to ask us to refer to another post in order on the board because if someone replies to the other one, it is bumped to the top of the board, thus not below this one anymore. (Aka topics shift around).

As for your question about getting multiple selected items into a textbox you would do it like this. Lets say you have a button that populates the listbox (that or you do it through the properties window)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add("Item1")
        ListBox1.Items.Add("Item2")
        ListBox1.Items.Add("Item3")
End Sub



Here we put in three items into the listbox. Make sure the listbox is set to a selection mode of "multi simple" or "multi extended" and then you can use the "selectedItems" collection. This collection contains all the items that are selected in a listbox. You can then use a loop to read each selected item and put its text into the textbox like so...

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer = 0

        ' Loop through all the selected items of the listbox and append them to the textbox text property
        For i = 0 To ListBox1.SelectedItems.Count - 1
            TextBox1.Text &= ListBox1.SelectedItems.Item(i).ToString() & " "
        Next
End Sub



As you can see we use a for loop to loop through the selectitems and add each to the textbox1.text property through concatenation. So if we selected item2 and item3 we would see the texbox read "Item2 Item3"

Hopefully that helps you out. Enjoy!

"At DIC we be textbox wrangling, listbox reading, vb.net loving code ninjas!" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

Re: listbox items to textbox

Posted 04 April 2008 - 12:07 PM

View Postkeithmoloney, on 4 Apr, 2008 - 09:24 AM, said:

in regard to the post below this one, if anyone could tell me how to display a listboxes selected items in a textbox.
there will be multiple listbox items selected as i have an select-all function.



im doing that but the listbox items replace each other in the textbox instead of all going in there so i always end up with one even tho i select multiple items
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: listbox items to textbox

Posted 04 April 2008 - 12:31 PM

Notice how I am CONCATENATING them. I am not just setting the textbox, I am adding to it. Notice how I use &= which means take what is in the box and add on to that and assign it back to the textbox. It is shortcut for doing textbox1.text = textbox1.text & ListBox1.SelectedItems.Item(i).ToString() & " "

:)
Was This Post Helpful? 0
  • +
  • -

#5 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

Re: listbox items to textbox

Posted 04 April 2008 - 12:42 PM

oh ok, i couldnt get it working that way.
this worked for me tho

lstFiles.Items.Clear()
		If ofd1.ShowDialog() = _
			Windows.Forms.DialogResult.OK Then
			For Each file_name As String In ofd1.FileNames
				lstFiles.Items.Add(file_name)
				txtList.Text = txtList.Text + Environment.NewLine + file_name
			   Next
		End If



now i just want to add a simple file copy to the loop.
im adding it like so
lstFiles.Items.Clear()
		If ofd1.ShowDialog() = _
			Windows.Forms.DialogResult.OK Then
			For Each file_name As String In ofd1.FileNames
				lstFiles.Items.Add(file_name)
				txtList.Text = txtList.Text + Environment.NewLine + file_name
				File.Copy(file_name, "C:\Documents and Settings\kmoloney\Desktop\kmoloney\RELEASEDtest" & "\" & file_name, True)
			Next
		End If



but it doesnt work. can you see my mistake??
thanks
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: listbox items to textbox

Posted 04 April 2008 - 12:49 PM

Yeah file_name is going to be a complete path. You have to pull off the filename from it and append that to the path you have going there. Otherwise you will have a string that is two paths pasted together.

:)
Was This Post Helpful? 0
  • +
  • -

#7 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

Re: listbox items to textbox

Posted 04 April 2008 - 12:52 PM

dont suppose u could tel me how to do that????
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: listbox items to textbox

Posted 04 April 2008 - 12:59 PM

Check out the Path.GetFileName() method. That is, the GetFileName method of the Path object.

String filename as String

' filename will contain the filename from the path... in this case "filename.txt"
filename = Path.GetFileName("c:\thedirectory\folder\filename.txt")



Enjoy!
Was This Post Helpful? 0
  • +
  • -

#9 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

Re: listbox items to textbox

Posted 04 April 2008 - 01:08 PM

nope, cant do it.
what do i change this line to?

File.Copy(file_name, "C:\Documents and Settings\kmoloney\Desktop\kmoloney\RELEASEDtest" & "\" & file_name, True)

i know i have to change the second file_name to what u wrote but it wont work
last post i promise
Was This Post Helpful? 0
  • +
  • -

#10 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: listbox items to textbox

Posted 04 April 2008 - 01:25 PM

Do you actually have a directory called "RELEASEDtest" or did you mean "RELEASED\test"?
Was This Post Helpful? 0
  • +
  • -

#11 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

Re: listbox items to textbox

Posted 04 April 2008 - 01:31 PM

its called RELEASEDtest. the actual folder that im gonna be copying to with this proj is called RELEASED so i created a test one to mess around with while i try to get my code to do what i want


i got it! thanks a million for all ur help.
this is what i did if ur interested. its probably what u meant anyway

If ofd1.ShowDialog() = _
			Windows.Forms.DialogResult.OK Then
			For Each file_name As String In ofd1.FileNames
				lstFiles.Items.Add(file_name)
				txtList.Text = txtList.Text + Environment.NewLine + file_name
				File.Copy(file_name, "C:\Documents and Settings\kmoloney\Desktop\kmoloney\RELEASEDtest" & "\" & Path.GetFileName(file_name), True)
			Next
		End If


Was This Post Helpful? 0
  • +
  • -

#12 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: listbox items to textbox

Posted 04 April 2008 - 01:42 PM

'Boiled down version for you to play with
If ofd1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            For Each file_name As String In ofd1.FileNames
                File.Copy(file_name, "C:\Documents and Settings\kmoloney\Desktop\kmoloney\RELEASEDtest\" & Path.GetFileName(file_name), True)
            Next
End If




Now that code will work if that path you have there actually exists. It will copy the files you open from the open dialog screen and selected.





View Postkeithmoloney, on 4 Apr, 2008 - 01:31 PM, said:

its called RELEASEDtest. the actual folder that im gonna be copying to with this proj is called RELEASED so i created a test one to mess around with while i try to get my code to do what i want


i got it! thanks a million for all ur help.
this is what i did if ur interested. its probably what u meant anyway

If ofd1.ShowDialog() = _
			Windows.Forms.DialogResult.OK Then
			For Each file_name As String In ofd1.FileNames
				lstFiles.Items.Add(file_name)
				txtList.Text = txtList.Text + Environment.NewLine + file_name
				File.Copy(file_name, "C:\Documents and Settings\kmoloney\Desktop\kmoloney\RELEASEDtest" & "\" & Path.GetFileName(file_name), True)
			Next
		End If



Wtf dude? That is exactly what I told you to do.
Was This Post Helpful? 0
  • +
  • -

#13 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

Re: listbox items to textbox

Posted 07 April 2008 - 07:28 AM

yeah i know! thats why i said "its probably what u meant anyway".

anyways.....if im using the openfiledialog, do u know of a way i can copy everything with the same extension in one go?
Was This Post Helpful? 0
  • +
  • -

#14 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: listbox items to textbox

Posted 07 April 2008 - 09:24 AM

All with one go? The idea of the openfiledialog is so that the user can choose. The only way you can go through everything they select is with a loop like we have been doing. This is considered "All with one go" because the only time the user can then do anything is when the loop is finished. (unless of course it is on a different thread but that is a different story altogether.)

:)
Was This Post Helpful? 0
  • +
  • -

#15 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

Re: listbox items to textbox

Posted 07 April 2008 - 09:31 AM

i have a group of 4 checkboxes. in the folder im copying from there are 4 different file types so each has a checkbox. i want the user to check 1 or more checkboxes, then all the files with that (or those) extensions are copied from that folder to the destination. i dont even necessarily want to use an openfiledialog. i just want to be able to copy certain files in a folder based on their extension.

eg; if the .jpeg and .mp3 checkboxes are checked then all the images and songs from the source folder are copied to the destination folder.
Was This Post Helpful? 1

Page 1 of 1