Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hey to all of you.

I've been doing a really exhausting project about inheritence and composition; and after I finished coding classes, I started doing the windows form, and here, I really got overwhelmed, as I couldn't find out how to add an object property (object.name) to the listbox.
I've tried and tried, but no matter what I do, I always get stuck having some problem with listbox object adding, either It only adds the 1st object, then other elements are duplicated, or the listbox is never shown at all.

Here's a code snippet for Form1.cs :

C#
namespace Map
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       public  ArrayList countries = new ArrayList();
        private void button1_Click(object sender, EventArgs e)
        {

            Country c = new Country(cntrynm.Text, comboBox1.Text, Convert.ToDouble(areatxt.Text), Convert.ToInt32(numericUpDown1.Value), nibor1.Text, nibor2.Text, nibor3.Text, Convert.ToDouble(xcontxt.Text), Convert.ToDouble(ycontxt.Text), Convert.ToDouble(zcontxt.Text));

            countries.Add(c);       
            listBox1.Items.Add(countries.Add(c.Country_Name));       //This Approach is totally wrong



            //OR  
            //this one below:


           listBox1.Items.Clear(); //this clear method is used to not have any remaining duplicates from previous clicks
   for (int i = 0; i < countries.Count; i++)
   {                                    
 
             countries.Add(c);     //we insert the object in ArrayList "countries"

             countries[i]= new Country(cntrynm.Text, comboBox1.Text, Convert.ToDouble(areatxt.Text), Convert.ToInt32(numericUpDown1.Value), nibor1.Text, nibor2.Text, nibor3.Text, Convert.ToDouble(xcontxt.Text), Convert.ToDouble(ycontxt.Text), Convert.ToDouble(zcontxt.Text));
                listBox1.Items.Add((Country)countries[i]);   //After all this thinking, it was totally not working
                 
       //OR this third way of approach:

           (Country)countries[i] = c;
           listBox1.Items.Add((Country)countries[i].);
                                                              //totally nothing
                
                }
          

          

        }


here's the download link for those of you who are intrested in giving me a hand:
http://www.ziddu.com/download/19354518/Map.zip.html"&gt;http://www.ziddu.com/download/19354518/Map.zip.html[^]
(only 66 KB)

P.S: there are lots of classes and coding, so everything's set well, and you only have to correct for me the
Form1.cs coding, specificly the listbox.

so, please if you have any idea who to use objects and object's properties with listbox adding, thanks for the help forwards

you can send me the corrected version to my email: obzajd@mail.com

Thanks for listening^]
Posted
Updated 10-May-12 3:12am
v3
Comments
bbirajdar 10-May-12 8:46am    
Sorry..You need a talented person.. I can't help you in this ( because I am not.. :()
obzajd 10-May-12 8:58am    
I meant by talented, is anyone who knows windows form application with OOP experience, so that "someone" can answer me switfly and easily.
I've never meant anything personal. I'm sorry If you thought I sounded,by any means, offensive.
Prasad_Kulkarni 10-May-12 8:50am    
Hope you get your 'someone talented' soon
All the best!
Sandeep Mewara 10-May-12 9:01am    
Don't take it personally, OP might not have said that to someone specific.
Sandeep Mewara 10-May-12 8:59am    
It would be better to post your issue specific code snippet here to look at. I doubt anyone will download and see what's wrong in your code.

You should not be passing objects to the listBox1.Items.Add() unless their ToString() method returns the correct value. That is easy to check in your class definition. Alternatively, just add c.CountryName property (or whatever it is called). For more information take a look at the documentation[^].
 
Share this answer
 
Comments
obzajd 10-May-12 11:38am    
I've overrided to.string method inside the country class
then I wrote inside the loophole:
listBox1.Items.Add(countries[i].ToString());

it's now ready, and it's correct!!!
thansks
Try this:
private void button1_Click(object sender, EventArgs e) {
 
            Country c = new Country(cntrynm.Text, comboBox1.Text, Convert.ToDouble(areatxt.Text), Convert.ToInt32(numericUpDown1.Value), nibor1.Text, nibor2.Text, nibor3.Text, Convert.ToDouble(xcontxt.Text), Convert.ToDouble(ycontxt.Text), Convert.ToDouble(zcontxt.Text));
 
            countries.Add(c);  // insert new country object before looping so it only goes in one time
 
            listBox1.Items.Clear(); //clear your list

            for (int i = 0; i < countries.Count; i++)   {
                listBox1.Items.Add((Country)countries[i].Country_Name);                                                                    
            }

        }
 
Share this answer
 
Comments
Richard MacCutchan 10-May-12 10:15am    
The cast from string to Country should not be included.
Stryder_1 10-May-12 10:30am    
Was attempting to use the author's own code as much as possible. I think he is attempting to cast the object in the array to a Country, not the name string. I don't know if the countries array is a typed list or just an object array, so I left it.
Richard MacCutchan 10-May-12 10:35am    
<i.i think="" he="" is="" attempting="" to="" cast="" the="" object="" in="" array="" a="" country<i="">
It's already a Country, I don't think he really knows what he is doing. I get the impression the the OP really needs some C# training or study. There are lots of other things wrong with this code that will cause other problems in the future if it is left as is.
Stryder_1 10-May-12 10:49am    
Yes, he doesn't know what he's doing, that's why he's posting here for help ;-)

And again yes, it's a country, but if it's stored in a generic object array, you won't be able to access the Country_Name property unless you explicitly cast it.
Richard MacCutchan 10-May-12 11:16am    
Indeed, you are correct.
listbox is not meant to be used as a multi column component, so its better for you to swap for listview component, anyway if you want to add items to your listbox try the following

//instance object and lets suppose your object have a name property there
Country c = new Country()
c.Name="USA"
ListBox1.Items.Add(c.Name)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900