Results 1 to 15 of 15

Thread: [RESOLVED] Copy controls from one tab to another (with code)

  1. #1

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Resolved [RESOLVED] Copy controls from one tab to another (with code)

    I have been searching and having no luck...So anyway, what I need is a way to copy tab1's controls or items (like textboxes) and then have them pasted into a new tab.

    This would be done through a "New Tab" button. When that button is pressed, a new tab will open up and it should have all the same items that tab1 has.

    thanks for any help...can't think of anything else to add

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Copy controls from one tab to another (with code)

    Well you could loop through the source tab page's controls collection and for each item determine what type it is and create a new control of the same type, set its properties and add the new control to the destination tab.

    Handling events associated with the controls may be more awkward though, I'm not sure how you would find the routine that handles (for example) a button's click event and then attach that to the new buttons on the new tab page.

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Copy controls from one tab to another (with code)

    Hey,

    I think a better approach would be to define a base tab page that has everything that you want on it, and then at design time create an instance of this tab page and add it to the TabControls TabPages Collection.

    Gary

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Copy controls from one tab to another (with code)

    Quote Originally Posted by gep13 View Post
    Hey,

    I think a better approach would be to define a base tab page that has everything that you want on it, and then at design time create an instance of this tab page and add it to the TabControls TabPages Collection.

    Gary
    That raises a question in my mind - I had assumed that the intention was to copy the values as well as the controls, but I may be wrong.

    If you don't then GEP's solution is a neat one.

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    That's a good point, I hadn't considered that!!

    I would have thought that the OP was wanting it so that a new instance of a TabPage was created ready to accept input, hence all the textboxes would simply be empty.

    It does raise a question though, which only the OP can answer.

    Gary

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Copy controls from one tab to another (with code)

    Quote Originally Posted by gep13 View Post
    Hey,

    I think a better approach would be to define a base tab page that has everything that you want on it, and then at design time create an instance of this tab page and add it to the TabControls TabPages Collection.

    Gary
    Could you give an example of how to do this? I have tried this in the past but did not get it to work; I had to resort to creating new TabPages and creating new controls on them manually.

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    Hey,

    One of the best examples that I have seen for this was here:

    http://www.vbforums.com/showthread.php?t=506103

    Here you can see jmcilhinney extending the functionality of a TabPage to include properties, methods and controls.

    Hope that helps!!

    Gary

  8. #8
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    In that example (note his very first post for example) he still creates a new, completely blank tabpage, and adds a browser control to that Tabpage's Control collection in its constructor:
    vb.net Code:
    1. Public Sub New()
    2.    Me._browser.Dock = DockStyle.Fill
    3.    Me.Controls.Add(Me._browser)
    4. End Sub

    That's not the same as having a TabPage created at design-time, complete with controls and their layout and properties, and then creating a new instance of that 'template' tabpage.

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    Hey,

    You are correct, it's not the same, but it is a perfectly valid approach, and there are a number of ways that you can extend what jmcilhinney has shown you in that example.

    For instance, create a Composite User Control, which will give you the full design time experience, and then add an instance of this user control to the custom Tab Page in the same way that the WebBrowser control is added.

    I was merely trying to give you a starting point which could be extended on.

    Gary

  10. #10
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    Quote Originally Posted by gep13 View Post
    Hey,

    You are correct, it's not the same, but it is a perfectly valid approach, and there are a number of ways that you can extend what jmcilhinney has shown you in that example.

    For instance, create a Composite User Control, which will give you the full design time experience, and then add an instance of this user control to the custom Tab Page in the same way that the WebBrowser control is added.

    I was merely trying to give you a starting point which could be extended on.

    Gary
    That was exactly the approach I took in my old thread I posted above.

    I was then however looking for a different approach where an entire tabpage is 'copied' to a new tabpage, complete with controls and their properties. I finally came to the conclusion that it was not possible except by looping through all controls, somehow finding their type and then creating new controls based on their type and properties (something I have not managed to do yet except with a Select Case statement where each possible type is used, not the best way I think...).

    I saw you talking about defining a base tabpage, of which you then add a new instance to your TabControl. This was exactly what I was looking for, but it seems that you didn't mean what I thought you did.

    Thanks though!

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    Hey,

    I think we are talking about two separate things here.

    As in jmcilhinney's thread, you can create a base tabpage and on the new event, add a new instance of the user control that contains all the textboxes etc that you want.

    You can then add this instance of the tabpage to the tabcontrol, and do this over and over again, each time creating a new instance of the tabpage and the usercontrol.

    Given that these new instances are separate from all others, they are not going to share the same properties, each Text Property as I suspect you are referring to. To do this, you would, as you have suggested, need to loop through each control and set the properties to match those as the source tab.

    Can you give an example of when you would need to do this though? Forgive me if this was mentioned in your other thread, as I have not read all of it.

    Gary

  12. #12
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    I wasn't really talking about setting the properties only. What I meant was that you create a TabPage during design-time, add some controls, set their properties etc, as usual.

    Then during run-time, you hide this tab. When the user asks for it (eg, presses a certain button), you create a new instance of the hidden 'template' tab, along with all its controls and their properties, and add that to the TabControl.

    This way you can easily add as many TabPages as you want to a TabControl without having to do complicate loops that add the correct controls.

    But, as I found out earlier, creating a UserControl and adding a single instance of that usercontrol to a new TabPage is a much better solution (as long as you allow the TabPage to use visual styles for its backcolor, which my old thread was actually about).

    I don't need this anymore, I was just interested in it, because I spent quite some time trying to figure out how to do it (before I thought of using a UserControl), and you made it sound like it was very easy to do


    So, before we completely hi-jack this thread, I think the best answer to the OP's question is to create a UserControl that hosts all the controls you want. You can expose some of their properties and events via the UserControl if you need them. You can then add a new instance of this UserControl to a new TabPage, which you add to the TabControl.

    This way you are not really copying all the controls from a previous tabpage, but you have stored the controls during design-time (in your usercontrol). This of course only works assuming you know beforehand which controls will be on your TabPage. If not, there must be some way to let the user add or remove controls, which seems unlikely.

  13. #13
    Addicted Member
    Join Date
    Apr 2009
    Location
    Near Dog River (sorta)
    Posts
    160

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    Thanks for the thread. When my app is complete, I intend to add a feature like the one in the OP, as part of version 2.0.

    I bookmarked this and referenced threads.

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    Hey,

    @NickThissen, I think you have hit the nail on the head, and this is definitely the approach that I would recommend.

    If the user does want to take over the creation of the controls, with the loss of design time support, he/she can take control of adding the controls at run time in the same way as jmcilhinney did in his thread. This will be slightly more involved though, but it is still an option.

    Gary

  15. #15
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: [RESOLVED] Copy controls from one tab to another (with code)

    As the OP marked the thread as resolved some time ago (and didn't comment on the posts offered) I can only assume that he's now got a working solution!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width