- 題名: ボタンを長押ししている間カウントする
- 日時: 2012/04/09 13:16:32
- ID: 30254
- この記事の返信元:
- (なし)
- この記事への返信:
- [30255] Re[1]: ボタンを長押ししている間カウントする2012/04/09 13:20:30
- ツリーを表示
■No30255に追記(魔界の仮面弁士の記事)
> 下記は VB6 の例ですが、C# でも同じようにできるはず…。
> http://rucio.cloudapp.net/ThreadDetail.aspx?ThreadId=10296
C# 版。
public partial class Form1 : Form
{
private int incrementValue = 10;
public Form1()
{
InitializeComponent();
label1.Text = "10";
timer1.Interval = 800;
button1.Text = "↑";
button2.Text = "↓";
//
// イベントの割り当てを、デザイン時ではなく
// 実行時に行う場合は下記のコメントを解除する
//
//button1.MouseDown += ButtonDown;
//button2.MouseDown += ButtonDown;
//button1.MouseUp += ButtonUp;
//button2.MouseUp += ButtonUp;
//timer1.Tick += timer1_Tick;
}
int counter = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (incrementValue == 0)
{
counter = 0;
timer1.Stop();
return;
}
counter++;
if (counter < 10)
{
timer1.Interval = 200;
}
else
{
counter = 10;
timer1.Interval = 55;
}
CurrentValue += incrementValue;
}
private void ButtonDown(object sender, MouseEventArgs e)
{
CurrentValue += (sender == button1) ? 1 : -1;
timer1.Interval = 800;
timer1.Start();
incrementValue = (sender == button1) ? 10 : -10;
}
private void ButtonUp(object sender, MouseEventArgs e)
{
incrementValue = 0;
}
public int CurrentValue
{
set
{
label1.Text = value.ToString();
}
get
{
int ret;
int.TryParse(label1.Text, out ret);
return ret;
}
}
}
分類:[.NET]
2012/04/09(Mon) 13:17:20 編集(投稿者)
矢印ボタンがあります。
【↑】【↓】の二種類です。
Labelには【10】が設定されています。
矢印ボタンを長押しをした場合に
Labelの値を高速にUp / Downしたいです。
MouseUp、MouseDownイベントを使い、
Threadクラスなどを使えば出来るかと思ったのですが、
Threadの中で
this.label1.Text = count.ToString();
などは出来ないというエラーが出てしまいます。
どのようにしたらいいでしょうか?
よろしくお願いします。