Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Database and Reporting > Populate listbox with Objects. OOP


Reply
 
Thread Tools Display Modes
  #1  
Old 04-24-2012, 07:13 AM
Worf Worf is offline
Newcomer
 
Join Date: Nov 2008
Posts: 14
Default Populate listbox with Objects. OOP


Hi.

Firstly, sorry if this is in the wrong forum and also for the amount of code posted. I thought by posting them you would be able to see and understand what it is i am trying to do.

I am writing a database using OOP, i have created my classes ect and i am able to input data.

Now to the problem.

I have four textboxs, and two listbox's. The first listbox lists all the categories which i have managed to do. Now when the user selects a catelgory a list of objects under that category are listed in the second listbox which the user then selects to display any related data and this is the problem i am having.

Pic example.

"http://img15.imageshack.us/img15/2100/viewscripts.jpg"

This is some of the code:

BaseClass:

Code:
Public Class BaseClass


    ' ScrName is the name of the script file.

    Public Property ScrName() As String

    ' Usage is what to do with the script.

    Public Property Usage() As String

    ' Desc is the description of what the script does.

    Public Property Desc() As String

    ' Script is the script file it's self.

    Public Property Script() As String

    ' Gets the Version of the Script. Either X9 or X10

    Public Property Xver() As String

    ' Holds weather script is compateble with each version. X9 Only, X10 Only or both X9 & X10

    Public Property Compat() As String

    ' Holds additional information of what the script.

    Public Property AddInfo() As String

    ' Hold the date script was added

    Public Property Dateadded() As String

    Public Property Scriptcount() As String

End Class
This is my Records class which Inherits the Baseclass:

Code:
Public Class Record

    Inherits BaseClass

    Public Property Catel As String

    ' Electronics Sub-Category Data

    Public Property Electronics As New Electronics

    ' Enemies Sub-Category Data

    Public Property Enemies As New Enemies

End Class
And this is one of the categories:

Code:
Public Class Electronics

    Inherits BaseClass

    Public Sub New()
        'Empty Constructor   
    End Sub

    Public Sub New(ByVal scrName As String, ByVal usage As String, ByVal desc As String, ByVal script As String, ByVal xver As String, _
                   ByVal compat As String, ByVal addInfo As String, ByVal dateadded As String, ByVal scriptcount As String)

        Me.Desc = desc
        Me.Usage = usage
        Me.ScrName = scrName
        Me.Script = script
        Me.Xver = xver
        Me.Compat = compat
        Me.AddInfo = addInfo
        Me.Dateadded = dateadded
        Me.Scriptcount = scriptcount

    End Sub

End Class
This code populates the first listbox with the categories.

Code:
Public Class ViewScripts

    Private Sub ViewScripts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Add list of categories to listbox

        For Each f As String In Catelog

            CatelogList.Items.Add(f)

        Next

    End Sub
The next two bits of code are what i am having trouble with.

This code allows the user to select a category which then populates the second listbox with the data stored under that category.

Code:
Private Sub CatelogList_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CatelogList.SelectedIndexChanged


        ScriptList.Items.Clear()

        ' Grab the category from the list 

        Dim CurrentRecord As Record = ListOfRecords.Item(CatelogList.SelectedIndex)

       
          ' Load Cat with the selected category.
        Cat = CatelogList.SelectedItem

        Dim CatSelected As String = Cat

        ' Add list of catelogs to listbox

        With CatSelected

          ' Now populate listbox two with any objects stored under category.

            For Each f As String In CurrentRecord.ScrName

                ScriptList.Items.Add(CurrentRecord.ScrName)

            Next
        End With

    End Sub
Now, when the second listbox is populated, the user can then select any of the objects in listbox 2 and display it's contents in the relevant textboxs.

Code:
   Private Sub ScriptList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ScriptList.SelectedIndexChanged


        Dim selectedRecord As New Record

        For Each checkRecord As Record In ListOfRecords

            If checkRecord.ScrName = CatelogList.SelectedItem.ToString Then
                
                ScriptUsage.Text = checkRecord.Usage
                
                ScriptDescription.Text = checkRecord.Desc
                
                AddInfo.Text = checkRecord.AddInfo
                
                ScriptView.Text = checkRecord.Script

            End If
        Next

    End Sub
