在这里,我打开了一个单词/定义文本文件。我把它修整了,把词放进去newword,把定义放进去newdefn。我把它放在字典里,创建如下:
Dictionary<string, string> d = new Dictionary<string, string>();
我宣布为全球性的。由于它在while(!endofstream)循环内,所有单词和定义都存储在那里。
我的问题是如何将字典中的值放入listbox. 我的代码:
    public search()
    {
        InitializeComponent();                                
        Stream txtStream = Application.GetResourceStream(new
            Uri("/PanoramaApp1;component/word.txt", UriKind.Relative)).Stream;
        using (StreamReader sr = new StreamReader(txtStream))
        {
            string jon;
            //definition.Text = sr.ReadToEnd();
            while (!sr.EndOfStream)
            {
                jon = sr.ReadLine();
                newword = (word.Trim(new Char[] { '-',' ' }));
                newdefn = (defn.Trim(new Char[] { '-',' ' }));                 
                d.Add(newword, newdefn);                                  
            }
        }                     
    }
我现在想在文本框中进行搜索,并将结果显示在列表框中。但是我对“文本框选择已更改”中的部分有疑问。我在listbox.Items.Add(str);
 List<string> list = new List<string>(d.Keys);
 foreach (string str in list)
 {
     if (str.StartsWith(searchbox.Text, 
            StringComparison.CurrentCultureIgnoreCase))
     {
         listbox.Items.Add(str);
     }
 }