【问题标题】:How to access a List collection across winforms如何跨winforms访问列表集合
【发布时间】:2013-08-30 16:13:47
【问题描述】:

我有这个自定义类的列表。

public class Item
{
    public string @Url;
    public string Name;
    public double Price;

    public Item(string @url, string name, double price)
    {
        this.Url = url;
        this.Name = name;
        this.Price = price;
    }


    public void setPrice(Button button)
    {
        this.Price = Convert.ToDouble(button.Text);
    }
}

现在在我的主winform中声明了这个

List<Item> items = new List<Item>();

这些字段是通过添加按钮设置的,如下所示,它们从 3 个文本框中获取信息并将其存储在一个新项目中。

Regex url = new Regex(@"^[a-zA-Z0-9\-\.]+\.(com|org|net|ca|mil|edu|COM|ORG|NET|CA|MIL|EDU)$");
Regex name = new Regex(@"^[0-9, a-z, A-Z, \-]+$");
Regex price = new Regex(@"^[0-9, \.]+$");

if (url.IsMatch(urlText.Text) && name.IsMatch(nameText.Text) && price.IsMatch(priceText.Text))
{
     itemListBox.Items.Add(nameText.Text);
     double item_Price = Convert.ToDouble(priceText.Text);
     items.Add(new Item(@itemURL.Text, itemName.Text, item_Price));
     nameText.Clear();
     priceText.Clear();
     urlText.Clear();
}
else
{
     match(url, urlText, urlLabel);
     match(price, priceText, priceLabel);
     match(name, nameText, nameLabel);
}

正如您在上面的代码中看到的,它还将项目的名称添加到项目列表框中。现在我有另一个窗口窗体,当单击编辑按钮时会弹出。如何使编辑表单中的项目列表框显示与主窗口表单中的项目列表框完全相同?

在一个基本问题中,我如何将项目列表传输到编辑表单。我已经尝试通过构造函数传递它,因为我希望编辑的信息保持不变,无论 winform 是什么。构造函数在编辑表单中声明:

public Edit(List<Item> i)
{
    itemList = i;
    InitializeComponent();
}

当我加载列表框时

foreach (Item i in itemList)
{
   itemListBox.Items.Add(i.Name);
}

列表框显示名称而不是名称的实际值

更新 1:

更新 2:

我的主winform代码http://pastebin.com/mENGKdnJ

编辑winform代码http://pastebin.com/tvp95jQW

不要关注打开的文件对话框 我还不知道如何编写代码 到目前为止,这个程序的代码已经教会了我很多,因为我边走边学。

【问题讨论】:

  • 好的,所以你尝试了一些东西(顺便说一下,根本不需要ref)。发生了什么?
  • itemListBox.Items.Add(i.Name); 上放置断点以确认 .Name 包含“名称”。
  • 提示:如果你想成为编辑窗体来阻止主窗体直到它关闭,使用 ShowDialog() 而不是 Show()
  • 非常感谢您!

标签: c# winforms


【解决方案1】:

您不需要将列表作为 ref 发送,仅在更改(指针)实例本身时才需要 ref。添加/删除项目不会影响列表实例。

更新:

列表框使用 ToString() 来呈现项目的一些描述。

public class Item
{
    public string @Url;
    public string Name;
    public double Price;

    public Item(string @url, string name, double price)
    {
        this.Url = url;
        this.Name = name;
        this.Price = price;
    }


    public void setPrice(Button button)
    {
        this.Price = Convert.ToDouble(button.Text);
    }

    public override string ToString()
    {
        // example: return string.Format("{0} -> {1}", this.Name, this.Price);
        return this.Price;
    }

}

所以你不应该添加名称,而是添加对象

foreach (Item i in itemList)
{
   itemListBox.Items.Add(i);
}

上瘾(小提示)

public class Item
{
    public string @Url {get; set;}
    public string Name {get; set;}
    public double Price {get; set;}

    public Item(string @url, string name, double price)
    {
        this.Url = url;
        this.Name = name;
        this.Price = price;
    }
}

SetPrice(转换为 double 的不应放在此处)

更新__________________

我想我看到了:

if (url.IsMatch(urlText.Text) && name.IsMatch(nameText.Text) && price.IsMatch(priceText.Text))
{
     itemListBox.Items.Add(nameText.Text);
     double item_Price = Convert.ToDouble(priceText.Text);
     items.Add(new Item(@itemURL.Text, itemName.Text, item_Price));
     nameText.Clear();
     priceText.Clear();
     urlText.Clear();
}

您将 nameText.Text 添加到列表框,但您将 itemName.Text 传递给项目构造函数。

与正则表达式匹配器中的 urlText.Text 相同,但将 itemURL.Text 传递给构造函数。

【讨论】:

  • 进行了所有建议的更改,但不幸的是,我仍然遇到同样的问题,请查看主帖以获取显示的图片。
  • 你检查过清单上的内容了吗?在 itemListBox.Items.Add(i.Name) 上放一个断点;要确认 i.Name 包含“名称”,我认为将一个空列表传递给您的编辑表单。并且名称是列表框的默认内容
  • 我放了一个断点下面的值在那里 Name = "Name" Price是正确的值 URL = "URL"
  • 对不起,不清楚部分 i.Name = "Name" 是的,这是错误的,这就是为什么我不明白只有价格被转移,其他一切都不正确
  • 更新了主帖中的 pastebin 链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多