12 Replies - 20969 Views - Last Post: 13 February 2018 - 11:01 AM Rate Topic: -----

#1 n0c0d3   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-December 08

Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 01:54 PM

Hi all,

I have a NotifyIcon (named TrayIcon). When I right-click it I get a menu. When I left-click it I want the form to be shown when it's minimized or hidden. But I can't find a way to get the latter done.
Private Sub TrayIcon_MouseClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrayIcon.MouseClick
	If e.Equals(MouseButtons.Left) Then
		Me.Show()
	End If
End Sub
 

When I debug, "e" shows "almost" same value ({X = 0 Y = 0 Button = Left{1048576}}) as"MouseButtons.Left" (Left{104875}) in the quick-watch window, still the Me.show is passed. I think this has to do with the X and Y, and "MouseButtos.Left" is of the type "System.Windows.Forms.MouseButtons" and "e" is of the type "System.EventArgs", but I have no idea how to get the right comparison.
It's partially working though. When I leave out the if statement and only the Me.Show() is left, both right and left-click make the form pop-up. With the if statement, it never shows up.

Any suggestions how to get the left-click working?

Thanks,
Bart

This post has been edited by n0c0d3: 20 December 2009 - 02:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Capturing which mouse button is clicked on a NotifyIcon

#2 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 02:04 PM

View Postn0c0d3, on 20 Dec, 2009 - 12:54 PM, said:

Hi all,

I have a NotifyIcon (named TrayIcon). When I right-click it I get a menu. When I left-click it I want the form to be shown when it's minimized or hidden. But I can't find a way to get the latter done.
Private Sub TrayIcon_MouseClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrayIcon.MouseClick
	If e.Equals(MouseButtons.Left) Then
		Me.Show()
	End If
End Sub
 



Thanks,
Bart

well, you dont need the
 
if e.equals(mousebuttons.left) then
end if


try:
		If Me.WindowState = FormWindowState.Minimized Then
			Me.WindowState = FormWindowState.Normal
		End If

and why do you want to use the "me.hide"?
why dont just minimize and then
 Me.ShowIcon = False 
:)
Was This Post Helpful? 0
  • +
  • -

#3 n0c0d3   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-December 08

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 02:18 PM

Hi 'EndLessMind

Thanks for your reply, but this won't help me.

Quote

well, you dont need the
 
if e.equals(mousebuttons.left) then
end if


try:
If Me.WindowState = FormWindowState.Minimized Then
	Me.WindowState = FormWindowState.Normal
End If

I think you didn't understand what I actually meant. On right-click on the try-icon I want the menu to show up, and on left click I want the form to pop-up, which works fine as long as there can be distinguished between right and left-click, (or hide in the tray, but when I get the "show"part done, I can get the "hide"part done as well I suppose). And the "Me.Show()" works fine, so why replace it by 2 lines of code?

Quote

and why do you want to use the "me.hide"?
why dont just minimize and then
 Me.ShowIcon = False 

Because I don't want to hide or show the icon, I want to show or hide the form.

This post has been edited by n0c0d3: 20 December 2009 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 02:20 PM

Okey. so your question is " how do i get a menu when i right-click on the notifyicon? " ? and " how do i make the form hide" ?
Was This Post Helpful? 0
  • +
  • -

#5 n0c0d3   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-December 08

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 02:26 PM

View PostEndLessMind, on 20 Dec, 2009 - 09:20 PM, said:

Okey. so your question is " how do i get a menu when i right-click on the notifyicon? " ? and " how do i make the form hide" ?

Not exactly. The menu is no problem, neither to make he form show. The left-click is the problem. The question is "how to determine if the left-button is clicked on the NotifyIcon?"

This post has been edited by n0c0d3: 20 December 2009 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#6 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 02:33 PM

View Postn0c0d3, on 20 Dec, 2009 - 01:26 PM, said:

View PostEndLessMind, on 20 Dec, 2009 - 09:20 PM, said:

Okey. so your question is " how do i get a menu when i right-click on the notifyicon? " ? and " how do i make the form hide" ?

Not exactly. The menu is no problem. The left-click is the problem. The question is "how to determine if the left-button is clicked on the NotifyIcon?"

sorry. but the MouseClick event is for the left mouse button ^^ and if you dubble click on the NotifyIcon in for form in vb .net then you can selec event like "MouseClick" and "MouseDubbleClick"
I hope i understod you right:P

View Postn0c0d3, on 20 Dec, 2009 - 01:26 PM, said:

View PostEndLessMind, on 20 Dec, 2009 - 09:20 PM, said:

Okey. so your question is " how do i get a menu when i right-click on the notifyicon? " ? and " how do i make the form hide" ?

Not exactly. The menu is no problem. The left-click is the problem. The question is "how to determine if the left-button is clicked on the NotifyIcon?"

