newbie question - listbox copy/paste

  • Thread starter Thread starter jh
  • Start date Start date
J

jh

I'd like to copy/paste into a listbox during runtime. I can do this for a
textbox but can't figure out how to accomplish this for a listbox. Any
help? Thanks.
 
I'd like to copy/paste into a listbox during runtime.  I can do this for a
textbox but can't figure out how to accomplish this for a listbox.  Any
help?  Thanks.

To copy a selected listbox item to clipboard, you can use:
Clipboard.SetText(Listbox1.SelectedItem.ToString)

To paste, you can try to use Add or Insert method of Listbox to
include previously copied item into Listbox:
Listbox1.Items.Add(Clipboard.GetText.ToString)

Hope this helps,

Onur Güzel
 
What I really need is to be able to paste into a listbox the same way I
would a textbox. I need to be able to put the cursor into the listbox and
hit paste and copy into it. Either that or I need to be able to select
items or groups of items in a textbox. Why are there different boxes anyway
with different means of access? Is there a control that will allow the
functionality of both the text and the list box?
 
A list box does not support pasting like that. Why are you using a listbox?
Why not just use a text box that consists of multiple lines?

How do you expect the pasting to work? Is it going to be a new list item,
pushing all the others down by one, or are you talking about only pasting
something into an existing item in the list? What happens if the user pastes
multi-line text?
 
You can't directly copy and paste items to listbox, but if you use the code
that i've posted earlier(Clipboard.SetText and GetText methods) with a
ContextMenuStrip control by associating it with Listbox, you can simulate a
kind of way to paste and copy an item by right-clicking on Listbox control.

However, because of it's not an usual thing, it's proper to use it carefully
or not to use that technique unless it's required.

Hope this helps,

Onur Güzel
 
I want to use a listbox because after multi-line text is pasted into it, I
want the user to be able to select multiple lines for processesing. I want
the functionality of both a textbox and a listbox in this regard. I am
frustrated that VB.net doesn't have a control to allow this and I'm trying
to see if I've missed something or if there is a suitable workaround. The
only other thing I can think of is to use a textbox to enter the mulit-line
text, then automatically copy that to a listbox and go from there. Seems
stupid to put an extra box on the form for no other reason.
 
I'm not sure how to use the ContextMenStrip or how that helps here but I'll
try to learn more about it. Thanks.
 
Hi,
To illustrate better what i mean, for example, place a Listbox including its
items and ContextMenuStrip control on your form and add 2 items in it named
"Paste" and "Copy", then double click each and place each proper code(the
code in previous post) in their click event, then through properties window
of Listbox, associate Listbox's ContextMenuStrip property with your
ContextMenuStrip (by default it would be named ContextMenuStrip1).

For example to use copy functionality, select an item in Listbox, then right
click, your ContextMenuStrip will appear, click "Copy" (that is, your
ContextMenuStrip's item) then selected item will be copied to clipboard.
After that you're ready to paste it to any text area or listbox using add or
insert method.

Hope it's clear,

Onur Güzel
 
I think that James and Onur are trying to give you the right help, but here
is how I copy and paste multiline data into a listbox. Basically you listen
for a keyup event in the listbox which is the Ctrl-V, then handle it
accordingly. I split the string on vbcrlf because I assume that is how
items are separated.

Private Sub ListBox1_KeyUp _
(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles ListBox1.KeyUp

If e.Control Then
If e.KeyCode = Keys.V Then
Dim words As String() = Clipboard.GetText.Split(vbCrLf)
Dim word As String

For Each word In words
ListBox1.Items.Add(word)
Next

e.Handled = True
End If
End If

End Sub
 
Back
Top