Solved

Copy, Cut, Paste and the Clipboard... easy points here

Posted on 2006-11-30
14
8,287 Views
Last Modified: 2008-09-18
Ok, I'm pretty new to C# and I'm writing a simple program for songwriters/composers for identifying compatible music keys.  Anyway, I'm now stuck in trying make copy, cut and paste work from a menu bar.  Here's what I have:

       private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetText(this.ActiveControl.Text);
            this.ActiveControl.Text = "";
        }
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetText(this.ActiveControl.Text);
        }
        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ActiveControl.Text = Clipboard.GetText();
        }

The program builds fine, but when I run it and click cut or copy FOR SECOND TIME, the program locks up.  My theory is that I'm cutting or copying to the clipboard, but doing so again screws up the clipboard because it hasn't cleared the original copy/cut text.  Any ideas?  (previously, i tried the program without the "Clipboard.Clear()" to no avail.)
0
Comment
Question by:coolrazor
  • 4
  • 3
  • 2
  • +1
14 Comments
 
LVL 5

Expert Comment

by:Collindsouza
ID: 18050642
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetText(this.ActiveControl.Text);
            this.ActiveControl.Text = "";
        }
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetDataObject(this.ActiveControl.Text) //-------------------------------> missing line for copy
            Clipboard.SetText(this.ActiveControl.Text);
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ActiveControl.Text = Clipboard.GetText();
        }

The SetDataObject can take a second argument, which you should set to True if you want to make the copied object available after the current program terminates:

        ' Make the copied text available after the application ends.
        Clipboard.SetDataObject(t, True)


0
 
LVL 5

Expert Comment

by:Collindsouza
ID: 18050648
sorry my bad..

 ' Make the copied text available after the application ends.
        Clipboard.SetDataObject(this.ActiveControl.Text, True)
0
 

Author Comment

by:coolrazor
ID: 18050804
     private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetDataObject(this.ActiveControl.Text) //-------------------------------> missing line for copy
            Clipboard.SetText(this.ActiveControl.Text);
        }

That didn't work, the program hangs on the "Clipboard.SetText(this.ActiveControl.Text);"  It does the same for the Cut.  I'm missing something simple, because I just want the basic copy, cut, paste functionality for whatever text is selected in the program.  

Also, where does the "Clipboard.SetDataObject(this.ActiveControl.Text, True)" go?
0
 

Author Comment

by:coolrazor
ID: 18050949
Also, what's the syntax for closing the current form?

buttonClick
{
     activeform.close;
}

-?  (just a bonus question)
0
 

Author Comment

by:coolrazor
ID: 18050959
oops, forgot to add bonus points.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:Expert1701
ID: 18051829
To expand on Collindsouza's solution, try this:

  private void cutToolStripMenuItem_Click(object sender, EventArgs e)
  {
    Clipboard.SetDataObject(ActiveControl.Text, true);
    ActiveControl.Text = "";
  }

  private void copyToolStripMenuItem_Click(object sender, EventArgs e)
  {
    Clipboard.SetDataObject(ActiveControl.Text, true);
  }

  private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
  {
    ActiveControl.Text = (string)Clipboard.GetDataObject().GetData(typeof(string));
  }

> Also, what's the syntax for closing the current form?

  private void button1_Click(object sender, EventArgs e)
  {
    Close();
  }
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 18052415
From Menu

Cut is:- richTextBox.Cut();
Copy is:-   Clipboard.SetData(DataFormats.Rtf, richTextBox.SelectedRtf);
Paste is:-
            if (Clipboard.ContainsText(TextDataFormat.Rtf))
            {
                richTextBox.SelectedRtf = Clipboard.GetData(DataFormats.Rtf).ToString();
            }
            else
            {
                MessageBox.Show("The clipboard does not contain any text");
            }


From Mouse
private void Form1_Load...
{
            MenuItem mi = new MenuItem("Cut");
            mi.Click += new EventHandler(mi_Cut);
            cm.MenuItems.Add(mi);

            mi = new MenuItem("Copy");
            mi.Click += new EventHandler(mi_Copy);
            cm.MenuItems.Add(mi);

            mi = new MenuItem("Paste");
            mi.Click += new EventHandler(mi_Paste);
            cm.MenuItems.Add(mi);
           
            textBox1.ContextMenu = cm;
}

      void mi_Cut(object sender, EventArgs e)
        {
            rchNotepad.Cut();
        }
        void mi_Copy(object sender, EventArgs e)        
        {
            Graphics objGraphics;
            Clipboard.SetData(DataFormats.Rtf, rchNotepad.SelectedRtf);
            Clipboard.Clear();
        }

        void mi_Paste(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText(TextDataFormat.Rtf))
            {
                rchNotepad.SelectedRtf = Clipboard.GetData(DataFormats.Rtf).ToString();
            }
        }


0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 18052428
Oh and to close the current form use:

this.Close();

But if you want to close that form and open another one, use this code:
            Form1 formShow = new Form1();  // this will open form1
            formShow.Show();
            this.Close();   // will close the current form, in this example the currently open form is Form2
0
 

Author Comment

by:coolrazor
ID: 18058280
Ok, I used:
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
  {
    Clipboard.SetDataObject(ActiveControl.Text, true);
    ActiveControl.Text = "";
  }
  private void copyToolStripMenuItem_Click(object sender, EventArgs e)
  {
    Clipboard.SetDataObject(ActiveControl.Text, true);
  }
  private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
  {
    ActiveControl.Text = (string)Clipboard.GetDataObject().GetData(typeof(string));
  }
This is the error I get when I run the application and try to cut or copy:
"ExternalException was unhandled"
"Requested Clipboard operation did not succeed"

The problem with angus_young_acdc's solution is that it only works for a specific text box.  I have over seven text boxes on my form.  I'd like to be able to copy to and from any of them (also to and from other applications).  Also, cut, copy and paste work if you use the keyboard or mouse, just not the menu items for cut, copy and paste.  I want the program to have those menu items, partly so that I can learn more about how to use them.
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 18211859
Do you have the STAThread attribute on your Main entry point for the application?
0
 
LVL 11

Accepted Solution

by:
Expert1701 earned 400 total points
ID: 18211893
coolrazor,

  I have tried the code you supplied in your last post, and it worked perfectly.  I understand that when you copy and paste from the keyboard it works, but I would suggest that there is some machine specific issue here.  After you check for the STAThread attribute, try running on another machine (and not in Virtual PC, or a Terminal Services session).  If you are looking a cut/copy/paste that works like the keyboard, you can also try this:

  private void cutToolStripMenuItem_Click(object sender, EventArgs e)
  {
    if (ActiveControl is TextBox)
      ((TextBox)ActiveControl).Cut();
  }

  private void copyToolStripMenuItem_Click(object sender, EventArgs e)
  {
    if (ActiveControl is TextBox)
      ((TextBox)ActiveControl).Copy();
  }

  private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
  {
    if (ActiveControl is TextBox)
      ((TextBox)ActiveControl).Paste();
  }

or worst case:

  private void cutToolStripMenuItem_Click(object sender, EventArgs e)
  {
    SendKeys.Send("^X");
  }

  private void copyToolStripMenuItem_Click(object sender, EventArgs e)
  {
    SendKeys.Send("^C");
  }

  private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
  {
    SendKeys.Send("^V");
  }

TheLearnedOne,

  If there are no more replies here, my suggestion is that Collindsouza's first response be accepted as the answer, as it is a correct solution to the original question, "when I run [the program] and click cut or copy FOR SECOND TIME, the program locks up".
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

20 Experts available now in Live!

Get 1:1 Help Now