![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: controls, extending |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| Many times we want to stylize our form elements but modifying the properties of each form element can only take us so far. In many cases, their is also a need to make a control act in a certain way, but should only be changed once and reflected application-wide. I'll discuss a few ways you can make your OWN controls or extend the current controls for the features and functionality you actually deserve ![]() User controls can be created with whatever elements you like. They can be added by right-clicking the project, and adding a new user control. At this point you are a blank slate, and you can add whatever elements (including other user controls) to your user control. You can even add your own properties which are then viewed and executed real time when you add the to a form. It's pretty cool when you "compile" and you see your own custom control in the toolbar ![]() For example, Create a user control, add 5 textboxes and add this to the code of the User Control. Code: Public Class UserControl1
Private _TextboxBackgroundColor As Color
Public Property TextBoxBackgroundColor() As Color
Get
Return _TextboxBackgroundColor
End Get
Set(ByVal value As Color)
_TextboxBackgroundColor = value
TextBox1.BackColor = value
TextBox2.BackColor = value
TextBox3.BackColor = value
TextBox4.BackColor = value
TextBox5.BackColor = value
End Set
End Property
End Class The next lessons to come will discuss the ability to override controls using their events and how to inherit the already-built controls to make shnazzy looking form controls ![]() __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| |
| |||
| Yes, in VB, getters/setters and called Properties. The Private variable holds the data for color value, the Get returns the private variable, and the Set actually sets the the private variable. We also hook into setting other control properties in our set... so when you set the Property "TextBoxBackgroundColor" to Color.Red for example, it will set all 5 controls and the internal variable so you could retrieve it again later. This way it is realtime in the VisualStudio GUI and you can use the properties in the properties window to see your changes instantly ![]() _TextBackgroundColor is my own variable... showing you can extend these user controls to anything you want... __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| |||
| This is the hook if you ever wanted to retrieve the value that you've set. Say you wanted all form elements to use the same background color that you set your user control. You could set the color of your user control to red, enumerate over all other controls on the form, and "Set" their background color from the "Get" of your User control. In all reality, if you wanted to limit it to "Set Only" you could change it the property to WriteOnly but then your loosing the ability to get the value realtime. Code: Public Class UserControl1
Public WriteOnly Property TextBoxBackgroundColor() As Color
Set(ByVal value As Color)
TextBox1.BackColor = value
TextBox2.BackColor = value
TextBox3.BackColor = value
TextBox4.BackColor = value
TextBox5.BackColor = value
End Set
End Property
End Class __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes Last edited by ccoonen : 07-03-2007 at 11:17 AM. |
![]() |
| Thread Tools | |
| Display Modes | |
| |