Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
(VB Winforms)
I want to know if there is a way that one can reuse user controls.

I have several user controls I want to use throughout my application but I have run into a snag where:
(1) If I declare the user control in the declaration section and then add it to a Panel control, the moment I clear all controls from the Panel I am not able to reuse the user control,

(2) If I
VB
Dim ucUserControl As New _ucUserControl
within a Procedure or Function I am unable to pass information to it from another Form for instance. what I have found was that the information I passed from a Form to a TextBox in the User Control exists but it is not showing in the text of the TextBox.

I know it has something to do with Global and Local scope but I find it difficult to find a meaningful and sustainable solution to be able to reuse User Controls effectively.

Guidance will be appreciated.

Thanks
T

What I have tried:

Tried both Global and Local (Procedures and Functions) declarations and only the Global declaration works but once the User Control is cleared you need to redeclare it and as soon as that happens it loses Global scope and information does not show.
Posted
Comments
Ralf Meier 11-Feb-16 0:36am    
You should not improve your question with solution when this Solutions are only additional Information. For this we have the function "Improve Question" and this you should use.

To your Problem :
I'm very familiar with creating (customized) controls. But all your Information you have given don't help me to give you an answer.

Perhaps something generell :
If I create a Usercontrol all the included controls are PRIVATE / not accessible from the outside. If I want to Access an include Control I allways add Properties to the Host-Control which do the access (data-transfer) to the included Controls. You can do this as well without those Properties but I think, the decribed way is the better way.
Tino Fourie 11-Feb-16 3:40am    
Ralf, thanks for your reply.
I found out later that I was going about it all the wrong way.... I just had too many hours invested into my work yesterday that I did not see the obvious.

When declaring a User Control Globally, there is no need to declare it again in a Function / Procedure, you can ".clear" it from any parent control as long as you do not "destroy" it in the process.

I am with you on the Getter and Setters.

Thanks for your reply.

(As for not "Improving" my question, it was getting to a point where I had no idea of what was happening. Losing functionality as I did, things just came apart at a rapid pace and I had no idea of why it was happening.)

Seems I may have found the err in my ways - and that does not make sense to me.

I had
VB
Me.ucUserControl.Textbox1.text
and as soon as I remove the "Me." it worked !!

I have no idea why but I am sure there is some theory behind the madness in this one.

Just to give better feedback now that I can think straight again.

One can effective reuse UserControls after it has been cleared before e.g.:

Instead of the Global declaration (which I normally would do) I can now declare it almost anywhere in my code:
VB
Private Sub GetData()
   Dim ucMyControl As New _ucMyControl
   With ucMyControl
      .Dock = DockStyle.Fill
      .Visible = True
  End With

   'Add to Main Panel
   pnlMain.Controls.Clear
   pnlMain.Controls.Add(ucMyControl)

   'Populate
   ucMyControl.TextBox1.Text = ExternalForm.TextBox1.Text
   'etc.....

End Sub


You can now reuse that user control anywhere else in your program after you have cleared it from the parent control.

Just remember, never to use "Me." when referring to the User Control.

Regards,
T

EDIT:
New snag!!!! Nothing makes sense at all!!

If I use
VB
Me.ucMyControl.TextBox1.Text = ExternalForm.TextBox1.Text
then the Text Property of MyControl.TextBox has a value but it is not displayed.

If I use
VB
ucMyControl.TextBox1.Text = ExternalForm.TextBox1.Text
then the Text Property of MyControl.TextBox has NO value however it displays the correct information.

Even if I assign a value directly e.g:
VB
ucMyControl.TextBox1.Text = "12345"
the value of "12345" is displayed but the value is not stored in the Text property of the control.

Any idea why this is happening ?
 
Share this answer
 
v6
Continued from Solution 1: (Solution 1 getting too lengthy)

I have established that:
VB
Me.ucMyControl.TextBox1.Text = "12345"

will store the value of "12345" in its Text property BUT not display it!!

I have also established that:
VB
ucMyControl.TextBox1.Text = "12345"

will display the value "12345" BUT not store it in the Text property!!

I can now confirm that:
VB
Me.ucMyCotrol.TextBox1.Text = "12345"
ucMyControl.TextBox1.Text = Me.ucMyControl.TextBox1.Text

will both store the value "12345" in its Text property as well as display the value "12345".

Anyone with an answer to this anomaly? (Is it something I am doing wrong and should rather go to bed....and sleep over it)

Regards,
T
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900