|
-
Apr 26th, 2009, 08:38 AM
#1
Thread Starter
Member
[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
-
Apr 26th, 2009, 09:38 AM
#2
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.
-
Apr 26th, 2009, 10:59 AM
#3
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
-
Apr 26th, 2009, 11:32 AM
#4
Re: Copy controls from one tab to another (with code)
 Originally Posted by gep13
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.
-
Apr 27th, 2009, 01:13 AM
#5
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
-
Apr 27th, 2009, 10:38 AM
#6
Re: Copy controls from one tab to another (with code)
 Originally Posted by gep13
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.
-
Apr 27th, 2009, 10:47 AM
#7
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
-
Apr 27th, 2009, 11:48 AM
#8
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:
Public Sub New() Me._browser.Dock = DockStyle.Fill Me.Controls.Add(Me._browser) 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.
-
Apr 27th, 2009, 11:55 AM
#9
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
-
Apr 27th, 2009, 12:00 PM
#10
Re: [RESOLVED] Copy controls from one tab to another (with code)
 Originally Posted by gep13
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!
-
Apr 27th, 2009, 12:06 PM
#11
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
-
Apr 27th, 2009, 12:22 PM
#12
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.
-
Apr 27th, 2009, 12:37 PM
#13
Addicted Member
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.
-
Apr 27th, 2009, 01:56 PM
#14
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
-
Apr 27th, 2009, 02:04 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|