Reputation: 11
i'm trying to update/paint a listbox in real time, but i'm having some problems. I got a button to start the process of populating the listbox button_traceroute_Click.
My problem is that the listbox is only painted/updated when the whole process (button click) has ended, i wanted the the items to be inserted(viewed) one by one. I already tried using ListBox.Update() but nothing happened. (this is a traceroute)
private void button_traceroute_Click(object sender, EventArgs e)
{
String target;
int i = 0;
target = textBox_target.Text;
Mydata data = new Mydata();
TraceRoute traceroute = new TraceRoute();
while (i < 50 && !data.getReached() && !data.getError()) // i = hop count
{
data = traceroute.startTrace(target, data, i);
listBox_trace.Items.Add(data.getOutput());
i++;
}
}
data.getOutput() returns (string) something like: "Hop X: 165.468.354.4 -> 50 ms" or "Hop X: Timeout"
Mydata{
bool Finish flag;
bool Error flag;
int badcounter;
String output;
}
For now im populating the listbox with Strings, but the objective is to use a object.
Upvotes: 1
Views: 449
Reputation: 46425
Try this:
data = traceroute.startTrace(target, data, i);
listBox_trace.Items.Add(data.getOutput());
Application.DoEvents();
i++;
Its not ideal - Michael G's answer is best, but this could work as a quick fix.
Upvotes: 0
Reputation: 34592
Also, you can use the BeginUpdate and EndUpdate method to speed up the repainting of the listbox. When BeginUpdate is called, any pending paint to the listbox is suspended, likewise, EndUpdate resumes the painting, this can help making your listbox look as if it is fast in loading the data into it and minimizes the number of painting to it when adding the data.
Hope this helps, Best regards, Tom.
Upvotes: 0