Windows Develop Bookmark and Share   
 index > Windows Forms General > Problem with copy and paste button using c#?
 

Problem with copy and paste button using c#?

Hai all
i'm succesfully to create copy and paste button. but i have the next case. i have two context menu. the first context menu in the button that i use to copy paste. in the first context menu when right click it show copy and paste item. in the second contextmenu when i right click it show paste item. the problem is when i use paste item in the second context menu. the button can only paste for one time. so i want the paste item in the second contextmenu to be paste more than one time. how to do that? please answer my question....!!
muhamad nizar iqbal  Wednesday, September 23, 2009 4:47 AM
Hi,
As he said we can not copy the control.but runtime we can create control like.
 Button btn = new Button();
            btn.Name = button1.Name+"Clone";
            btn.Width = button1.Width;
            btn.Text = button1.Text;
            btn.Location = new Point(button1.Location.X + 100, button1.Location.Y);
            this.Controls.Add(btn);

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 23, 2009 5:06 AM
Hi,
i hope it will help you
 Button btn;
         copyToolStripMenuItem_Click(object sender, EventArgs e)
         {
              btn = new Button();
            btn.Name = button1.Name+"Clone";
            btn.Width = button1.Width;
            btn.Text = button1.Text;
            btn.Location = new Point(button1.Location.X + 100, button1.Location.Y);

         }
        pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
             this.Controls.Add(btn);

        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 23, 2009 7:04 AM
I'm not sure about the context of your question.

Do you have a button on your form you want to copy at design time?

Do you have a button on your form you want to copy at runtime?

Do you want to copy the button text off of a button from another application?

Could you provide more information on what you are trying to accomplish?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, September 23, 2009 4:57 AM
Controls cannot be copied. You have to make a new Button() and assign its properties from the original. Putting the code that makes the first button in a method by itself and just calling the method again is a simple workaround.

Hans Passant.
  • Proposed As Answer byGnanadurai Wednesday, September 23, 2009 6:31 AM
  •  
nobugz  Wednesday, September 23, 2009 4:58 AM
Hi,
As he said we can not copy the control.but runtime we can create control like.
 Button btn = new Button();
            btn.Name = button1.Name+"Clone";
            btn.Width = button1.Width;
            btn.Text = button1.Text;
            btn.Location = new Point(button1.Location.X + 100, button1.Location.Y);
            this.Controls.Add(btn);

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 23, 2009 5:06 AM
i have a button in my form and i want to copy it at runtime?
muhamad nizar iqbal  Wednesday, September 23, 2009 5:46 AM
not copy button when design time but i want to copy button at runtime. how to do that?
muhamad nizar iqbal  Wednesday, September 23, 2009 5:48 AM
i have a button in my form and i want to copy it at runtime? not at design time?
muhamad nizar iqbal  Wednesday, September 23, 2009 5:49 AM
hello, i want to create code that can copy button. can anyone help me? i was search in google but can not find the answer. the sample just copy text from textbox. Please answer my question,maybeexisted sample can be referenced, thanks.
i have a button in my form and i want to copy it at runtime? not at design time?
muhamad nizar iqbal  Wednesday, September 23, 2009 5:52 AM
Hi,
Try out my code .it will create new button at run time same like existing button.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 23, 2009 5:55 AM
Do you want to copy the text in the textbox to clipboard on the click event of the button..
then do like this ..

Clipboard.SetDataObject(TextBox1.Text,

True)


:: Learning .NET ::
Prasant Swain  Wednesday, September 23, 2009 6:03 AM
sorry your code is not copy button but just create new button. i want to copy button code that can implement at runtime.
muhamad nizar iqbal  Wednesday, September 23, 2009 6:07 AM
sorry, i don't want to copy text in the textbox but, i want to copy button at runtime?
muhamad nizar iqbal  Wednesday, September 23, 2009 6:10 AM
Then you can follow the process suggested by Gnanadurai ..
:: Learning .NET ::
Prasant Swain  Wednesday, September 23, 2009 6:20 AM
Gnanadurai when i try your code in the context menu, when i click copy it create new button at run time same like existing button. but, i want the new button create when i press paste button. my idea is cut code in copyToolStripMenuItem_Click(object sender, EventArgs e) function and paste it into pasteToolStripMenuItem_Click(object sender, EventArgs e) function. what is the code in the copyToolStripMenuItem_Click(object sender, EventArgs e) function?.
muhamad nizar iqbal  Wednesday, September 23, 2009 6:43 AM
Hi,
i hope it will help you
 Button btn;
         copyToolStripMenuItem_Click(object sender, EventArgs e)
         {
              btn = new Button();
            btn.Name = button1.Name+"Clone";
            btn.Width = button1.Width;
            btn.Text = button1.Text;
            btn.Location = new Point(button1.Location.X + 100, button1.Location.Y);

         }
        pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
             this.Controls.Add(btn);

        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 23, 2009 7:04 AM
Gnanadurai i'm succesfully to create copy and paste button. but i have the next case. i have two context menu. the first context menu in the button that i use to copy paste. in the first context menu when right click it show copy and paste item. in the second contextmenu when i right click it show paste item. the problem is when i use paste item in the second context menu. the button can only paste for one time. so i want the paste item in the second contextmenu to be paste more than one time. how to do that? thanks before.
muhamad nizar iqbal  Wednesday, September 23, 2009 8:34 AM
Hi,
i hope it will help you.

  Button btn;
        int locX = 100;
        int locY = 0;
         copyToolStripMenuItem_Click(object sender, EventArgs e)
         {
             btn = new Button();
            btn.Name = button1.Name + "Clone";
            btn.Width = button1.Width;
            btn.Text = button1.Text;           
            btn.Location = new Point(button1.Location.X + locX, button1.Location.Y + locY);
         }
        pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
             this.Controls.Add(btn);
            if (this.Width <= locX)
            {
                locX = 0;
                locY = locY + 50;
            }
            else
            {
                locX = locX + 100;
            }

        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Thursday, September 24, 2009 4:35 AM
Hi,
you got the solution for your problem but you unmarked as a answer.
if you want more help let me know.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Thursday, September 24, 2009 7:21 AM
hai Gnanadurai ,

i want to make my button can be resizeable at runtime.how to make that? please give me a sample code or reference...!!
muhamad nizar iqbal  Thursday, September 24, 2009 10:05 AM

You can use google to search for other answers

Custom Search

More Threads

• Is it possible to embed a command prompt (cmd.exe) as a control on a Windows Form?
• Treeview AfterSelect and TextBox Focus
• split parent form
• all i want to do is draw a circle on the form
• Windows Explorer Default Tile Size
• MDI + external exe
• Several questions concerning DGV
• Test for Existance of Object
• Task Scheduler
• How can i draw graphics on a different form