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

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.)
coolrazorAsked:
Who is Participating?
 
Expert1701Connect With a Mentor Commented:
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
 
CollindsouzaCommented:
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
 
CollindsouzaCommented:
sorry my bad..

 ' Make the copied text available after the application ends.
        Clipboard.SetDataObject(this.ActiveControl.Text, True)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
coolrazorAuthor Commented:
     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
 
coolrazorAuthor Commented:
Also, what's the syntax for closing the current form?

buttonClick
{
     activeform.close;
}

-?  (just a bonus question)
0
 
coolrazorAuthor Commented:
oops, forgot to add bonus points.
0
 
Expert1701Commented:
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
 
angus_young_acdcCommented:
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
 
angus_young_acdcCommented:
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
 
coolrazorAuthor Commented:
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
 
Expert1701Commented:
Do you have the STAThread attribute on your Main entry point for the application?
0
All Courses

From novice to tech pro — start learning today.