Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Framework 3.5  »  Objects and the ListBox  »  Creating a UI
 »  Home  »  .NET Newbie  »  Objects and the ListBox  »  Creating a UI
 »  Home  »  Windows Development  »  Visual Basic 2005  »  Objects and the ListBox  »  Creating a UI
Objects and the ListBox
by Larry Blake | Published  06/05/2009 | Framework 3.5 .NET Newbie Visual Basic 2005 | Rating:
Larry Blake
Larry has been a professional developer since the 1980s, on a broad range of platforms. An early adopter of PC technology, he started fooling around with Visual Basic 1.0 in 1992. Today he is Microsoft MVP in Visual Basic. Larry is available for contract coding in VB, C# and ASP, and for Project Management. See: . 

View all articles by Larry Blake...
Creating a UI


Okay, now let's use our Custom Object in a Windows form. Go back to Form1 in design mode and add two buttons and a ListBox. Name the buttons "btnInfo" and "btnRaise". The form should look like this:

Now change the Form1_Load event as follows:

Private Sub Form1_Load(ByVal sender ...
   Dim p As Person

   With Me.ListBox1

      p = New Person("Joe", "Smith")

      p = New Person("Mary", ""Dellacorte", ""Manager")

      p = New Person(Hubert", "King", "President", 100000)

      p = New Person("Ronald", "King", "Boss' Nephew", 80000)

      .SelectedIndex = 0
   End With
End Sub

This new version creates Person objects and loads them into a ListBox on the form. The person variable p is re-used. Notice the use of different Constructors.

If you press "Play", you'll see that adding an Object to a ListBox doesn't give you the same result as adding a String would. The ListBox contains four copies of something like "MyProjectName.Person".  So let's add one more bit of code to Person.

Public Overrides Function ToString() As String
   Return FirstName & " " & LastName
End Function

The ListBox can't directly display an Object, so it tries to convert the Object to a String before displaying it. All Objects have a ToString() function, and the "MyProjectName.Person" came from the default implementation. By using Overrides, we give it a new behavior.

Overriding behavior is part of another OOP concept, Inheritance. Our Person object inherits behavior from the "parent" class Object.  Person could be the "parent" to new "child" objects as well.  Children can use the logic of their parent objects (and grandparent objects, etc.), "as is" or with overrides.

When we press "Play", the ListBox now contains the correct Person names.


Almost Finished

The interesting thing about ListBoxes is that although they display Strings, they contain Objects. You can see the difference when we add the final code to implement the buttons. From design mode in Form1, double-click the buttons to enter their code.  Here is how it should end up:

Private Sub btnInfo_Click(ByVal sender ...
   Dim p As Person

   p = Me.ListBox1.SelectedItem
End Sub

Private Sub btnRaise_Click(ByVal sender ...
   Dim p As Person

   p = Me.ListBox1.SelectedItem
End Sub

Now move the selection bar in the ListBox up and down, and press the "Info" button. You'll get something like this:

Clearly, the ListBox is keeping track of more than just Strings. But that's not all. These are fully functioning objects. Press the "Raise" button, then press "Info" again.  You'll see that the salary has been raised.  You can add multiple raises or select a different Person, and the ListBox will continue to maintain each Person separately.

This feature makes the ListBox good as a temporary data store.  Another common use is to display a name while remembering an associated number (course number, employee number, product number, etc.).

In this example, the button code takes the selected line in the ListBox and moves it into a usable Person variable. You can easily replace Person with an other object type to suit your purposes.



We have looked at a number of topics quickly.  To recap:

  • Creating Custom Objects is your first step towards becoming an OO programmer.
  • Abstraction and Encapsulation encourage smaller, more reusable code.
  • Public properties (and methods, etc.) implement Abstraction.
  • You can create Instances of Custom Objects just as you can for Toolbox Objects.
  • IntelliSense works on Custom Objects.
  • Use the Constructor method New() to execute any initialization code.
  • Overloading is using the same method name with a different Signature.
  • Use Overrides to change default behavior on Inherited objects.
  • The ListBox contains Objects, but displays Strings. 

I hope this was helpful.


How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.39473684210526 out of 5
 38 people have rated this page
Article Score16920
Sponsored Links