【问题标题】:Adding items to a listbox from a different class将项目从不同的类添加到列表框
【发布时间】:2021-08-19 11:28:48
【问题描述】:

我想将项目添加到名为“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.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
            } else if ( _year.Any(char.IsDigit) == false || _dailycharge.Any(char.IsDigit) == false)
            {
                MessageBox.Show("Error! Invalid input, please try agaain.", "Invalid Input", MessageBoxButtons.OK, 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);
        }
    }
}

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

【问题讨论】:

  • 如何填写 Business.VehicleList?技术是什么?是 WinForms、WPF 还是其他?
  • 错误是什么?你的意思是它不起作用。是访问问题吗?
  • @AdamJachocki 使用 windows 窗体见附件
  • @Ramji 我没有收到任何错误,它只是在调用时不起作用
  • 所有相关问题的来源:MainForm fm = new MainForm(); in addVehicle():您正在创建 MainForm 类的新实例,与现有的无关。调用它的updateDisplay() 当然不会做任何事情。

标签: c# winforms controls


【解决方案1】:

问题是由于AddVehicle 中的以下行:

MainForm fm = new MainForm();

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

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

【讨论】:

  • 你能写下代码我会怎么做吗?非常感谢!
猜你喜欢
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多