Solved

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

Posted on 2006-11-30
14
8,309 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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