As you already knows that this does not work.

I have been stuck on this for weeks and would be greatful to anyone who can help me with this.

Many Thanks

Worf.
Reply With Quote
  #2  
Old 04-24-2012, 07:54 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I'm a bit loathe to read through all the code to try and work out what's going on.

But you should be aware that you can chuck any object at a listbox, and then get that object out again. I think this should help you.

I'll put together an example. Basically a custom class with a couple of fields and I'll put them into the listbox and then get the details out again.

Here's the class I'm using. It's pretty basic with a couple of names and an age. There's a fullname property that puts the names together. The main thing to notice is the overriden ToString. That's what the ListBox will use as the text for the item.

Code:
Public Class AClass
    Public FirstName As String
    Public SecondName As String
    Public Age As Integer

    Sub New(ByVal firstName As String, ByVal secondName As String, ByVal age As Integer)
        Me.FirstName = firstName
        Me.SecondName = secondName
        Me.Age = age
    End Sub

    Public ReadOnly Property FullName() As String
        Get
            Return FirstName & " " & SecondName
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return FullName
    End Function
End Class
I've then got a listbox a form. When the form loads it creates 3 of these classes and puts them into the list box.

When an item in the listbox is selected it gets the class out of the listbox and shows that it even remembers the age that was assigned to the class.
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim c As New AClass("John", "Smith", 24)
        ListBox1.Items.Add(c)

        c = New AClass("Fred", "Boon", 30)
        ListBox1.Items.Add(c)

        c = New AClass("Jack", "Spencer", 45)
        ListBox1.Items.Add(c)
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim c As AClass

        c = CType(ListBox1.Items(ListBox1.SelectedIndex), AClass)

        MessageBox.Show(c.FullName & ", " & c.Age)
    End Sub
So for example, you could easily store an ID of an object and get the ID back for querying something else when it is selected whilst displaying something totally different. You don't need to keep going back to a datasource to find out other information of what was selected. Overriding ToString is the key to the displayed text in the list for the class.

If that's no help at all, then sorry.
__________________
There are no computers in heaven!

Last edited by DrPunk; 04-24-2012 at 08:01 AM.
Reply With Quote
  #3  
Old 04-24-2012, 08:06 AM
Worf Worf is offline
Newcomer
 
Join Date: Nov 2008
Posts: 14
Default

Hi DrPunk.

Thank you for your reply. Forgot to mention that i am using

'Public ListOfRecords As New List(Of Record)'

and i need to get the information from this as i need to populate the listbox with the object 'ScrName'.

Regards

Worf.
Reply With Quote
  #4  
Old 04-24-2012, 08:17 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

If I understand that right then you want to make your Record class override ToString and return scrName.

When you populate your listbox you can loop over For Each Record in your ListOfRecords and put the Record object directly into your listbox. The overridden ToString will display the scrName of the Record in the listbox.

Because your Record classes will exist in the ListBox then you might not need ListOfRecords, depending on what else you do with ListOfRecords, because the ListBox is acting as the ListOfRecords (if you see what I mean. I'm assuming you're using ListOfRecords to refer back to when a scrName is selected. The fact that you can get the Record class out of the ListBox means you don't need to find the scrName reference in the ListOfRecords).

If it helps, then imagine the Age property of my example being the ID of that object in a database and when the item is selected in a list it needs to query a table based on that ID. I could have a collection of AClass and just put the fullname into the listbox, then when one is selected refer back to my collection to find the ID. Putting AClass directly into the listbox means that you don't need to go back to the collection to find the ID of that fullname.
__________________
There are no computers in heaven!

Last edited by DrPunk; 04-24-2012 at 08:25 AM.
Reply With Quote
  #5  
Old 04-24-2012, 09:17 AM
Worf Worf is offline
Newcomer
 
Join Date: Nov 2008
Posts: 14
Default

I think i know what you mean, i will try something out and will let you know how i got on.

Many Thanks for you help.

Worf
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->