我遇到了問題。 我正在創建一個使用我們的電話配置文件的小應用程序。 我創建了一個類叫電話並給予其4個屬性:Listbox displayMember not working
private int extension;
private String sExtension;
private String userName;
private String filePath;
我已經包含了相應的get/set方法,以及:
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";
我仍然遇到的問題是仍然由對象名稱填充(參考MSDN Article)
我已經通過這個論壇上一對夫婦的文章閱讀ListBox
並且大多數提到同樣的問題,我可能不會正確呼叫ListBox.DisplayMember
,但我相信我。
編輯:我已嘗試退回List<T>
,ArrayList
,Array[]
。
編輯:代碼實用
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;
}
代碼看起來沒問題。我猜,我們必須看到你的'Utility.populatePhoneList'函數。 – LarsTech
可以顯示Utility.populatePhoneList(fileList)的代碼 –
在週末深入探討這個問題之後,我開始禁用'ListBox'上的一些事件處理程序,並發現它正常工作。我發現我的'ListBox.SelectedIndexChanged'事件在系統完全填充前捕獲了列表。我的解決方案是將'ListBox.SelectionMode'變成none,然後在ListBox被填充後重置。 –