簡體   English   中英

列表框顯示成員不起作用

[英]Listbox displayMember not working

我遇到了一個問題。 我正在創建一個用於處理電話配置文件的小型應用程序。 我創建了一個名為Phone的類,並為其指定了4個屬性:

private int extension;
private String sExtension;
private String userName;
private String filePath;

我已經包括了各自的獲取/設置方法以及:

public String Extension
{
    get
    {
        return sExtension;
    }
}
public String Path
{
    get
    {
        return filePath;
    }
}

我創建了一個執行大多數靜態工作的實用程序類。 包括創建電話對象的List<Phone>的方法以填充ListBox

一切正常,直到將List<Phone>返回為ListBox的數據源。 我都設置了:

fileList = Directory.EnumerateFiles(path, "ext-*");
lst_Files.DataSource = Utility.populatePhoneList(fileList);
lst_Files.DisplayMember = "Extension";
lst_Files.ValueMember = "Path";

我仍然遇到的問題是ListBox仍然由對象名稱填充(參考MSDN Article

我已經閱讀了該論壇上的幾篇文章,並且大多數提到了相同的問題,我可能沒有正確調用ListBox.DisplayMember ,但我相信我是。

編輯:我試圖返回List<T>ArrayListArray[]

編輯:實用程序代碼

public static List<Phone> populatePhoneList(IEnumerable<String> newFileList)
{
    List<Phone> phones = new List<Phone>();
    Phone p = null;

    for (int i = 0; i < newFileList.Count(); i++)
    {
        p = getPhoneInfo(newFileList.ElementAt(i));
        phones.Add(p);
    }
    return phones;
}

public static Phone getPhoneInfo(String newPath)
{
    StreamReader sr = new StreamReader(newPath);
    Phone p1 = new Phone();
    p1.setFilePath(newPath);            
    String testLine;

    while (sr.Peek() >= 0)
    {
        testLine = sr.ReadLine();
        if (testLine.Contains("reg.1.displayName"))
            p1.setUserName(testLine.Substring(testLine.IndexOf("\"") + 1, ((testLine.LastIndexOf("\"") - 1) - testLine.IndexOf("\""))));
        if (testLine.Contains("reg.1.address"))
            p1.setExtension(testLine.Substring(testLine.IndexOf("\"") + 1, ((testLine.LastIndexOf("\"") - 1) - testLine.IndexOf("\""))));
    }
    return p1;
}

在周末研究了這個問題之后,我開始在ListBox上禁用了一些事件處理程序,並發現它可以正常工作。 我發現我的ListBox.SelectedIndexChanged事件在系統完全填充之前正在捕獲列表。 我的解決方案是將ListBox.SelectionMode為none,然后在ListBox被填充后將其重置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM