Solved

User Controls

Posted on 2012-03-15
6
146 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
  • 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

810 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