Solved

User Controls

Posted on 2012-03-15
6
144 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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:ScottParker
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
Thanks for the info.   I think it will work well now.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now