473,419 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Copy/Cut/Paste

Hi All,
I would like to put a method for copy, cut and paste into my
application and this seems to be easy enough except that it's not
working the way i would like it to, I thought someone might be able to
give me a bit of insight and for that I say Thank You in advance!

this is what I have so far.

private void CutText()
{
try
{
Clipboard.SetDataObject(this.ActiveControl.Text);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

private void PasteText()
{
try
{
// Create a new instance of the DataObject interface.
IDataObject data = Clipboard.GetDataObject();
// If the data is text, then set the text of the
// control to the text in the Clipboard.
if (data.GetDataPresent(DataFormats.Text))
this.ActiveControl.Text = data.GetDataDataFormats.Text).ToString();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

private void CopyText()
{
try
{
Clipboard.SetDataObject(this.ActiveControl.Text);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

Here is my problem.

Firstly, I am assuming that the "activecontrol" has a text property
which by all rights it may not. I haven't had an error yet however.

Secondly, I can copy and cut ALL the text in the activecontrol but i
can't copy or cut the "SelectedText" for instance, if my activecontrol
says "Hello World" i can't select "World" and just copy what i have
selected. It takes the whole string.. the same goes for cutting the
text.

If anyone has any insight about this that would be great. It seems
fairly trivial however I am just a beginner with C# and would like to
do it the "right" way!

Thank you
Erin
Nov 15 '05 #1
6 3045
Hi,

If controls which have to provide Copy/Paste inherits from
*TextBoxBase* class then you can call their methods:
Copy(), Cut(), Paste() in other controls you should
provide your own methods.

Regards

Marcin
Nov 15 '05 #2
How many controls do you have that might need to copy from or paste to? It
sounds like you must have several.

Dale

"XmlAdoNewbie" <er************@cowaninsurancegroup.com> wrote in message
news:c9**************************@posting.google.c om...
Hi All,
I would like to put a method for copy, cut and paste into my
application and this seems to be easy enough except that it's not
working the way i would like it to, I thought someone might be able to
give me a bit of insight and for that I say Thank You in advance!

this is what I have so far.

private void CutText()
{
try
{
Clipboard.SetDataObject(this.ActiveControl.Text);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

private void PasteText()
{
try
{
// Create a new instance of the DataObject interface.
IDataObject data = Clipboard.GetDataObject();
// If the data is text, then set the text of the
// control to the text in the Clipboard.
if (data.GetDataPresent(DataFormats.Text))
this.ActiveControl.Text = data.GetDataDataFormats.Text).ToString();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

private void CopyText()
{
try
{
Clipboard.SetDataObject(this.ActiveControl.Text);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

Here is my problem.

Firstly, I am assuming that the "activecontrol" has a text property
which by all rights it may not. I haven't had an error yet however.

Secondly, I can copy and cut ALL the text in the activecontrol but i
can't copy or cut the "SelectedText" for instance, if my activecontrol
says "Hello World" i can't select "World" and just copy what i have
selected. It takes the whole string.. the same goes for cutting the
text.

If anyone has any insight about this that would be great. It seems
fairly trivial however I am just a beginner with C# and would like to
do it the "right" way!

Thank you
Erin

Nov 15 '05 #3
So does this mean that i will have to have a separate function for each
control that i might want to copy from? how would you go about that?
Would you put the code for copy and paste into the gotfocus() event? I
was hoping to have one method that i could call when the copy, cut or
paste button was clicked.
I have a lot of controls that i want to be able to copy/cut and paste
from and they aren't all necessarily textboxes.
Thanks for your quick replies!!
Erin
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
Erin Sebastian wrote:
So does this mean that i will have to have a separate function for each
control that i might want to copy from? how would you go about that?
Would you put the code for copy and paste into the gotfocus() event? I
was hoping to have one method that i could call when the copy, cut or
paste button was clicked.
I have a lot of controls that i want to be able to copy/cut and paste
from and they aren't all necessarily textboxes.


Let me show you something:

private void CopyText()
{
if( this.ActiveControl is TextBoxBase ) {
((TextBoxBase) this.ActiveControl).Copy();
}
else {
try
{
Clipboard.SetDataObject(this.ActiveControl.Text);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}

Pay attention that TextBoxBase support *Undo* method.

See You!

Marcin
Nov 15 '05 #5
Thank you so much, that is Excellent. The only thing to do now is to be
able to copy or cut only the selected text. I tried to say:

((TextBoxBase) this.ActiveControl).SelectedText.Copy()

however the property SelectedText does not have a Copy() method. Any
ideas for this?

Erin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6
Hi Erin,
Thank you so much, that is Excellent. The only thing to do now is to be
able to copy or cut only the selected text. I tried to say:

((TextBoxBase) this.ActiveControl).SelectedText.Copy()

however the property SelectedText does not have a Copy() method. Any
ideas for this?


You don't need to *point* at SelectedText if you are using
TextBoxBase's *Copy()* method.

private void textBoxItem_KeyDown(object sender
, System.Windows.Forms.KeyEventArgs ea) {
TextBoxBase txtBoxItem=(TextBoxBase) sender;
if( ea.Control ) {
switch( ea.KeyCode ) {
case Keys.C:
txtBoxItem.Copy();
MessageBox.Show( this
, String.Format("Copied text=\'{0}\'"
,
Clipboard.GetDataObject().GetData(typeof(string)) ) );
break;
}
}
}
You should write your own copy/cut/paste methods for controls other
than *TextBoxBase*-based (e.g. ComboBoxes). Then you can use the
*SelectedText* or *SelectionStart* and *SelectionLength* properties.

Marcin
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Legendary Pansy | last post by:
I was checking out the 101 C# Samples, specifically Windows Forms - Use the Clipboard. I took a look at the code for a while, and I understand what the program is doing with the cut, copy, pasting...
3
by: Rachel Suddeth | last post by:
This may not be the right forum, but it's a problem I chiefly come across when trying to post here. When I do a copy/paste from VS, the text always looks really weird (and even if I'm in an...
2
by: Matt | last post by:
Hello, I have a copy button and a paste button. What code should I add to the copy button and the paste button to do it's work? Thanks, Matt
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
0
by: jshoffner | last post by:
This sounds like a really silly question but I'm can't find a solution anywhere. I use the Search window within VS.NET 2003 all the time. However, when I find a sample that is usefull I would like...
5
by: Kaur | last post by:
Hi, I have been successful copying a vba code from one of your posts on how to copy and paste a record by declaring the desired fields that needs to be copied in form's declaration and creating two...
6
by: Ben R. | last post by:
Hi, I've got a vb.net winforms app. Out of the box, I can use Ctrl X, C and V as expected in controls like textboxes. I've got a menustrip, and if I click the link "Add standard items" which...
17
by: Steve | last post by:
I'm trying to code cut, copy, and paste in vb 2005 so that when the user clicks on a toolbar button, the cut/copy/paste will work with whatever textbox the cursor is current located in (I have...
5
by: phill86 | last post by:
Hi I have a main form that holds records for scheduled meetings, date time location etc... in that form i have a sub form that has a list of equipment resources that you can assign to the meeting in...
8
by: jh | last post by:
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.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.