Copy/Cut/Paste

  • Thread starter Thread starter XmlAdoNewbie
  • Start date Start date
X

XmlAdoNewbie

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
 
How many controls do you have that might need to copy from or paste to? It
sounds like you must have several.

Dale
 
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
 
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
 
Erin said:
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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top