Solved

User Controls

Posted on 2012-03-15
6
149 Views
Last Modified: 2012-03-15
So, I decided to add my 2nd ever user control to a project I have been working on in the last few days...

On my main form, I have serveral buttons that are added to the form dynamicly during runtime.   When one of these buttons is pressed, I would normaly have opened another form, and then would have "Access" to all of the fields on the main form.  

However, if I just show a user control, I have not been able to figure out how I could access those other controls from the UC.  There is a lot of code I would need to do on this.. Look up some values in a database,  based upon certain values found on the main form.   Add fields and controls at runtime, based upon those values found in the database.  etc.

Is it possible?  or am I better off just using a seperate form.
What are some of the pro's and con's of using a User Control vs a seperate "Form".
Re-useability is one I can see.
0
Comment
Question by:ScottParker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37724838
Re-Usability is a huge one.  Code seperation is also a great thing . . . at least for me it also keeps my head straight about what each is responsible for.

You don't actually show a user control . . . a user control gets added to a form which is then shown.

Essentially what you can do is create whatever properties the control needs and then populate them from the main form prior to showing.


If you look at it from a bottom up approach, just think of the user control as it's own isolated object.  I'm responsible for doing X,Y, & Z.  For that I need these variables to know.

Talking back to the main form is easy.  What you do is create events that are raised later on with whatever parameters you think you need to send.


For instance:

Public Event Added(ByVal ID as integer, ByVal Name as string)


Somewhere in your code:

RaiseEvent Added(theNewId, theNewName)


then you can subscribe to that event in your main form the same way you would any other event on any other control.
EG:

Private Sub ucAdded(ByVal NewId as Integer, ByVal NewName as String) Handles MyUserControl.Added

' DO SOMETHING

end sub




If it's a one off, you're probably okay just doing it in the form, but if you're going to be reusing the code in a different part of your application, you should probably make it a user control.
0
 
LVL 3

Author Comment

by:ScottParker
ID: 37724920
if I have on the form (frmMain) a textbox called  txtThisTextbox
If I put a UserControl (ucThisUserControl) on frmMain then  how do i reference the value txtThisTextBox.Text from within the User Control.

From a seperate form I would do something like....  frmMain.txtThisTextBox.Text
but from within the user control I cant seem to figure it out.
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 37724957
you don't


In userControl:


Private _TextBoxText as String

Public Property TextBoxText as string

get
 return _textBoxText
end get

set (value as string)
  _TextBoxText = value
   'Call a function if you need to do something once this is set.  Work with it internally.
end set

End Property
 


In frmMain:

dim uc as new ucThisUserControl

uc.TextBoxText = txtThisTextBoxText


I guess if you really wanted the Whole form you could set the form itself.

Public frm as MyParentForm

sub New(ByVal Parentfrm as ParentForm)
     frm = Parentfrm
end sub


In main:
new ucThisUserControl(me)


then you should be able to call frm.TextBox1
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:ScottParker
ID: 37725085
Ok so using the "Properties" method I would set up all of the properties I need.

In my main form I can put the ucThisUserControl on the main form.. and put the visable property to false.
Then in the button click function, first set all of the properties, then make the User Control visiable.  
That should trigger the user controls "Visible Changed" event, in which I can then put code to do the lookups in the database, and create the fields on the control itself.

That sound about right?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37725190
Just be careful using an event like that only because it could be handled elsewhere and have side effects.

The better way is to create A Sub though that does the same thing:

Public Sub GetDataFromDB()

end sub


and then just call that (still setting the visibilty to true once set).

You may want to refreshData eventually and that would help rather than trying to toggle visible changed.
0
 
LVL 3

Author Closing Comment

by:ScottParker
ID: 37725740
Thanks for the info.   I think it will work well now.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question