如何将Key + Value添加到ListBox?

时间:2012-10-11 21:15:55

标签: c#

我有这个按钮点击代码,现在正在使用:

private void button6_Click(object sender, EventArgs e)
        {

            using (var w = new StreamWriter(keywords))
            {
                crawlLocaly1 = new CrawlLocaly();
                crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
                DialogResult dr = crawlLocaly1.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    if (LocalyKeyWords.ContainsKey(mainUrl))
                    {
                        LocalyKeyWords[mainUrl].Clear();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    else
                    {
                        LocalyKeyWords[mainUrl] = new List<string>();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    Write(w);
                    listBox1.Items.Add("Url: " + LocalyKeyWords[mainUrl] + " --- " + "Localy KeyWord: " + crawlLocaly1.getText());
                }
                if (dr == DialogResult.Cancel)
                {
                    Write(w);
                }
            } 
        }

当我对mainUrl(key)或crawlLocaly1.getText()(键的值)进行更改时,我需要这样做,所以它也会在ListBox中实时更改。我试着这样做:

listBox1.Items.Add("Url: " + LocalyKeyWords[mainUrl] + " --- " + "Localy KeyWord: " + crawlLocaly1.getText());

但它没有显示密钥及其值并添加新行而不是仅更新密钥的值(如果密钥已经存在)或者如果密钥不存在于List中以向ListBox添加新密钥及其价值。

如何制作它以便在ListBox中实时更新?


确定将ListBox行更改为:

listBox1.Items.Add("Url: " + mainUrl + " --- " + "Localy KeyWord: " + crawlLocaly1.getText());

现在它添加它很好,但它将它添加为新行。我希望它会添加它并替换已经存在于ListBox中的mainUrl的行。

如果ListBox中不存在mainUrl,则将其值添加到新行中。 但是,如果ListBox中存在Key(mainUrl),则替换它或仅更改值(crawlLocaly1.getText())。

3 个答案:

答案 0 :(得分:1)

纠正行
当你写LocalyKeyWords[mainUrl]时,你得到一个List<string>对象。因此,当您将此List连接到字符串时,实际上是调用方法List<string>.ToString()

尝试使用此行:

listBox1.Items.Add("Url: " + mainUrl + " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]); 

对于实时问题
要实时编辑该行,您需要删除之前的行,如

private void button6_Click(object sender, EventArgs e) 
        { 

            using (var w = new StreamWriter(keywords)) 
            { 
                crawlLocaly1 = new CrawlLocaly(); 
                crawlLocaly1.StartPosition = FormStartPosition.CenterParent; 
                DialogResult dr = crawlLocaly1.ShowDialog(this); 
                if (dr == DialogResult.OK) 
                { 
                    int line = listBox1.Items.Count;
                    if (LocalyKeyWords.ContainsKey(mainUrl)) 
                    { 
                        line = listBox1.Items.IndexOf("Url: " + mainUrl+ " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);

                        listBox1.Items.Remove("Url: " + mainUrl+ " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
                        LocalyKeyWords[mainUrl].Clear(); 
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText()); 
                    } 
                    else 
                    { 
                        LocalyKeyWords[mainUrl] = new List<string>(); 
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText()); 
                    } 
                    Write(w); 
                    listBox1.Items.Insert(line, "Url: " + mainUrl+ " --- " + "Localy KeyWord: " + LocalyKeyWords[mainUrl][0]);
                } 
                if (dr == DialogResult.Cancel) 
                { 
                    Write(w); 
                } 
            }  
        } 

正如您所知,处理包含两个或更多变量的数据。最好使用ListView控件。此控件知道如何像列表一样工作,它可以将数据拆分为列。 请参阅msdn。中的ListView

答案 1 :(得分:0)

如果我理解正确,请尝试以下方法:

// Find the existing item. (s.Contains could be changed to something else, like s.StartsWith.)
var existingItem = listBox1.Items.OfType<string>()
    .FirstOrDefault(s => s.Contains(mainUrl));

if (existingItem != null)
{
    // Item exists, find index of it and change it.
    var index = listBox1.Items.IndexOf(existingItem);
    listBox1.Items[index] = "Url: " + mainUrl + " --- " + "Localy KeyWord: " + crawlLocaly1.getText();
}
else
{
    // Item is new, add it.
    listBox1.Items.Add("Url: " + mainUrl + " --- " + "Localy KeyWord: " + crawlLocaly1.getText());
}

答案 2 :(得分:0)

如果我理解正确,您应该添加以下类:

public class KeyWordWrapper
{
    public string Url { get; private set; }
    public string Value { get; set; }

    public KeyWordWrapper(string url)
    {
        this.Url = url;
    }

    public override string ToString()
    {
        return string.Format("Url: {0} --- Localy KeyWord: {1}", Url, Value);
    }
}

然后按如下方式定义LocalyKeyWords

Dictionary<string, KeyWordWrapper> LocalyKeyWords =
  new Dictionary<string, KeyWordWrapper>();

将这两行放入表单的构造函数

listBox1.DisplayMember = "Value";
listBox1.DataSource = LocalyKeyWords;

最后更新button6_Click如下:

if (dr == DialogResult.OK)
{
    KeyWordWrapper keyWordWrapper;
    if (!LocalyKeyWords.TryGetValue(mainUrl, out keyWordWrapper))
    {
        keyWordWrapper = new KeyWordWrapper(mainUrl);
        LocalyKeyWords.Add(mainUrl, keyWordWrapper);
    }

    keyWordWrapper.Value = crawlLocaly1.getText();
    Write(w);
}