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

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
angelarmandoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
Cut, copy and paste functions are your responsibility. OS cannot know how to copy/paste an object defined by you.
0
angelarmandoAuthor 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
0
abelCommented:
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)
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

angelarmandoAuthor 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
0
abelCommented:
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

0
angelarmandoAuthor 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

0
abelCommented:
> 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 --


0
angelarmandoAuthor 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?
0
abelCommented:
> 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.
0
abelCommented:
ah wait, my mistake, it is called "HideSelection". Set it to false and your selection will not be hidden.

Then, just test for whether it is null or empty (as explained above) for the text controls you want to use in the Copy action and you should be all set.

' prevent hiding selection '
textBox1.HideSelection = False

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
angelarmandoAuthor Commented:
Thanks Abel :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.