Moving Controls from One Tab Page to Another

10,017

Solution 1

I just tested it. What is happening when you cut and paste your controls, you losing the wiring of the events. What you need to do after cut and paste is to go to control properties-events, find the event in question and on the right, select a method that you want to handle that event.

Solution 2

Use the Document Outline.

View... Other Windows... Document Outline.

Select the required component and drag it from one tab page to the other in the tree control. I did this and the actions are preserved in the new tab page.

Solution 3

Drag the item out of the tab control and onto the form itself. Change to the other tab. Then drag the item into that tab. It is essentially 2 drag moves, but since you do not ever cut, all code linking is maintained. If your tab control takes up the entire form, simply make it smaller while you do the preceding steps and then make it large again when you are done.

Solution 4

When you "cut" the controls, you sever the connections between the controls and their respective events. When you "paste" them again, they're not hooked up to the events anymore, so they don't appear to do anything.

The "event" methods should still be present in your code, but you'll have to manually go through and subscribe each event to each control again (via the Properties window).

Alternatively, revert those changes, then open the .Designer.cs file and look for something like this:

this.tabPage1.Controls.Add(this.dataGridView1);

Which (for example) places dataGridView1 inside tabPage1.

If you wanted to move the DataGridView to another TabPage, you could just change this.tabPage1 in the above code to this.tabPage2.

this.tabPage2.Controls.Add(this.dataGridView1);

Then you can flip back over to the designer view and move the control around to wherever you want it within the TabPage.

Solution 5

This will cut them from the first TabPage and paste them on the second, i think you can do this as often as you want. And with a small change you can make it a truly copy. hope it helps

    private void ControlsToTabPage(TabPage from, TabPage to)
    {
        Control[] ctrlArray = new Control[from.Controls.Count];
        from.Controls.CopyTo(ctrlArray, 0);
        to.Controls.AddRange(ctrlArray);
    }
Share:
10,017
DeeWBee
Author by

DeeWBee

Updated on July 28, 2022

Comments

  • DeeWBee
    DeeWBee almost 2 years

    I currently am working on a WinForm project in which there are several different tabs. Within each tab there are various controls such as buttons, sub-tabs, text-boxes, ect...

    I need to consolidate the overall application which involves taking certain controls from one tab and moving them to another. When I first tried doing so, I simply copy and pasted the controls. As you can imagine this didn't work due to the fact that I didn't move the properties with the controls, I really just created NEW ones on a different tab. Therefore when I complied the code, nothing worked because there was no code assigned to the new controls.

    When I tried it again, this time I CUT and paste which also maintains the same properties as the old controls (specifically the reference name in the code), so as far as I can tell, the code should identify the controls by name, and apply the same actions. However, when I compile the code, the application successfully builds but the controls do not perform any actions.

    At this point I am not sure what to do...

  • DeeWBee
    DeeWBee almost 10 years
    Thank you so much! This had been bugging me forever!
  • DeeWBee
    DeeWBee almost 10 years
    Thanks so much for the suggestion! Really appreciate the help. Would I have to type in this bit of code for every single control involved? (Because there are a lot). Or does it just take all the controls from one tab and move them to another.
  • DeeWBee
    DeeWBee almost 10 years
    Thanks so much for the suggestion. You are right. I do want to move them around at control time. As far as I can tell though, when I move the controls from one tab to another, the auto-generated code already edits it self.
  • DeeWBee
    DeeWBee almost 10 years
    Grant is right about what I want to do. T.S. seems to have solved it though. Thanks!
  • DanG
    DanG over 7 years
    This works great. I also used it when i wanted to create tabs after I had a lot of controls on my form, and i wanted to put some of the controls on one tab and some of the controls on another tab. I just selected the tab then dragged to controls from the form onto that tab. @DeeWBee you might consider selecting this answer as the preferred method as it was added much later.
  • Webmasterjeff
    Webmasterjeff almost 7 years
    @DanG Thanks. I have done that too. Another tip is that if I am moving a bunch of controls between tabs at once, I will create a group in the tab, drag the items into the group, drag the group to the form, then drag the group to the new tab, and finally drag controls out of the group. The group container makes it so I can do them all together without creating a mess of it.
  • lanartri
    lanartri over 3 years
    right, but it's a PITA anyway. And there's a risk forgetting some.
  • David P
    David P about 3 years
    Awesome! Never used that window before, Very handy!