Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Adding Custom objects into a Controls/Listboxes [OOP]


My second tutorial here. This tutorial is more about the object orientation methods that can be used to tackle more complex programming solutions.

So in this tutorial I will explain and show you how you can add custom objects into controls. In general you can use this example with any windows control which you can add objects to. Like the ComboBox. TreeNode. This is just no exclusive to VB, It works on C# too, and I'm sure it would work on any object orientated language.

So Let's begin by designing our form first.

[Image: gdKurGY.png]

Now that's done, we can now create our class Persion() where we will add our Properties and Methods.

[Image: SqQdszL.png]

After we done that, we now subscribe to the Clicked Event for our add button.
Inside that sub routine we instantiate our persons class. When we Instantiate we use the New Keyword.
[Image: rGdVDnx.png]

In this context, what we're doing is creating a New copy of the Person class. So in simpler terms. Each time we Instanciate. We create a new stickman.

[Image: 0VkpBCj.png]

And we define this new stickman, by giving him a Firstname, Lastname, Age and his own HF UID.

So each time we click the add button, a new stick man will be created and his properties(Firstname) will be the value of the txtFirstname textbox and so forth. And then that person is now added to then using the listbox using the ListBox1.Items.Add() method

Finally, this is the most important bit of our tutorial. This is how we get back these objects that we have added into our listbox.

[Image: gPUNo0d.png]

The direct cast allows us to convert the SelectedItem object to the Person object/class we have inserted. And this way we can get back the object we had previously added and view all its unique properties.

Here is a short clip of the end result of this tut.





so you can see the uses of this type of programming technique. Allowing us to use objects to accomplish complex tasks. And thus making programming easier when you're faced with scenarios where this method could be useful.

Here are the the vb.net code on pastebin with syntax highlighting.
http://pastebin.com/wvPU8BbS
and here's the .Zip of the source code.

https://www.mediafire.com/?do58dtcuyu46pd3

enjoy.
Some feedback would be nice too
Great! That can be useful in a lot of cases!

Thanks for this clear tutorial!
Why are you using string.format on something with one variable? And that variable being on the end of the string.
[Image: vjPWhyy.gif]
Fair enough good point, but do remember I constructed this thread whilst I had no sleep. So yaa..
I thought you used arrays :(. Oh well thanks the possible the core of an future inventory system I'm making.
Nope, using arrays would be really messy when you have to store more than 1 property. And you'd have to constantly keep resizing your array, unless you use a List. But this method is the best to my knowledge on doing something like this. I just wish more would see this.
Thanks for the help Haze, I appreciate it
[Image: aqcF8UA.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
by AnonynukSep 28, 2026 10:32 AM 2 39 Sep 28, 2026 10:32 AM
Last Post: Anonynuk
[.NET] by ExproMar 25, 2022 03:58 AM 8 724 Mar 25, 2022 03:58 AM
Last Post: SaketSaket
[Java] by PsychoJul 3, 2021 11:15 PM 24 1,973 Jul 3, 2021 11:15 PM
Last Post: Lookdadon