Hi all,

I'm writing a simple application in C# but I'm having an annoying ArgumentOutOfRangeException thrown by Invoke() saying "Text length must be less than 64 characters long.".

Is this usual that strings passed through delegates have to be shorter than 64 characters? What's the usual neat way around it? Do I have to box it in some container class?

The code is here:

public enum StatusIcons { NoChange, Idle, Download, Fail, Reply, ShuttingDown, Pause } ;
public void updateStatus(string description, StatusIcons icon = StatusIcons.NoChange, string tooltip = null)
{
    if (this.InvokeRequired)
    {
        System.Diagnostics.Debug.Print("invoke {0},{1}: \r\n{2}\r\n{3}", description.Length, tooltip!= null ? tooltip.Length : 0, description, tooltip);
        this.Invoke(new Action<string,StatusIcons,string>(updateStatus), description, icon, tooltip);
    }
    else
    {
        if (description != null)
        {
            tsStatus.Text = description;
            notify.Text = description;
        }

        if (icon != StatusIcons.NoChange)
            notify.Icon = getIcon(icon);

        if (tooltip != null)
        {
            notify.BalloonTipText = tooltip;
            notify.ShowBalloonTip(2000);
        }
    }
}

Dani AI

Generated

was right — this is not a delegate limitation but a tooltip-length limit on the NotifyIcon/Text API. The WinForms setter for NotifyIcon.Text will throw an ArgumentException when you try to assign more text than the runtime allows; older .NET (and .NET Framework) enforced a ~63-character limit while .NET 6+ relaxed that to 127. (learn.microsoft.com)

The root comes from the native Shell API: the NOTIFYICONDATA structure uses fixed-size buffers (szTip for the hover tooltip and szInfo for balloon text), so different fields have different maximums and the shell historically limited tooltip text sizes. Balloon text (szInfo) is larger (up to 256 bytes including the terminating NUL), so it’s a better fit for longer messages. There are community “reflection” workarounds that try to bypass the managed limit, but they’re brittle and not recommended for production. (learn.microsoft.com)

Practical fixes that work across runtimes:

  • Don’t rely on delegates to change the behavior — sanitize before assigning to the control.
  • Try to set the full text and fall back to a safe truncated value if the setter throws. Example helper (safe for both old and new runtimes):
private void SafeSetNotifyText(NotifyIcon notify, string text)
{
    if (notify == null) return;
    if (string.IsNullOrEmpty(text)) { notify.Text = string.Empty; return; }

    try
    {
        notify.Text = text; // try full text first
    }
    catch (ArgumentException) // too long for this runtime
    {
        const int fallbackMax = 63; // safe for older .NET Framework runtimes
        notify.Text = text.Length <= fallbackMax ? text : text.Substring(0, fallbackMax);
    }
}

Do this truncation (or the try/fallback) before calling Invoke/BeginInvoke so the UI thread never receives an invalid assignment. For longer messages prefer BalloonTipText (or a click-to-open details window / context menu / status bar) rather than the small hover tooltip. Test on the target Windows and .NET versions and avoid reflection hacks except for short-term experiments. (learn.microsoft.com)

Summary: the exception comes from NotifyIcon limits, not delegates. ’s assignments to the tray icon are the trigger; using the safe-set pattern above or moving long text into the balloon or a detail view will eliminate the ArgumentOutOfRangeException. (learn.microsoft.com)

Recommended Answers

All 2 Replies

I'm inferring from your code that you're using a NotifyIcon with some tooltip text. The limitation actually resides here, as the text for this can be no longer than 63 characters.

It's possible to hack around it using reflection, but if you can get it down to 63 characters that would be much cleaner.

Note: The maximum possible length is 127 characters, even with the hack

Thanks very much for the reply. You're completely right. 63 characters is enough for my notify tooltip purposes, really.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.