Click here to Skip to main content
15,067,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I back again with yet another problem in my code.
I populate my first listbox like so:
C#
private void addList_Click(object sender, EventArgs e)
        {
            List<string> _scoreboard = new List<string>();
            ProSailor sailor1 = new ProSailor(1, 24, 7);
            ProSailor sailor2 = new ProSailor(2, 23, 14);
            ProSailor sailor3 = new ProSailor(3, 20, 5);
            CharitySail sailor4 = new CharitySail(4, 210, "WWF");
            CharitySail sailor5 = new CharitySail(5, 75, "Anaphylaxis Campaign");
            CharitySail sailor6 = new CharitySail(6, 325, "Childs Play");
            NoveltySail sailor7 = new NoveltySail(7, 210, "Games Aid", "Batman", 2);
            NoveltySail sailor8 = new NoveltySail(8, 210, "Extra-Life", "Legend of Zelda", 3);
            NoveltySail sailor9 = new NoveltySail(9, 210, "water", "Disney", 5);

            _scoreboard.Add(sailor1.ToString());
            _scoreboard.Add(sailor2.ToString());
            _scoreboard.Add(sailor3.ToString());
            _scoreboard.Add(sailor4.ToString());
            _scoreboard.Add(sailor5.ToString());
            _scoreboard.Add(sailor6.ToString());
            _scoreboard.Add(sailor7.ToString());
            _scoreboard.Add(sailor8.ToString());
            _scoreboard.Add(sailor9.ToString());

            listBox2.DataSource = _scoreboard;
        }

Now I would like to transfer this data to another listbox with one of two messages!
Either:
C#
private void btnDNF_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(listBox2.SelectedItem + " This Sailor Did Not Finish.");
        }

Or
C#
private void btnFinished_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(listBox2.SelectedItem + " They finished the race in " + textBox1.Text + " hour(s) " + textBox2.Text + " minute(s) and " + textBox3.Text + " second(s)");
        }


I end up with something that says: "Sailor 1, 24 years of age has 7 races completed. They Finished the Race in 1 hour(s), 07 minute(s) and 45 second(s)" Or instead of the time, "Did Not Finish the Race."

My problem is that the number of 7 races doesn't increase to 8 when I click finished. I have no clue how to go about doing this.

Also of the numbers are stored in another class. Here is the ProSailor Class:

C#
class ProSailor : Sailor
    {
        public int Age { get; set; }
        public int NoOfSailsFin { get; set; }

        public ProSailor(int number, int age, int noOfSailsFin)
            : base(number)
        {
            this.Age = age;
            this.NoOfSailsFin = noOfSailsFin;
        }

        public override string ToString()
        {
            return "Sailor " + Number + ", " + Age + " years of age has " + NoOfSailsFin + " races completed.";
        }

Basically, I need "noOfSailsFin" to increase by 1 number when the finished button is clicked.

Also, if someone could tell me how to make it so that this outputs from one listbox to the other and then deletes/removes the item from the first listbox that would be super helpful too!

Thanks!
Posted

1 solution

You have multiple problems making it difficult to offer any help that doesn't lead to other needed changes.

Personally, I think you should rework your application and code it without the UI. Create a Console application and redo this logic. I believe if you try to code it without the UI you will start to see where you went wrong.

Basically, you are storing all your objects in a list of strings and there is no efficient way to pull out the selected item as an object. You need to store the items as objects so you can fetch the selected list item from the object collection and update its properties. Once you can retrieve the selected object you can then increment the NoOfSailsFin property.

To answer your questions as best as possible:
The NoOfSailsFin never increments because you never update the value after you initialize the object. You set the 7 here...
C#
ProSailor sailor1 = new ProSailor(1, 24, 7);

...but you never update it when the btnFinished_Click event is handled. And there is no easy way to get back to your sailor1 object.

As for removing the list item after you move it to the other list; since you are binding the _scoreboard List to the listBox2 control, you need to first update the _scoreboard List and then rebind it to the listBox2 control. So, remove the item from the _scoreboard List and then rebind to the ListBox.
   
v3

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