简体   繁体   中英

Mouse Click Event in Timer in c#

I'm having trouble with mouse click event in a timer in c#.

My timer's interval is 100 and set on True, i want to make action each 100 ticks and indentify the type of click in. I want to play the action each 100 ticks when a mouse is pressed, but this only plays one time. EDIT: I don't want to have to enable/disable the time.

   private void timer1_Tick(object sender, MouseEventArgs e)
{
    if (MouseButtons == MouseButtons.Left)
    { 
       //Action...
    }

    if (MouseButtons == MouseButtons.Right)
    {  
        //Action...
    }
}

If you are really really lazy and don't want to read the answer in my comment or redo the logic, you can perhaps modify your code like this (if this was your intent all along):

private void timer1_Tick(object sender, EventArgs e)
{
    if (MouseButtons == MouseButtons.Left)
    { 
       //Action...
    }

    if (MouseButtons == MouseButtons.Right)
    {  
        //Action...
    }
}

MouseButtons as in Control.MouseButtons

This is the method that is used to get the mouse button state within a timer_tick, with no need for a mouse click event, as per your request.

UPDATE Could you please mention what kind of timer are you using? They all have different behaviours and gotchas. System.Windows.Forms.Timer, System.Timers.Timer and System.Threading.Timer.

I ask because sometimes there is an AutoReset property that you should set to true if you want more than one timer_tick to occur. I could be wrong, but this sounds like what you are describing, so it's worth a shot!

@Soenhay: I suspect that OP wanted to "loop and execute actions every t ticks" WHILE the mouse is held down. afaik MouseClick triggers after MouseUp. You could modify the code to use MouseButtons (the static WinForms oddity) to check the state of the buttons.

@OP: Without you posting additional code, there is no way I see for anyone to help you further other than taking stabs in the dark with random code. Right now you have at least 3 examples similar to what you need, new knowledge about the static MouseButtons class, so I think you can and should take it from here or else you learn nothing!

100 ticks is 0.01 milliseconds and Interval is an integer so I used seconds for this test.

This webform will detect the last clicked mouse button and change the timer interval in the timer tick event based on the mouse button that was last clicked. I also added a label for visual indication:

public partial class Form1 : Form
{
    Timer t;
    MouseButtons lastMouseButtonClicked;
    Label lblStatus;

    public Form1()
    {
        InitializeComponent();

        lblStatus = new Label()
        {
            Text = "No click since tick."
            ,Width = 500
        };
        this.Controls.Add(lblStatus);

        t = new Timer();
        //A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.
        //t.Interval = (int)(100 / TimeSpan.TicksPerMillisecond);//0.01 ms
        t.Interval = 1000;
        t.Tick += T_Tick;
        t.Enabled = true;

        this.MouseClick += Form1_MouseClick;
    }

    private void T_Tick(object sender, EventArgs e)
    {
        switch (lastMouseButtonClicked)
        {
            case MouseButtons.Left:
                //Action...
                lblStatus.Text = "MouseButtons.Left";
                t.Interval = 1000;
                break;
            case MouseButtons.Right:
                //Action...
                lblStatus.Text = "MouseButtons.Right";
                t.Interval = 3000;
                break;
            default:
                lblStatus.Text = "No click since tick.";
                break;
        }
        LastMouseButtonClicked = MouseButtons.None;
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        lastMouseButtonClicked = e.Button;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

The timer interval is changed to either 1s or 3s depending on the mouse click. You could also change the interval in the mouse click event to simplify it.

Here is an example:

public partial class Form1 : Form
{
    Timer t;
    MouseButtons LastMouseButtonClicked;
    Label lblStatus;
    DateTime previousTick;
    TimeSpan elapsed;

    public Form1()
    {
        InitializeComponent();

        lblStatus = new Label()
        {
            Text = "No click since tick."
            ,
            Width = 1000
        };
        this.Controls.Add(lblStatus);

        t = new Timer();
        //A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.
        //t.Interval = (int)(100 / TimeSpan.TicksPerMillisecond);//0.01 ms
        t.Interval = 1000;
        t.Tick += T_Tick;
        t.Enabled = true;

        this.MouseClick += Form1_MouseClick;

        elapsed = TimeSpan.Zero;
    }

    private void T_Tick(object sender, EventArgs e)
    {
        if (elapsed == TimeSpan.Zero)
        {
            elapsed += new TimeSpan(0, 0, 0, 0, 1);
        }
        else
        {
            elapsed += DateTime.Now - previousTick;
        }

        switch (LastMouseButtonClicked)
        {
            case MouseButtons.Left:
                //Action...
                lblStatus.Text = "MouseButtons.Left " + elapsed.Seconds;
                break;
            case MouseButtons.Right:
                //Action...
                lblStatus.Text = "MouseButtons.Right " + elapsed.Seconds;
                break;
            default:
                lblStatus.Text = "No click since tick. " + elapsed.Seconds;
                break;
        }

        previousTick = DateTime.Now;
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        switch (e.Button)
        {
            case MouseButtons.Left:
                LastMouseButtonClicked = e.Button;
                t.Interval = 1000;
                break;
            case MouseButtons.Right:
                LastMouseButtonClicked = e.Button;
                t.Interval = 3000;
                break;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM