MouseWheel イベントでのプライベート メソッドの呼び出しに問題があります。実際、変数をインクリメントしたり、タイトルバーなどに何かを表示したりするだけで、マウスホイールイベントが適切に発生します。しかし、プライベートメソッドを呼び出したい場合、そのメソッドは1回だけ呼び出されます。スクロールの速度に応じたメソッド、つまり、スクロールが1回行われるときはプライベートメソッドを1回ゆっくりと呼び出しますが、スクロールが高速で行われるときは、スクロール速度に応じてプライベートメソッドを複数回呼び出します。
詳細については、i の値をタイトル バーに表示し、スクロール速度に応じて適切に Listbox コントロールに追加するサンプル コードを配置していますが、スクロール速度に応じてプライベート メソッドを複数回呼び出したい場合は、そのメソッドは 1 回だけ呼び出されます。
public partial class Form1 : Form
{
ListBox listBox1 = new ListBox();
int i = 0;
public Form1()
{
InitializeComponent();
// Settnig ListBox control properties
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(13, 13);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(259, 264);
this.listBox1.TabIndex = 0;
// Attaching Mouse Wheel Event
this.listBox1.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
// Adding Control
this.Controls.Add(this.listBox1);
}
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
i++;
this.Text = i.ToString();
this.listBox1.Items.Add(i.ToString());
// Uncomment the following line to call the private method
// this method gets called only one time irrelevant of the
// mouse wheel scroll speed.
// this.LaunchThisEvent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.listBox1.Select();
}
private void LaunchThisEvent()
{
// Display message each time
// this method gets called.
MessageBox.Show(i.ToString());
}
}
マウスホイールのスクロール速度に応じて、プライベートメソッドを複数回呼び出す方法は?