Solved

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

Posted on 2009-06-29
11
779 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
0
Comment
Question by:angelarmando
  • 5
  • 5
11 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24739148
Cut, copy and paste functions are your responsibility. OS cannot know how to copy/paste an object defined by you.
0
 

Author Comment

by:angelarmando
ID: 24739228
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
 
LVL 39

Expert Comment

by:abel
ID: 24739474
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
 

Author Comment

by:angelarmando
ID: 24739739
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
 
LVL 39

Expert Comment

by:abel
ID: 24739813
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:angelarmando
ID: 24741280
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
 
LVL 39

Expert Comment

by:abel
ID: 24742877
> 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
 

Author Comment

by:angelarmando
ID: 24746054
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
 
LVL 39

Expert Comment

by:abel
ID: 24746599
> 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
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24746637
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
 

Author Comment

by:angelarmando
ID: 24747945
Thanks Abel :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now