Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-06-29
11
Medium Priority
?
1,117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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
 

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 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

604 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