Solved

User Controls

Posted on 2012-03-15
6
150 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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