Jump to content

Recommended Posts

Posted

I recall doing NotifyIcon etc in vb6 but have not done much dot net programming / coding. I have seen the how to on codeproject.org but keep getting errors ref the Tray.Icon is not a member of Resources or something to that effect and some other errors ref creating the controls for menu strip etc

 

Any chance of a working example put together and zipped up as am getting a bit frustrated now.

Posted (edited)

Only quick knocked up, (and 2008 but no real diff) as I don't have 2010 installed at home, but here you go:

 

(I assume your error was the fact you didn't add a NotifyIcon component? or something silly?)

 

WindowsApplication1.zip

 

Click button on form to hide form,

 

then in bar there's little runny man (or should be unless it broke in transit :p I know uncompiled projects don't like changing directories. It's just on my desktop © atm (Even if it breaks could just ninja form code))

 

Click him to reshow form etc etc

 

Any use?

 

Steve

Edited by Steve21
Posted

I was hoping for a system tray icon, right click on system tray icon with 3 options

 

1. Info

2. Log off

3. Exit

 

Click on Info and it opens the form with the username, ip address and stating network connected ( if not connected then ip address is blank or states invalid ip address and the latter option states disconnected )

 

Click on the Info form and it vanishes

 

Click on log off and it prompts if you want to log off and then logs off if yes is selected

 

Exit literally exists the app

 

I can do all the wmi stuff with ref to getting if its connected / disconnected , ip address, username etc but cant do the system tray icon with a popup menu

Posted

   
Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
       'handling click event of the NotifyIcon

       If e.Button = Windows.Forms.MouseButtons.Right Then
           popup.Show(e.Location)
       End If

   End Sub

 

The context menu strip still appears at the very top left of the screen, tried using e.x, e.y to get the screen co-ordinates and also tried another drawing function so it would draw it relevant to the screen co - ordinates etc but the menu still appears at the top left.

 

What control do I need to use to make a popup menu so when I right click on the notifyicon in the system tray it gives me a popup / context menu where I right clicked not miles away from where I am clicking.

Posted

Quick note, Can you do me favour, and just make reply here. So tomorrow it'll popup in newposts for me :p Just incase I forgot, Will upload working example of what you want in morning, when in office.

 

Steve

Posted
I've made a quick vid on how to do it. YouTube - How to add a Tray Icon in VS 2010. It's in C#, but the code is nearly directly comparable. I personally don't like using the context menu strip as it uses it's own graphics, the context menu control is the one that uses the Windows Theme. It's hidden by default.

 

That did not help one bit as I do not know which things are vital in making the popup menu appear on the notify icon you just went through a whole bunch of stuff

 

went to form1 --> context menu --> set to popup ( this is what I named my context menu strip )

 

What does Initalise component do and why do I need that ??

 

I dont want to minimize the form, I want to totally hide it as in visible = false

 

Also how am I meant to close / destroy the notify icon and in which event ? Because when ever I stop the project form running its constantly leaving multiple icons in the system tray

 

Also I am getting an error on GetHostEntry when I use initialise component command ??

 

To get the hostname and ip address, am I better off just using wmi instead of using the inbuilt system.net.dns and gethostentry etc??

Posted
I'm confused as to what the issue here is? To add a notifyIcon and contextMenuStrip to that notifyIcon is super simple. You add the notifyIcon to your form, then add a contextMenuStrip to it too, setting up your menu options. Then simply set the 'ContextMenuStrip' property of the notifyIcon to the contextMenuStrip icon you added.
Posted
That did not help one bit as I do not know which things are vital in making the popup menu appear on the notify icon you just went through a whole bunch of stuff

 

went to form1 --> context menu --> set to popup ( this is what I named my context menu strip )

 

What does Initalise component do and why do I need that ??

 

I dont want to minimize the form, I want to totally hide it as in visible = false

 

Also how am I meant to close / destroy the notify icon and in which event ? Because when ever I stop the project form running its constantly leaving multiple icons in the system tray

 

Also I am getting an error on GetHostEntry when I use initialise component command ??

 

To get the hostname and ip address, am I better off just using wmi instead of using the inbuilt system.net.dns and gethostentry etc??

Initalise component runs the hidden VS code, don't worry about it.

 

The Hide and Show functions are the same as setting visible = false | true

 

When you stop the project from the STOP icon in VS, it's doing a task kill event, which leaves the icon behind. If you right click and select close, it will remove the icon.

 

Don't use it then. I think Initalise component is a C# thing anyhow.

 

Try to avoid WMI where ever possible, it is buggy as anything. To get the IP you do Dns.GetHostEntry(Dns.GetHostName()). If you want the DNS name of the computer you just do Dns.GetHostName()

  • Thanks 1
Posted
Also What I'm doing on the resize event is sending the window to the tray icon, and removing it from the task bar. Because it's happening on the minimize event, you need to bring it back to a normal state when you show it again
Posted (edited)
Try that Mr Mac, and if it's how you want I'll post code. (It's just the context how you wanted)

 

[ATTACH]9883[/ATTACH]

 

Steve

 

If the above exe is uploaded inc source code The context menu works correctly so Id like that chunk of source, what you did etc, the minimizing the form is wrong but I can easily enough fix that.

 

Because I am using system.net.dns to get ip address / hostname etc, how does that work if say its a laptop which is physically connected ( ethernet cable ) and also by wireless G or N ??

Edited by mac_shinobi
Posted
Initalise component runs the hidden VS code, don't worry about it.

 

The Hide and Show functions are the same as setting visible = false | true

 

When you stop the project from the STOP icon in VS, it's doing a task kill event, which leaves the icon behind. If you right click and select close, it will remove the icon.

 

Don't use it then. I think Initalise component is a C# thing anyhow.

 

Try to avoid WMI where ever possible, it is buggy as anything. To get the IP you do Dns.GetHostEntry(Dns.GetHostName()). If you want the DNS name of the computer you just do Dns.GetHostName()

 

I can't because I keep getting errors about can't convert to type string etc etc

Posted
If the above exe is uploaded inc source code The context menu works correctly so Id like that chunk of source, what you did etc, the minimizing the form is wrong but I can easily enough fix that.

 

Because I am using system.net.dns to get ip address / hostname etc, how does that work if say its a laptop which is physically connected ( ethernet cable ) and also by wireless G or N ??

 

Did you read my bit above? It should be done via the designer, and is just a couple a couple of clicks rather than writing anything in the code?

Posted (edited)

Ok full code enclosed (Not sure where you'll try to run it from but I'm in N drive so it might emo, but you can view form1.vb as it's all in there anyway even if project wont run)

 

WindowsApplication2.zip

 

(From Scratch)

Step 1:

Add ContextMenu and NotifyIcon from Toolbox (I left as default names)

 

Step 2:

In form load, Link Notifyicon to the menu (so when you right click it it appears near etc)

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       NotifyIcon1.ContextMenuStrip = ContextMenuStrip1
       'Links menu to the Icon (aka, why yours was miles away)
   End Sub

 

Step 3:

Link event of clicking Icon to menu showing

 Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
       If e.Button = MouseButtons.Right Then NotifyIcon1.ContextMenuStrip.Show()
       'Shows menu when Icon right clicked
   End Sub

 

Step 4:

Add menu items (Click on contextmenu item on form design page), Right click and edit items (add menu items)

 

Step 5:

Link commands/subs to clicks


   Private Sub InfoMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InfoMenu.Click
       MsgBox("You clicked Info")
       'When Info clicked
   End Sub

 

 

If the default project works ignore my waffle :D But I know when I tried it at home, it emo'd as my home pc is C:\ (obviously) and work one is N:\ :p

 

Any problems shout etc,

 

(ps, off topic but anyone knows how to disable quotes popping up as huge AMG ALERTS on edugeek?)

 

Steve

Edited by Steve21
  • Thanks 1
Posted
If the above exe is uploaded inc source code The context menu works correctly so Id like that chunk of source, what you did etc, the minimizing the form is wrong but I can easily enough fix that.

 

Because I am using system.net.dns to get ip address / hostname etc, how does that work if say its a laptop which is physically connected ( ethernet cable ) and also by wireless G or N ??

 

This is the code I use in HAP+ to get the IP address(es)

 

                foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
                   if (ip.ToString().Contains(".")) { ip1 = ip.ToString(); break; }

Posted
Put .ToString() at the end

 

Also check out MSDN to find out what Properties you can use of them

 

               foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
                   if (ip.ToString().Contains(".")) { ip1 = ip.ToString(); break; }

VB

For Each ip As IPAddress In Dns.GetHostEntry(Dns.GetHostName()).AddressList    
      If ip.ToString().Contains(".") Then
           ip1 = ip.ToString()
           Exit For     
      End If 
Next

  • Thanks 1
Posted (edited)

what is the safest method of logging off, I used to use WMI but you are stating that this is a messy method / means of logging off.

 

The only other way is to use the process.start() method and to use the shutdown with relevant switches or rundll ??

 

This is what I have so far, waiting on confirmation on the best method to log off but as far as the form is concerned ( might be messy ) but functionality assuming it does the same as what it did when I was running it, seems to work, obviously buggy

LogOff.zip

Edited by mac_shinobi
Posted
what is the safest method of logging off, I used to use WMI but you are stating that this is a messy method / means of logging off.

 

The only other way is to use the process.start() method and to use the shutdown with relevant switches or rundll ??

 

This is what I have so far, waiting on confirmation on the best method to log off but as far as the form is concerned ( might be messy ) but functionality assuming it does the same as what it did when I was running it, seems to work, obviously buggy

 

WMI is fine, there are some buggy versions etc, but lots of programs use it with no issues.

 

Btw, There's a much easier method of username if you're intested.

 

ENVIRONMENT.USERNAME works fine assuming it's run as user, and not service/domain account etc. No imports etc, but only an idea :p Both ways work end of day.

 

Other log off options:

 

(Start process as you mentioned) System.Diagnostics.Process.Start("shutdown", "-l -t 00") (Can have issues based on OS etc)

(Windows API) ExitWindowsEx(EWX_LogOff, 0&) (May need premissions) e.g. ExitWindows(RestartOptions.LogOff, True)

Shell commands (Similar to the processes)

Win32Shutdown Method (But if you don't want WMI, can't really use)

 

 

Personally WMI is wonderful, and works for many many MANY things. Shouldn't discount it so quickly, but then opinions I guess :p End of day, Pick one you like, understand and makes it easier for you!

 

Steve

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...