Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Why the event listBox1_DrawItem is working only sometimes in some cases ?

$
0
0

In the designer I have set the listBox1 DrawMode property to: OwnerDrawVariable

Then in the Form1 Load event I did:

data.Add("Gpu Temeprature --- " + form1_location_on_x); data

.Add("Cpu Temperature --- " + form1_location_on_y); listBox1.DataSource = data;

Then I have two 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);

            }
        }

I used a breakpoint on the line: ColorText.ColorListBox(data, e); and it's working it's getting to this line.

But I want to show some values that are changing each X seconds.

So I delete the code from the Load event and did:

In the cpuView method in Form1 :

private void cpuView() { if (pauseContinueDoWork == true) { } else { Computer myComputer = new Computer(); myComputer = new Computer(settings) { CPUEnabled =

true }; myComputer.Open(); Trace.WriteLine(""); foreach (var hardwareItem in myComputer.Hardware) { if (hardwareItem.HardwareType ==

HardwareType.CPU) { hardwareItem.Update(); foreach (IHardware subHardware in

hardwareItem.SubHardware) subHardware.Update(); foreach (var sensor in hardwareItem.Sensors) { settings.SetValue("sensor",

sensor.Value.ToString()); if (sensor.SensorType ==

SensorType.Temperature) { sensor.Hardware.Update(); settings.GetValue("sensor",

sensor.Value.ToString()); //label17.Text =

sensor.Value.ToString() + "c";//String.Format("{0} Temperature = {1}c", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value"); if (InvokeRequired)

//this.Invoke(new Action(() => label17.Text = sensor.Value.ToString() + "c"));

data.Add("Gpu Temeprature --- " + sensor.Value.ToString()); else label17.Text =

sensor.Value.ToString() + "c"; tempCpuValue = sensor.Value; if (sensor.Value > 60) {

Logger.Write("The Current CPU Temperature Is ===> " + sensor.Value); button1.Enabled = true; } int t = label17.Text.Length; if (t >= 4) { if (InvokeRequired)

this.Invoke(new Action(() => label17.Location = new Point(50,20))); } else {

this.Invoke(new Action(() => label17.Location = new Point(50, 20))); } break; } } } } } }

I changed in this method:

if (InvokeRequired)
                                    //this.Invoke(new Action(() => label17.Text = sensor.Value.ToString() + "c"));
                                    data.Add("Gpu Temeprature --- " + sensor.Value.ToString());

Then in the background do work event i did:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            while (true)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    if (tempCpuValue >= (float?)nud1.Value || tempGpuValue >= (float?)nud1.Value)
                    {
                        soundPlay = true;
                        blinking_label();
                        NudgeMe();
                    }
                    else
                    {
                        soundPlay = false;
                        stop_alarm = true;

                    }
                    cpuView();
                    gpuView();
                    listBox1.DataSource = data;
                    Thread.Sleep(1000);
                }
            }
        }

In this case the event draw item of the listBox1 is not working.

What I wanted is to update the first item in the listBox with the value sensor.Value

The problems are that each time it's adding a new sensor.Value to data(List<string>) instead updating the item. Second the measure item and draw item events are not working .


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>