微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将项目从不同的类添加到列表框

如何解决将项目从不同的类添加到列表框

我想将项目添加到名为“mainFormRego”的列表框。如果我在它所在的 MainForm 类中调用它,则该方法有效,但如果我尝试在不同的类中调用它,则它不起作用。我已经实例化了回调。请帮助我我迷失了想法并且已经尝试了一段时间以找到解决方案。我正在使用 C# 和 windows 窗体。

MainForm 类(列表框所在的位置)


namespace VehicleSystem
{
    public partial class MainForm : Form
    {
public void updatedisplay()
        {
            mainFormRego.Items.Clear();
            foreach (Vehicle item in Business.VehicleList)
            {
                mainFormRego.Items.Add(item.Rego);
            }
        }
    }
}

添加车辆类别


namespace VehicleSystem
{
    public partial class AddVehicle : Form
    {
        public void addVehicle(string _rego,string _make,string _model,string _year,string _dailycharge)
        {
            MainForm fm = new MainForm();
            fm.updatedisplay();
            string[] inputs = { _rego,_make,_model,_year,_dailycharge };
            bool status = true;
            foreach (Vehicle item in Business.VehicleList)
            {
                if (item.Rego.ToString() == _rego)
                {
                    MessageBox.Show("Error! Invalid input,please try agaian.","Invalid Input",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    status = false;
                } else
                {
                    status = true;
                }
            }

            if (inputs.Any(x => string.IsNullOrWhiteSpace(x)))
            {
                MessageBox.Show("Error! Invalid input,please try agaain.",MessageBoxIcon.Error);
            } else if ( _year.Any(char.IsDigit) == false || _dailycharge.Any(char.IsDigit) == false)
            {
                MessageBox.Show("Error! Invalid input,MessageBoxIcon.Error);
            } else if (status == true)
            {
                Business.VehicleList.Add(new Vehicle() { Rego = _rego,Make = _make,Model = _model,Year = _year,DailyCharge = _dailycharge });
                this.Close();
            }
        }
   }
}

车辆列表


namespace VehicleSystem
{
    class Business
    {
        public const String fileName = "C:\\Users\\tbyrm\\Documents\\Workspace\\SDV601\\sdv601-s1-21-project-travisbyr\\save.dat";

        private static List<Vehicle> vehicleList = new List<Vehicle>();

        public static List<Vehicle> VehicleList { get => vehicleList; set => vehicleList = value; }

        public static void Save()
        {
            using (FileStream fileStream = new FileStream(fileName,FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(fileStream,VehicleList);
            }
        }

        public static void Retrieve()
        {
            using (FileStream fileStream = new FileStream(fileName,FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                VehicleList = (List<Vehicle>)formatter.Deserialize(fileStream);
            }
        }

        public static void RemoveVehicle(string rego)
        {
            VehicleList.RemoveAll((x) => x.Rego == rego);
        }
    }
}

请帮忙!非常感谢任何帮助。

解决方法

问题是由 AddVehicle 中的以下行引起的:

MainForm fm = new MainForm();

您正在创建 MainFormnew 实例,而不是引用当前显示的活动实例。因此,您正确地更新了一个列表,而不是您期望的列表(未显示 MainForm 的新实例上更新的列表)。

尝试传递对 mainFormRego 的引用并在另一个 class 内更新。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。