Hello all - I am trying to use a ListBox

Currymunch

Member
Joined
Jan 21, 2008
Messages
13
Programming Experience
Beginner
Beginner Help

Hello all - I am trying to use a ListBox in order to display information about a class that i have created, when I click the Add spy button - a form appears that allows values to be entered, this information corresponds to the properties of the class. I would then like to add the item to the ListBox

'****************************************
Private Sub cmdConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConfirm.Click

If Me.Text = "Add Spy" Then
Dim thisSpy As New CSpy
thisSpy = New CSpy


thisSpy.FirstName = txtFirstName.Text
thisSpy.Surname = txtSurname.Text
thisSpy.CodeName = txtCodeName.Text
thisSpy.Age = comboAge.Text
thisSpy.Sex = comboSex.Text
thisSpy.DMarks = txtDmarks.Text
thisSpy.Skills = txtSkills.Text
thisSpy.BankNo = txtBankNumber.Text

mainSpy1.lblCurrentSpy.Text = thisSpy.FirstName + thisSpy.Surname + _
thisSpy.CodeName + CStr(thisSpy.Age) + thisSpy.Sex + thisSpy.DMarks + _
thisSpy.Skills + CStr(thisSpy.BankNo)

mainSpy1.spyList.Items.Add(thisSpy)



Else


End If


End Sub

'*****************************************************

I'm just setting it up so im not worried about the formatting at the moment I just want to add it to the ArrayList

This is what I get with the code thus far



I am looking to get the labels values into the ListBox. Any pointers would be greatly apprieciated.

Thanks in advance.
 
Last edited by a moderator:
Mmm, i'm not 100% sure what you're after but I have an idea.

What's showing in the listbox is the default ToString function of your class. This will be the full type name.

You need to overrides the ToString function in CSpy. Just add something like this. Except change it so it returns FirstName + Lastname etc. Also, you might want to use a listview for this instead, and have a column for each property.

PublicOverridesFunction ToString() AsString
Return"This Will Show in the Listview"
EndFunction
 
If you just add an object to a ListBox then what gets displayed is the result of the object's ToString method. By default that will be the name of the object's type, which is what you're seeing. If you want something else then you will need to override the ToString method of your CSpy class and return the value you want. Note that this will be what gets returned EVERY time you call ToString on an object of that type though, so if that's not what you want then you would need to do it a different way.

Can I also ask, is it really necessary to name your class "CSpy"? I assume that the "C" is a C/C++ style prefix for a class. Think about all the classes you use from the .NET Framework. Do you forget that they are classes without the "C" prefix? If not then do you really need it on your class names? You're making your own code inconsistent with the rest of the Framework by doing so.
 
Thanks for the replies, got it working now with that help.


Can I also ask, is it really necessary to name your class "CSpy"? I assume that the "C" is a C/C++ style prefix for a class. Think about all the classes you use from the .NET Framework. Do you forget that they are classes without the "C" prefix? If not then do you really need it on your class names? You're making your own code inconsistent with the rest of the Framework by doing so.

I'm afraid that I am just following what I am learning through lectures, for example : Cspy / CBankAccount / CRain they may have some leftover C/C++ style or may just be over emphasising using classes and I am just picking up bad habits. <<

Thanks again ^^
 
Excerpt from .NET Framework Developer's Guide : General Naming Conventions
Do not use Hungarian notation.
Hungarian notation is the practice of including a prefix in identifiers to encode some metadata about the parameter, such as the data type of the identifier.
Excerpt from .NET Framework Developer's Guide : Names of Classes
Do name classes, interfaces, and value types with nouns, noun phrases, or occasionally adjective phrases, using Pascal casing.

Do not give class names a prefix (such as the letter C).
 
Back
Top