1、我的程式listbox每秒刷新一次,拖动scrollbar后,刷新后scrollbar会自动回到顶端,怎样才能在刷新后使scrollbar在原来的位置,而不回到listbox顶端?
2、怎样才能在刷新时,listbox不会有明显的抖动
附上源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.Threading;
namespace ProcessTest
{
public partial class Form1 : Form
{
private ArrayList alist;
private Process[] process;
private Thread thread;
private delegate void clear_datasource();
private delegate void get_datasource();
public Form1()
{
InitializeComponent();
}
private void show_process()
{
process = Process.GetProcesses();
string process_name;
this.Invoke(new clear_datasource(clear_list));
alist.Clear();
foreach (Process t in process)
{
process_name = t.ProcessName;
alist.Add(process_name);
}
this.Invoke(new get_datasource(get_list));
Thread.Sleep(1000);
show_process();
}
private void get_list()
{
listBox1.DataSource = alist;
}
private void clear_list()
{
listBox1.DataSource = null;
}
private void Form1_Load(object sender, EventArgs e)
{
alist = new ArrayList();
thread = new Thread(new ThreadStart(show_process));
thread.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
thread.Abort();
}
}
}
2、怎样才能在刷新时,listbox不会有明显的抖动
附上源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.Threading;
namespace ProcessTest
{
public partial class Form1 : Form
{
private ArrayList alist;
private Process[] process;
private Thread thread;
private delegate void clear_datasource();
private delegate void get_datasource();
public Form1()
{
InitializeComponent();
}
private void show_process()
{
process = Process.GetProcesses();
string process_name;
this.Invoke(new clear_datasource(clear_list));
alist.Clear();
foreach (Process t in process)
{
process_name = t.ProcessName;
alist.Add(process_name);
}
this.Invoke(new get_datasource(get_list));
Thread.Sleep(1000);
show_process();
}
private void get_list()
{
listBox1.DataSource = alist;
}
private void clear_list()
{
listBox1.DataSource = null;
}
private void Form1_Load(object sender, EventArgs e)
{
alist = new ArrayList();
thread = new Thread(new ThreadStart(show_process));
thread.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
thread.Abort();
}
}
}
scrollbar可以从ListBox.Controls里面挖出来
ListBox.BeginUpdate()
ListBox.EndUpdate()