13 Replies - 475 Views - Last Post: 21 July 2014 - 10:23 AM Rate Topic: -----

#1 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 85
  • Joined: 28-February 14

Perform left mouse click via coding?

Posted 20 July 2014 - 10:07 PM

Basically, I want my program to have a right click context menu show when you right click, but when the user right clicks on specific buttons, I want it to perform the left click at the current position as well before showing the menu.. This is the coding I've tried...
    Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

    Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            mouse_event(&H2, e.X, e.Y, 0, 1)
            ContextMenuStrip2.Show(CType(sender, Control), e.Location)
        End If
    End Sub
    Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Button1_Click(Button1, New MouseEventArgs(Windows.Forms.MouseButtons.Left, 2, e.X, e.Y, 0))
            ContextMenuStrip2.Show(CType(sender, Control), e.Location)
        End If
    End Sub
Neither of these performs a left click or opens the context menu and when I set the button itself to have a context menu, the button doesn't get the .Click or .MouseClick event.
Is there any way to do this?

Is This A Good Question/Topic? 0
  • +

Replies To: Perform left mouse click via coding?

#2 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 01:31 AM

Can you please simplify what you're trying to do?
So basically you want the context menu to appear on the current pointer location for a right and left click. Am I right?
Also why not just use
 ContextMenuStrip2.Show(Cursor.Position)

for the right and left click events?

This post has been edited by Bluezap: 21 July 2014 - 01:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3526
  • View blog
  • Posts: 12,034
  • Joined: 12-December 12

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 02:53 AM

Button1.PerformClick()

but, as Bluezap indicates, it is better to take the significant code out of the click-event and create it as a separate method that you can call.
Was This Post Helpful? 1
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 05:37 AM

Hi,
Use the MouseDown event instead of the MouseClick event like below. Don`t set the buttons ContextMenu property to the context menu, just leave it unset.

    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown

        MessageBox.Show("The button was clicked so run the code")

        If e.Button = MouseButtons.Right Then
            ContextMenuStrip1.Show(Button1, e.Location)
        End If
    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 Sheepings  Icon User is offline

  • D.I.C Addict

Reputation: 71
  • View blog
  • Posts: 565
  • Joined: 05-December 13

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 07:20 AM

    Private Sub StatusMenuPicBox_Click(sender As Object, e As EventArgs) Handles StatusMenuPicBox.Click 'Opens the status menu
        Dim x As Integer 'x As Location
        Dim y As Integer 'y As Location
        x = Control.MousePosition().X - Me.Location.X - 5  'Adjust Here If You Need
        y = Control.MousePosition().Y - Me.Location.Y - 23 'Adjust Here If You Need
        Dim xy As New Point(x, y)'Position is just to the right of where you click, if i set this right.
        StatusContextMenu.Show(Me, xy)
    End Sub
That will open the context menu where ever your mouse location is when you click on a an object, in my example, mine is a picture box which will open the context menu if clicked. combine that with IronRazer's code and you can have it work with only right click and it will open at the location of your mouse.

Hope it helps.

Further I would like to point out that your topic title wants it to be done under left click. But in your code, you are using
If e.Button = Windows.Forms.MouseButtons.Right Then'
Right click.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3526
  • View blog
  • Posts: 12,034
  • Joined: 12-December 12

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 07:31 AM

Note: I would never expect, or want, right-clicking a button to also execute the left-click action. If I want to carry-out that action I would expect to have to (left) click the button. Think about the user and expected behaviours.
Was This Post Helpful? 1
  • +
  • -

#7 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 07:32 AM

@ Sheepings,
If you use the MouseDown event instead of the Click event you can get the mouse position from the (e.Location) or (e.x and e.y) that is passed in the MouseEventArgs of the MouseDown event. It also provides the (e.Buttons) that you can check to see what mouse button raised the event. Then you don`t need all the calculations to get the MousePosition. :)
Was This Post Helpful? 0
  • +
  • -

#8 Sheepings  Icon User is offline

  • D.I.C Addict

Reputation: 71
  • View blog
  • Posts: 565
  • Joined: 05-December 13

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 07:47 AM

IronRazer. Correct you are. And a quick glance at the documentation... Good info covers this over here.
Was This Post Helpful? 1
  • +
  • -

