2016-07-13 5 views
0

私はWindowsフォームアプリケーションを開発しています。一部のサービスのUIを作成する。何が起こっているのですか?私がアプリケーションを作成したときに、値を取得して設定するために1回のクリックが機能するはずのさまざまなボタンがありますが、コードの完了後にはすべてのボタンがダブルクリックで反応します。 いくつかのブレークポイントとテストを適用すると、値はシングルクリックで取得および設定されますが、実行時にはダブルクリックが必要です。誰でも助けることができますか?C#。私はシングルクリックが必要なときにダブルクリックを避ける方法

+1

そのような反応を示すサンプルコードを含めてください。 –

+0

申し訳ありませんが、コード全体が含まれています。私がこのコードを完成したら、それはダブルクリックで反応し始めました。 CANバスの値を取得して設定しています。 最初に信号値を取得して設定する必要があるときに、値がdouble型であればすべてが正しく行われました。しかし、値のタイプがオブジェクトタイプに変更されたとき、ダブルクリックでレコーディングを開始しました。 – AutoEng

+0

一部のスニペットはあなたの言葉よりも多くを話すことができます。それは私たちのためにもっと明確になります –

答えて

0

私はコードを書くことで解決策を得ました。 最初のクリックでボタンをアクティブにしました。

bool firstClick = true; {if(firstClick){button.select(); //ボタンを有効にする}}

ご回答いただきありがとうございます。

0

MSDNのArticleは、シングルクリックとダブルクリックの使い方に関する完全な情報です。また、イベントを処理してシングルクリックとダブルクリックを区別する方法も説明しています。

この記事では、これを行うためにブール値とタイマーを使用しています。複数のボタンがある場合は、Dictionary<Button, boolean>を使用する必要があります。それが役に立てば幸い。リンクがダウンし得、またはいくつかのreaosnために削除した場合ここ

は一例であり:

MouseDownイベントを処理し、タイマ構成要素を適切なSystemInformationプロパティを使用して クリック間の位置と時間スパン を決定します。 のクリックまたはダブルクリックの有無に応じて適切な処置を実行します。次のコード例 は、これを実行する方法を示しています。

class Form1 : Form 
{ 
    private Rectangle hitTestRectangle = new Rectangle(); 
    private Rectangle doubleClickRectangle = new Rectangle(); 
    private TextBox textBox1 = new TextBox(); 
    private Timer doubleClickTimer = new Timer(); 
    private ProgressBar doubleClickBar = new ProgressBar(); 
    private Label label1 = new Label(); 
    private Label label2 = new Label(); 
    private bool isFirstClick = true; 
    private bool isDoubleClick = false; 
    private int milliseconds = 0; 

    [STAThread] 
    public static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.Run(new Form1()); 
    } 

    public Form1() 
    { 
     label1.Location = new Point(30, 5); 
     label1.Size = new Size(100, 15); 
     label1.Text = "Hit test rectangle:"; 

     label2.Location = new Point(30, 70); 
     label2.Size = new Size(100, 15); 
     label2.Text = "Double click timer:"; 

     hitTestRectangle.Location = new Point(30, 20); 
     hitTestRectangle.Size = new Size(100, 40); 

     doubleClickTimer.Interval = 100; 
     doubleClickTimer.Tick += 
      new EventHandler(doubleClickTimer_Tick); 

     doubleClickBar.Location = new Point(30, 85); 
     doubleClickBar.Minimum = 0; 
     doubleClickBar.Maximum = SystemInformation.DoubleClickTime; 

     textBox1.Location = new Point(30, 120); 
     textBox1.Size = new Size(200, 100); 
     textBox1.AutoSize = false; 
     textBox1.Multiline = true; 

     this.Paint += new PaintEventHandler(Form1_Paint); 
     this.MouseDown += new MouseEventHandler(Form1_MouseDown); 
     this.Controls.AddRange(new Control[] { doubleClickBar, textBox1, 
      label1, label2 }); 
    } 

    // Detect a valid single click or double click. 
    void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     // Verify that the mouse click is in the main hit 
     // test rectangle. 
     if (!hitTestRectangle.Contains(e.Location)) 
     { 
      return; 
     } 

     // This is the first mouse click. 
     if (isFirstClick) 
     { 
      isFirstClick = false; 

      // Determine the location and size of the double click 
      // rectangle area to draw around the cursor point. 
      doubleClickRectangle = new Rectangle(
       e.X - (SystemInformation.DoubleClickSize.Width/2), 
       e.Y - (SystemInformation.DoubleClickSize.Height/2), 
       SystemInformation.DoubleClickSize.Width, 
       SystemInformation.DoubleClickSize.Height); 
      Invalidate(); 

      // Start the double click timer. 
      doubleClickTimer.Start(); 
     } 

     // This is the second mouse click. 
     else 
     { 
      // Verify that the mouse click is within the double click 
      // rectangle and is within the system-defined double 
      // click period. 
      if (doubleClickRectangle.Contains(e.Location) && 
       milliseconds < SystemInformation.DoubleClickTime) 
      { 
       isDoubleClick = true; 
      } 
     } 
    } 

    void doubleClickTimer_Tick(object sender, EventArgs e) 
    { 
     milliseconds += 100; 
     doubleClickBar.Increment(100); 

     // The timer has reached the double click time limit. 
     if (milliseconds >= SystemInformation.DoubleClickTime) 
     { 
      doubleClickTimer.Stop(); 

      if (isDoubleClick) 
      { 
       textBox1.AppendText("Perform double click action"); 
       textBox1.AppendText(Environment.NewLine); 
      } 
      else 
      { 
       textBox1.AppendText("Perform single click action"); 
       textBox1.AppendText(Environment.NewLine); 
      } 

      // Allow the MouseDown event handler to process clicks again. 
      isFirstClick = true; 
      isDoubleClick = false; 
      milliseconds = 0; 
      doubleClickBar.Value = 0; 
     } 
    } 

    // Paint the hit test and double click rectangles. 
    void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     // Draw the border of the main hit test rectangle. 
     e.Graphics.DrawRectangle(Pens.Black, hitTestRectangle); 

     // Fill in the double click rectangle. 
     e.Graphics.FillRectangle(Brushes.Blue, doubleClickRectangle); 
    } 
} 
0

あなたは、単にタイマーを使用してシングルクリックとダブルを識別することができます。ここに例があります

class Form1 : Form 
{ 
     Timer timer; 

     public Form1() 
     { 
      InitializeComponent(); 
      timer = new Timer(); 
      timer.Interval = 500; 
      timer.Tick += new EventHandler(Timer_Tick); 
     } 
     private void App_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Clicks == 1) 
      { 
       timer.Start(); 
      } 
      else 
      { 
       timer.Stop(); 
       doubleClick(); 
      } 
     } 

     private void Timer_Tick(object sender, EventArgs e) 
     { 
      timer.Stop(); 
      singleClick(); 
     } 

     //Single click 
     public void singleClick() 
     { 
      MessageBox.Show("Single Click."); 
     } 

     //Double click 
     public void doubleClick() 
     { 
      MessageBox.Show("Double Click."); 
     } 
} 
+0

もう一つ。私はそれがダブルクリックでのみ動作することを正確にはわかりません。 私たちが一度クリックして、2秒と言い、もう一度クリックすると時間がかかるのが好きです。 フォームの読み込みと同様の問題が発生する可能性があります。 解決していただきありがとうございます。これらを試してみましょう。まだこのコメントを考慮に入れています。 – AutoEng

関連する問題