Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何跨winforms访问列表集合_C#_Winforms - Fatal编程技术网

C# 如何跨winforms访问列表集合

C# 如何跨winforms访问列表集合,c#,winforms,C#,Winforms,我有一个自定义类的列表 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 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>();
正如您在上面的代码中所看到的,它还将项目的名称添加到项目列表框中。现在我有了另一个windows窗体,当单击“编辑”按钮时会弹出。如何使编辑表单中的项目列表框与主windows表单中的项目列表框完全相同

在一个基本问题中,如何将项目列表转移到编辑表单中。我尝试将其传递给构造函数,因为我希望无论winform如何,编辑的信息都保持不变。已在编辑表单中声明构造函数:

public Edit(List<Item> i)
{
    itemList = i;
    InitializeComponent();
}
列表框显示名称,而不是名称的实际值

更新1:

更新2:

我的主要winform代码

编辑winform代码


不要注意“打开文件”对话框,我不知道如何编码,但到目前为止,编码这个程序已经教会了我很多东西,因为我边走边学

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

更新:

listbox使用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);
}

吸毒成瘾(小提示)

SetPrice(不应将转换为双精度)

更新

我想我看到了:

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文本添加到列表框,但将itemName文本传递到项构造函数


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

好,所以您尝试了一些方法(
ref
)。发生了什么?在itemListBox.Items.Add(i.Name)上放置一个断点;要确认.Name包含“Name”。提示:如果您想成为editform阻止主窗体直到其关闭,请使用ShowDialog()而不是Show()。非常感谢!做了所有建议的更改,但不幸的是,我仍然被同一个问题困扰。检查主帖子,查看显示内容的图片。你检查了列表上的内容了吗?在itemListBox.Items.Add(i.Name)上放置断点;为了确认i.Name包含“Name”,我想编辑表单时会向您传递一个空列表。名称是ListBox的默认内容我放了一个断点下面的值有Name=“Name”Price是正确的值URL=“URL”很抱歉第一部分不清楚。Name=“Name”是的,这是错误的,这就是为什么我不明白,只是价格被转移了其他一切都不正确,在主帖子中用pastebin链接更新
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;
    }
}
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();
}