By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,566 Members | 1,820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,566 IT Pros & Developers. It's quick & easy.

Copy/Cut/Paste

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.