移动到另一个列表框时增加ListBox项值 [英] Increase ListBox item value when moving to another listbox

查看:62
本文介绍了移动到另一个列表框时增加ListBox项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中又出现了另一个问题。

我填充了我的第一个列表框,如下所示:

I back again with yet another problem in my code.
I populate my first listbox like so:

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:

private void btnDNF_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(listBox2.SelectedItem + " This Sailor Did Not Finish.");
        }






Or

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)");
        }





我最终得到的结论是:水手1岁,24岁,已完成7场比赛。他们在1小时,07分钟和45秒(秒)完成比赛或者不是时间,没有完成比赛。



我的问题是当我点击完成后,7场比赛的数量不会增加到8。我不知道如何去做这件事。



这些数字也存储在另一个班级。这是ProSailor类:





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:

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.";
        }



基本上,单击完成的按钮后,我需要noOfSailsFin增加1个数字。



此外,如果有人可以告诉我如何制作它以便从一个列表框输出到另一个列表框然后从第一个列表框中删除/删除该项目也是非常有用的!



谢谢!


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!

推荐答案

您遇到了多个问题,难以提供任何不会导致其他必要更改的帮助。



就个人而言,我认为您应该重新编写应用程序并在没有UI的情况下对其进行编码。创建一个控制台应用程序并重做此逻辑。我相信如果您尝试在没有UI的情况下编写代码,您将开始看到出错的地方。



基本上,您将所有对象存储在字符串列表中并且没有有效的方法将所选项目拉出作为对象。您需要将项目存储为对象,以便从对象集合中获取所选列表项并更新其属性。一旦你可以检索所选对象,你就可以增加NoOfSailsFin属性。



尽可能回答你的问题:

NoOfSailsFin永远不会增加,因为在初始化对象后,您永远不会更新该值。你在这里设置了7 ...
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...
ProSailor sailor1 = new ProSailor(1, 24, 7);



...但是在处理btnFinished_Click事件时,您永远不会更新它。并且没有简单的方法可以回到你的sailor1对象。



至于将列表项移到另一个列表后删除列表项;由于您将_scoreboard List绑定到listBox2控件,因此需要先更新_scoreboard List,然后将其重新绑定到listBox2控件。因此,从_scoreboard List中删除该项,然后重新绑定到ListBox。


...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.


这篇关于移动到另一个列表框时增加ListBox项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