Search code examples
c#balloon-tip

BalloonTipClicked (Closed/Shown) Sender/EventArgs


I am trying to identify which BalloonTip (NotifyIcon) sent the BalloonTipClicked (and Closed, and Shown) events as I have a few different scenarios where balloons may be shown and they aren't all the same nor will have the same expected actions.

Does anyone know if you can identify anything about the BalloonTip that send the Clicked/Closed/Shown events?


Solution

  • It really does seem like this is a tough cookie to crack. Thanks to @keyboardP for his suggestion as it is a viable alternative, but I ended up creating a special method and enum (as SO didn't email me when I got answers or comments ... I need to check my prefs ...):

    internal static void ShowTip(Int32 timeout, String tipTitle, String tipText, ToolTipIcon tipIcon = ToolTipIcon.None, ShowTipAction tipAction = ShowTipAction.STA_Shown_WriteReg, String linkToOpen = "")
    {
        if ((tipAction & ShowTipAction.STA_Nothing) == 0) // if STA_Nothing has not been passed
        {
            if ((tipAction & ShowTipAction.STA_Clicked_Nothing) == 0 && ((tipAction & ShowTipAction.STA_Clicked_OpenLink) > 0 || (tipAction & ShowTipAction.STA_Clicked_WriteReg) > 0))
                trayIcon.BalloonTipClicked += (s, e) => // if STA_Clicked_Nothing has not been passed and either STA_Clicked_OpenLink or STA_Clicked_WriteReg has been passed
                { // when this balloon tip is clicked
                    if ((tipAction & ShowTipAction.STA_Clicked_OpenLink) > 0) // open passed link
                        MethodorProcessToLaunchSite(linktoOpen);
                    if ((tipAction & ShowTipAction.STA_Clicked_WriteReg) > 0) // write notification indicator to registry
                        RegWriteMethod;
                };
            if ((tipAction & ShowTipAction.STA_Closed_Nothing) == 0 && (tipAction & ShowTipAction.STA_Closed_WriteReg) > 0) // if STA_Closed_Nothing has not been passed and STA_Closed_WriteReg has been passed
                trayIcon.BalloonTipClosed += (s, e) => { RegWriteMethod; }; // when this balloon tip is closed, write notification indicator to registry
            if ((tipAction & ShowTipAction.STA_Shown_Nothing) == 0 && (tipAction & ShowTipAction.STA_Shown_WriteReg) > 0) // if STA_Shown_Nothing has not been passed and STA_Shown_WriteReg has been passed
                trayIcon.BalloonTipShown += (s, e) => { RegWriteMethod; }; // when this balloon tip is shown, write notification indicator to registry
        }
    
        // Show the balloon tip
        trayIcon.ShowBalloonTip(timeout, tipTitle, tipText, tipIcon);
    }
    

    ... and the enum:

    [Flags]
    internal enum ShowTipAction
    {
        STA_Nothing = 1,
        STA_Clicked_Nothing = 2,
        STA_Clicked_OpenLink = 4,
        STA_Clicked_WriteReg = 8,
        STA_Closed_Nothing = 16,
        STA_Closed_WriteReg = 32,
        STA_Shown_Nothing = 64,
        STA_Shown_WriteReg = 128
    }
    

    Between the lambdas and the enum, I can make this as extensible as I like. I know it's not really the prettiest solution, but it works; surprisingly well!