?
Solved

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

Posted on 2006-11-30
14
Medium Priority
?
8,352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

777 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