#9 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 85
  • Joined: 28-February 14

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 08:55 AM

I'll expand on why I want it to also trigger the button's left click function.

I have 50 buttons, Button1-Button50. When left clicked, they will load specific data that I want the user to see when they right click so they know for sure what data they would be modifying with the context menu.

Here's the button 1 example.

'The cModel, etc. are TextBoxes used to show the corresponding information.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        cModel.Text = Model1
        cYear.Text = Year1
        cRating.Text = Rating1
        cCost.Text = Cost1
    End Sub
If the user doesn't left click before right clicking, they are unable to see this information. I would like the right click to perform the left click so the program loads the data to the corresponding button without having to left click first.

The reason I don't want to use ".PerformClick()" is it will require more coding than I thought would be necessary. I've used this coding to accurately get a context menu to show, but I still don't know how to perform left click via coding, which is what I'd like to do.
Public Sub Label1_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.Click
        If e.Button = Windows.Forms.MouseButtons.Right Then
            ContextMenuStrip1.Show(CType(sender, Control), e.Location)
        End If
    End Sub
The above code shows a context menu, however, it doesn't work on buttons. I don't know why. The below code is what I tried for the button.
Public Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            ContextMenuStrip2.Show(CType(sender, Control), e.Location)
        End If
    End Sub


Edit: I changed the "Button1.MouseClick" to "Button1.MouseDown" so it will now show the context menu with the button.. But I don't know how to get that left click code.


Edit AGAIN: I got button1 to click.. Is there any way to make it work for all 50 buttons by tweaking the code to work for them all, or will I have to copy it and adjust it for each individual button?
    Public Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Button1_Click(Button1, New MouseEventArgs(Windows.Forms.MouseButtons.Left, 2, e.X, e.Y, 0))
            ContextMenuStrip2.Show(CType(sender, Control), e.Location)
        End If
    End Sub

This post has been edited by ZakaryHansen: 21 July 2014 - 09:11 AM

Was This Post Helpful? 0
  • +
  • -

#10 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 09:09 AM

Hi,
Maybe i am not understanding the full effect of what you want but, will this work for you.

    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        cModel.Text = Model1
        cYear.Text = Year1
        cRating.Text = Rating1
        cCost.Text = Cost1
        If e.Button = MouseButtons.Right Then
            ContextMenuStrip1.Show(Button1, e.Location)
        End If
    End Sub


This post has been edited by IronRazer: 21 July 2014 - 09:10 AM

Was This Post Helpful? 1
  • +
  • -

#11 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 85
  • Joined: 28-February 14

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 09:42 AM

@IronRazer, that does work. I'd tried that in the beginning, however the buttons don't work well with .MouseClick, which is what I had used. The only problem with that is, in case the user acts like me and uses the tabstop and whatnot, space to click selected, etc., it won't have a .Click and I can't set it to handle .Click 'cause they don't have a compatible signature.

I did get it to work, though.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        cModel.Text = Model1
        cYear.Text = Year1
        cRating.Text = Rating1
        cCost.Text = Cost1
    End Sub
Public Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Button1.PerformClick()
            ContextMenuStrip2.Show(CType(sender, Control), e.Location)
        End If
    End Sub

It's more coding than I would've liked, but it's all good. The above coding is what I'm using and that's duplicated and modified for each individual button.

It would've worked from the beginning if I had known to use MouseDown and not MouseClick.

This post has been edited by ZakaryHansen: 21 July 2014 - 09:43 AM

Was This Post Helpful? 1
  • +
  • -

#12 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 09:53 AM

Oh.. i see. I didn`t think about the user using the keyboard to tab and click the buttons. If you wanted to cut the code down it can be done. It would depend on if you want all the buttons or just a few buttons to act this way and if they are all directly on the form or if they are in a panel or groupbox or something.
Was This Post Helpful? 0
  • +
  • -

#13 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 85
  • Joined: 28-February 14

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 10:19 AM

I'm sure it could be cut down a bit, but it works as is.

I appreciate everyone's help.
Was This Post Helpful? 0
  • +
  • -

#14 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Perform left mouse click via coding?

Posted 21 July 2014 - 10:23 AM

Your Welcome. If at some point you would like to shorten the code then i just ask. Have fun. B)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1