Daniel Lip
Daniel Lip

Reputation: 11325

How do i add the Key + Value to a ListBox?

I have this button click code wich is working now:

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);
                }
            } 
        }

I need that when i make a change either to the mainUrl(key) or to the crawlLocaly1.getText()(the value of the key) so it iwll change in real time also in the ListBox. I tried to do it like this:

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

But its not showing the key and its vlaue and its adding a new line instead of updating only the value of the key if the key already exist or if the key is not exist in the List to add to the ListBox the new key and its value.

How do i make it so it will update it in real time in the ListBox ?


Ok changed the ListBox line to:

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

Now its adding it good but its adding it as a new line. I want that it will add it and replace the line that already the mainUrl exist in the ListBox.

And if the mainUrl is not exist in the ListBox then add it with its value in a new line. But all the time if the Key(mainUrl) exist in the ListBox replace it or change only the value(crawlLocaly1.getText() ) .

Upvotes: 1

Views: 281

Answers (3)

Nick Hill
Nick Hill

Reputation: 4917

If I understood you correctly, you should add the following class:

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);
    }
}

Then define your LocalyKeyWords as follows:

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

Put with these two lines into your form's constructor

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

And finally update button6_Click as follows:

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);
}

Upvotes: 0

nrofis
nrofis

Reputation: 9796

To correct the line
when you write LocalyKeyWords[mainUrl] you get a List<string> object. So when you concat this List to string, you actually call to the method List<string>.ToString().

Try to use this line insted:

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

For the real time problem
To edit the line in real time you need to remove the previous line before, like that

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); 
                } 
            }  
        } 

Just for you know, to work with data that contains a two or more variables. Its better to use the ListView control. This control know how to work like list, and it can split the data to columns. See ListView in msdn.

Upvotes: 1

Mario S
Mario S

Reputation: 11955

If I understood you correctly, try something like this:

// 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());
}

Upvotes: 0

Related Questions