Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

User Controls

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
ScottParker
Asked:
ScottParker
  • 3
  • 3
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
 
ScottParkerAuthor Commented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ScottParkerAuthor Commented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
 
ScottParkerAuthor Commented:
Thanks for the info.   I think it will work well now.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now