Solved

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

Posted on 2006-11-30
14
8,277 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

12 Experts available now in Live!

Get 1:1 Help Now