user2065612
user2065612

Reputation: 471

How do i add to the listBox another item?

I have this code in a method im calling the method in a timer every second:

data = new List<string>();
data.Add("Gpu Temeprature --- " + sensor.Value.ToString());
listBox1.DataSource = null;
listBox1.DataSource = data;
listBox1.Invalidate();

Its updating a value in the listBox every second in the first place as an item. Now i want to add in the second line a new item so they both will be updated every second so in the timer event im calling another method wich have this code inside:

// data = new List<string>();
data.Add("Cpu Temeprature --- " + sensor.Value.ToString());
listBox1.DataSource = null;
listBox1.DataSource = data;
listBox1.Invalidate();

If i will use the data = new List(); in the second method it will put both items in the same place in the listBox. If im not using this data = new List(); then the second one will blink all the time each second will blink and it's not working looking good.

This is the timer event:

private void timer2_Tick(object sender, EventArgs e)
{
    if (tempCpuValue >= (float?)nud1.Value || tempGpuValue >= (float?)nud1.Value)
    {
        soundPlay = true;
        blinking_label();
        NudgeMe();
    }
    else
    {
        soundPlay = false;
        stop_alarm = true;

    }
    cpuView();
    gpuView();
}   

cpuView() and gpuView() are the two method im calling in the timer wich updating the listBox.

And i have another two listBox events:

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = 25;
}    


private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index == -1)
    {
    }
    else
    {
        ColorText.ColorListBox(data, e);
    }
}

ColorText is in a new class where im cloring the items in the listBox.

Upvotes: 2

Views: 242

Answers (1)

Snukus
Snukus

Reputation: 1302

If you want to continue doing it this way you can do the following:

Try moving the data initialization, the datasource, and the invalidation outside of those function blocks as follows:

private void timer2_Tick(object sender, EventArgs e)
        {
            if (tempCpuValue >= (float?)nud1.Value || tempGpuValue >= (float?)nud1.Value)
            {
                soundPlay = true;
                blinking_label();
                NudgeMe();
            }
            else
            {
                soundPlay = false;
                stop_alarm = true;

            }
            data = new List<string>();
            cpuView();
            gpuView();
            listBox1.DataSource = data;
            listBox1.Invalidate();
        } 

And remove them from the functions so you don't keep doing it twice.


This can probably be done more efficiently using BindingList. Change Data to: BindingList Data = new BindingList();

Then you never need to create a new list. Add the two values you want into this bindinglist and then set the listbox.DataSource = the bindinglist. Then in your GPU/CPU functions you just need to update the index of the list IE:

data[0] = "Cpu Temeprature --- " + sensor.Value.ToString());

and the bindinglist should handle updating the listbox.

Upvotes: 2

Related Questions