We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Code for Cut, Copy and Paste for a VB.NET form

Medium Priority
1,965 Views
Last Modified: 2012-05-07
Hi folks,
You know here triyng to get better in VB.NET.. ;P
I just need some advice. I noticed that when I created my MDI(Parent) form, none of the basic edit buttons like CUT, COPY and PASTE on the menu are pre-coded. I'm pretty sure that there is a reson why. I tried to use My.Computer but I dont know how to start. I noticed that when you run the app you can actually use the right clink of the mouse on top of the text fields and Windows will popup a little menu with the things that I need but I think that it is part of the OS responsability, right? Anyway...as far as the menus and CTRL combinations (CTRL+C,etc...)

Anyone can guide me?

Thanks!

Armando
Comment
Watch Question

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
Cut, copy and paste functions are your responsibility. OS cannot know how to copy/paste an object defined by you.

Author

Commented:
Hi Jamie,

I knew that. I just need a sample of the code for those... do you have some? All my objects are text boxes....

Armando
Top Expert 2009

Commented:
If I understand you correctly, you want to copy the string or strings from the textedit boxes to be copied automatically (or on a button/menu click) to the clipboard. You can do so by executing the following line of code:

Clipboard.SetText(txtMyTextbox.Text)

Author

Commented:
Hi Abel,

Right on the dot! That is my undestanding about on how to use the clipboard. My problem is how do you identify the selected text in all the text fields? I got 30+...Do I need to put code behind every text box? Or can we use a Funtion or something that can copy the select text and move it to the clipboard with your comand? How would you do it?

Thanks for the reply!

Armando
Top Expert 2009

Commented:
I don't know how you reference the textboxes, but usually, only the currently selected textbox has focus and a selection and when you move the cursor to another textbox, the previous selection is void.

To get the selection from a textbox, you can use something like:

TextBox1.SelectedText

To get all textboxes in a form (assuming they are not hidden in usercontrols) you can do the following (included is code to add it to the clipboard)

' put all selected text of all textboxes inside the clipboard '
Dim sb As New StringBuilder
For Each ctl In Me.Controls
    If TypeOf ctl Is TextBox Then
        Dim tb As TextBox = CType(ctl, TextBox)
        ' append the selected text, if any '
        sb.Append(tb.SelectedText)
    End If
Next
Clipboard.SetText(sb.ToString())

Open in new window

Author

Commented:
Hi Abel,
Thanks for your help so far. I got your code implemented but it still not catching the SELECTED TEXT. First I try your code just like you posted it and it didn't work. I'm using VB 2008 BTW. There I made some modifications base on the errors and I finish with something like this:

Still not catching anithing. The problem is that if it doesn't catch the selected text. The clipboard insert run a null exeption and stops. Any Ideas?
Dim sb As System.Text.StringBuilder
        For Each ctl In MainProjects.Controls
            If TypeOf ctl Is TextBox Then
                Dim tb As TextBox = CType(ctl, TextBox)
                ' append the selected text, if any '
                If tb.Text <> "" Then
                    sb.Append(tb.SelectedText)
                End If
            End If
        Next
        Clipboard.SetText(sb.ToString())

Open in new window

Top Expert 2009

Commented:
> I made some modifications base on the errors
What modifications and what errors? Are you not running it from your form? What is MainProjects? Does it have any textboxes?

> The problem is that if it doesn't catch the selected text. The clipboard insert run a null exeption and stops. Any Ideas?

Then there's no text selected. But like I said, when focus changes, the text is unselected. Can you tell a bit more on what you are trying to accomplish? You said something about a menu. Are you referencing the correct controls and do the controls contain selected text? I.e., is it blue?

You can add a simple if-statement against the error:

If String.IsNullOrEmpty(tb.SelectedText) Then
   ....
End If

As an alternative, consider using richtextboxes. You can capture the SelectionChanged event. A textbox doesn't have that event.

-- Abel --


Author

Commented:
Abel,

Sorry for the confusion. Ok, I have a MDI Form with the Menu EDIT>COPY. Main Projects is a Child form on it. That one contains all the textboxes...I'm putting this on the click event of the menu. Like you said, it make sense that the text gets unselected once you click somewhere else. Yep, it's blue and it's a  Menu Strip

What is your Advice?
Top Expert 2009

Commented:
> Yep, it's blue and it's a  Menu Strip
you mean the menu is blue? I was talking about the selected text: how do you now it is selected? Normally, text that is selected is blue. When you click Ctrl-C it is in the clipboard. When you move to another window, it is deselected (well, at least with a textbox).

If you use richtextboxes instead you can use the SelectionChanged event to keep track of the selection. There's also a Win32 property you can use to keep the selection when loosing focus, but not sure you want to go that path with p/invoke and everything.
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks Abel :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.