【问题标题】:Changing a method to a shared method accessible in another form将方法更改为以其他形式可访问的共享方法
【发布时间】:2017-05-14 17:04:39
【问题描述】:

我之前创建了一个程序,该程序将文本文件中的数据显示到列表视图中,用户可以单击列表视图中的名称并显示该名称的电话号码。现在我正在尝试向程序添加一个新表单,该表单允许用户从组合框中选择名称,然后在文本框中显示名称并允许用户更改名称并将其保存在文本文件中。

Original Program

无论如何,我正在尝试使原始程序中的加载事件以新形式可访问,但我似乎无法弄清楚如何做到这一点。这是我的代码:

public partial class VendorsDictionary : Form
{
    public VendorsDictionary()
    {
        InitializeComponent();
    }

    private Dictionary<string,string> vendorPhones = new Dictionary<string,string>();

    public void VendorsDictionary_Load(object sender, EventArgs e)
    {
        string currentLine;
        string[] fields = new string[2];
        StreamReader vendorReader = new StreamReader("Vendor.txt");

        while (vendorReader.EndOfStream == false)
        {
            currentLine = vendorReader.ReadLine();
            fields = currentLine.Split(',');

            vendorPhones.Add(fields[1], fields[6]);

            string[] name = { fields[1] };
            string[] city = { fields[3] };
            string[] state = { fields[4] };
            string[] zipcode = { fields[5] };
            string[] phone = { fields[6] };

            for (int i = 0; i < name.Length; i++)
            {
                lvDisplay.Items.Add(new ListViewItem(new[] { name[i], city[i], state[i], zipcode[i] }));
            }    
        }

        vendorReader.Close();
    }

    private void lvDisplay_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lvDisplay.SelectedItems.Count>0)
        {
            ListViewItem item = lvDisplay.SelectedItems[0];
            lblName.Text = item.SubItems[0].Text;
            lblPhone.Text = vendorPhones[item.SubItems[0].Text];
        } 
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        UpdateVendor updateVendor = new UpdateVendor();
        updateVendor.Show();
    }
}

我尝试将其更改为静态以及我在这里找到的许多其他想法,但似乎无法使其发挥作用。任何帮助将不胜感激!

【问题讨论】:

  • 那么为什么需要VendorsDictionary_Load 函数呢?假设这个函数只将结果保存到lvDisplay
  • 在您的UpdateVendor 表单上创建一个公共属性(我们称之为public string Name;,并将其设置为组合框选定项(名称)。然后使用updateVendor.ShowDialog(); 启动表单(@987654329 @ 将阻止在您的VendorsDictionary 表单上执行,直到关闭updateVendor 表单。然后您可以检查updateVendor.Name 属性并且您拥有用户选择的名称。
  • @RufusL 我将如何将其设置为组合框所选项目?我不需要用名称填充组合框吗?对不起,如果这是一个愚蠢的问题,我刚开始学习如何编码 =/
  • 哦,我明白我误解了整个问题。我稍后会尝试发布答案,但基本上它总是一个类似的答案。您可以在您的第一个表单上公开该方法(可能不是整个表单加载方法,而是其他一些返回名称的方法),然后将 form1 的实例传递给 form2。或者,获取 form1 中的名称列表,然后将该列表作为 form2 方法的参数或作为 form2 属性传递给 form2。

标签: c# winforms dictionary methods


【解决方案1】:

您的VendorsDictionary_Load 方法中不需要所有这些代码。你真的可以把它清理干净。我会告诉你怎么做。

将此类添加到您的项目中:

public class Vendor
{
    public string City { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}

将此方法添加到VendorsDictionary:

public static IEnumerable<Vendor> LoadVendors()
{
    var vendors =
        File.ReadAllLines("Vendor.txt").Select(x => x.Split(','))
            .Select(x =>
            new Vendor
            {
                Name = x[1],
                City = x[3],
                State = x[4],
                ZipCode = x[5],
                Phone = x[6]
            }).ToList();

    return vendors;
}

VendorsDictionary_Load中的代码改成这样:

public void VendorsDictionary_Load(object sender, EventArgs e)
{
    var vendors = LoadVendors();
    foreach (var thisVendor in vendors)
    {
        vendorPhones.Add(thisVendor.Name, thisVendor.Phone);
        lvDisplay.Items
            .Add(new ListViewItem(new[] { thisVendor.Name, thisVendor.City,
                thisVendor.State, thisVendor.ZipCode }));
    }
}

在任何地方使用LoadVendors 方法:

var someOtherUsage = VendorsDictionary.LoadVendors();

要改进此代码,请将路径发送到LoadVendors,以便您可以从任何位置加载供应商。另外,VendorDictionary 不是一个好名字。

【讨论】:

    猜你喜欢
    • 2016-06-17
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多