Home > Net >  C# add item to listbox from list
C# add item to listbox from list

Time:05-10

When a shortcut is pressed, a value is added to the list and passed to the listbox to add an item. However, if AddRange(RecentColors.ToArray() is used, not only new list items are added, but they are added continuously. If a new value is added to a list value, is there a way to add only that new value to the listbox?

 private void mainForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.C)
        {
            RecentColors.Add(hexToRGB(hexcode.Text));
            for(int i = 0; i < RecentColors.Count; i  )
            {
                color_list.Items.AddRange(RecentColors.ToArray());
            }
        }
    }

Capture1

Capture2

When i add one item, i can't change the format that is already added..

CodePudding user response:

Here is the definition for AddRange:

Adds the elements of the specified collection to the end of the List<T>.

(source).

I could be misinterpreting what you're trying to do here, but you're adding the entire "RecentColors" sequence, every time your for loop is called.

CodePudding user response:

The problem is that you has RecentColors with all items and each key press you add all items in the listbox. Try adding one element in RecentColors and also in the listbox:

var item = hexToRGB(hexcode.Text);
RecentColors.Add(item);
color_list.Items.Add(item);

UPDATE

Ok, first we are going to create a class for ListBox and ComboBox items.

public class ComboBoxItem
{
    public bool Rgb { get; set; }

    public override string ToString()
    {
        // Text to show in ComboBox
        return this.Rgb ? "RGB" : "HTML";
    }
}

public class ListBoxItem
{
    public bool Rgb { get; set; }

    public Color Color { get; set; }

    public override string ToString()
    {
        // Text to show in ListBox
        return this.Rgb ?
            $"{this.Color.R},{this.Color.G},{this.Color.B}" :
            $"{this.Color.R:X2}{this.Color.G:X2}{this.Color.B:X2}";
    }
}

Each item in these controls will be an object of these classes. Add the items to the ComboBox:

this.comboBox1.Items.Add(new ComboBoxItem { Rgb = true });
this.comboBox1.Items.Add(new ComboBoxItem { Rgb = false });

When you need add items to ListBox:

this.listBox1.Items.Add(
    new ListBoxItem { Rgb = true, Color = Color.Red });

Now, when you change between RGB and HTML in the ComboBox, you get the selected ComboBox item to know when show colors in RGB or in HTML. Then, iterate all ListBox items and set the RGB with this value. In this form, ListBox items will draw text in that mode. The ListBox doesn't know that your items has changes: you must force to redraw the items.

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var comboBoxItem = (ComboBoxItem)this.comboBox1.SelectedItem;
    for (int i = 0; i < this.listBox1.Items.Count; i  )
    {
        var item = (ListBoxItem)this.listBox1.Items[i];
        item.Rgb = comboBoxItem.Rgb;

        // To force redraw
        listBox1.Items[i] = listBox1.Items[i];
    }
}
  • Related