sorry. but the MouseClick event is for the left mouse button ^^ and if you dubble click on the NotifyIcon in for form in vb .net then you can selec event like "MouseClick" and "MouseDubbleClick"
I hope i understod you right:P
Was This Post Helpful? 0
  • +
  • -

#7 n0c0d3   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-December 08

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 02:44 PM

I know about the Click and DoubleClick events of the NotifyIcon. I want to refine that further to "which button was clicked on the click-event". That's what's the "e.Equals(MouseButtons.Left)" is supposed to do, but it won't do the trick.
My question is: "What should I do to get the NotifyIcon respond differently on left and right-clicks?"
I really don't know how to make the question more clear.
Was This Post Helpful? 0
  • +
  • -

#8 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 02:58 PM

View Postn0c0d3, on 20 Dec, 2009 - 01:44 PM, said:

I know about the Click and DoubleClick events of the NotifyIcon. I want to refine that further to "which button was clicked on the click-event". That's what's the "e.Equals(MouseButtons.Left)" is supposed to do, but it won't do the trick.
My question is: "What should I do to get the NotifyIcon respond differently on left and right-clicks?"
I really don't know how to make the question more clear.

but i allready responde diffrently on right and left. at least if you have a contextmenu selected.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 03:06 PM

The event you need to use is MouseDown, where the e has a property from which you can tell which mouse button pressed.
Was This Post Helpful? 1
  • +
  • -

#10 n0c0d3   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-December 08

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 03:48 PM

View PostAdamSpeight2008, on 20 Dec, 2009 - 10:06 PM, said:

The event you need to use is MouseDown, where the e has a property from which you can tell which mouse button pressed.

Thanks Adam, this did the trick. Now I have this code which works as I was looking for:
Private Sub TrayIcon_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrayIcon.MouseDown
	If Me.Visible = False Then
		If e.Button = MouseButtons.Left Then Me.Show()
	Else
		If e.Button = MouseButtons.Left Then Me.Hide()
	End If
End Sub



And to EndLessMind, I'm sorry, but I don't see where your suggestion distinguishes between left- and right-click. I already had a menu, even with "show" and "hide to tray" options that both already worked fine. But thanks for your efforts anyway. I do appreciate it.

Thanks guys,
Bart

This post has been edited by n0c0d3: 20 December 2009 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#11 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 20 December 2009 - 05:20 PM

oh.. i thought that you hade a contextmenu with aoptions like "hide" and when it would open when you left-click. Sorry ^^
Was This Post Helpful? 0
  • +
  • -

#12 danmian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-December 12

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 24 December 2012 - 08:17 AM

Hi.

For those who are still looking for an answer to this topic, i use this:

    Private Sub NotifyIcon1_Click(sender As Object, e As System.EventArgs) Handles NotifyIcon1.Click
        Dim ElBoton As System.Windows.Forms.MouseEventArgs = e
        If ElBoton.Button = MouseButtons.Left Then
            Me.WindowState = FormWindowState.Normal
        End If
    End Sub



Regards!
Was This Post Helpful? 0
  • +
  • -

#13 Puffgroovy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-February 18

Re: Capturing which mouse button is clicked on a NotifyIcon

Posted 13 February 2018 - 11:01 AM

View Postn0c0d3, on 20 December 2009 - 01:54 PM, said:

Hi all,

I have a NotifyIcon (named TrayIcon). When I right-click it I get a menu. When I left-click it I want the form to be shown when it's minimized or hidden. But I can't find a way to get the latter done.
Private Sub TrayIcon_MouseClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrayIcon.MouseClick
	If e.Equals(MouseButtons.Left) Then
		Me.Show()
	End If
End Sub
 

When I debug, "e" shows "almost" same value ({X = 0 Y = 0 Button = Left{1048576}}) as"MouseButtons.Left" (Left{104875}) in the quick-watch window, still the Me.show is passed. I think this has to do with the X and Y, and "MouseButtos.Left" is of the type "System.Windows.Forms.MouseButtons" and "e" is of the type "System.EventArgs", but I have no idea how to get the right comparison.
It's partially working though. When I leave out the if statement and only the Me.Show() is left, both right and left-click make the form pop-up. With the if statement, it never shows up.

Any suggestions how to get the left-click working?

Thanks,
Bart




Here is what I use with C#.
private void notifyIconReferenceTableBuilder_Click(object sender, EventArgs e)
{
    // Box the EventArgs "e" variable into a MouseEventArgs object. 
    var MouseEventArgs = (MouseEventArgs) e;

    // If it is the right mouse button, do whatever you need to do
    if (MouseEventArgs.Button == MouseButtons.Right) 
	{
	   // Do whatever you want here. You have detected the right mouse click.
	}
}



This post has been edited by modi123_1: 13 February 2018 - 11:03 AM
Reason for edit:: fixed botched quotes

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1