question

AndrewMercer-9876 avatar image
0 Votes"
AndrewMercer-9876 asked AndrewMercer-9876 answered

ToolStrip single click

When Visual Studio does not have input focus, you can move the mouse over a tool strip item and you will see the tool tip text. A single click invokes the intended action. In my .net forms app, I have a tool strip with some items. Hovering the mouse shows my tool tip whether my app has focus or not. But if I don't have focus, it takes to clicks to invoke the intended action (one to activate/gain focus, and one to invoke the action). Except for this one vs two clicks issue, my tool strip behaves identically to VS. What is the proper way to get the same response as VS? I have seen some web pages describing various ways, but they look clunky and out of date.

windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndrewMercer-9876 avatar image
0 Votes"
AndrewMercer-9876 answered

Thanks viorel for your contribution, but I think I've found a way more in line with my use of .net. For each ToolStripItem in my app I add handlers for MouseEnter and MouseDown. On Enter, I activate the app, and on Down I execute the desired functionality. The key is handling Enter as that lets Down fire. MouseDown will not fire for a ToolStripItem unless it is active, but MouseEnter does.

Alternatively, activate on ToolStripItem.MouseEnter and subsequently handle ToolStrip.ItemClicked would work.

The down side is a focus shift by just passing the mouse over a tool strip, but that is ok with me in my multi-window app. The original problem was two clicks needed when one of my windows had focus but I wanted to run a toolstrip item on another window.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

Try using an adjusted Tool Strip. In case of C#, add this class to your project:

 using System;
 using System.Windows.Forms;
    
 namespace MyNamespace
 {
    public class MyToolStrip : ToolStrip
    {
       protected override void WndProc( ref Message m )
       {
          const int WM_MOUSEACTIVATE = 0x0021;
          const int MA_ACTIVATE = 1;
    
          switch( m.Msg )
          {
          case WM_MOUSEACTIVATE:
             m.Result = new IntPtr( MA_ACTIVATE );
             return;
          }
    
          base.WndProc( ref m );
       }
    }
 }

After compilation, this control will appear in Toolbar, and you can drag it to your forms. But since you already have a form, then try editing the Form.Designer.cs file manually, using Notepad or Visual Studio. Replace the standard System.Windows.Forms.ToolStrip with MyNamespace.MyToolStrip. The adjusted lines will look like this:

 . . .
 this.toolStrip1 = new MyNamespace.MyToolStrip();
 . . .
 private MyNamespace.MyToolStrip toolStrip1;
 . . .

Backup your files before such changes.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndrewMercer-9876 avatar image
0 Votes"
AndrewMercer-9876 answered

I understand your suggestion. But my needs have always been solved with out-of-the-box use of .net controls. I know I can make a new control and integrate it into visual studio, but that would be a new adventure for me. I could also add the new ToolStrip dynamically, and that would just be a different adventure.

Maybe I'll try one or the other (probably 2nd). Can you think of an alternative? I was thinking of catching mouse down at the form level and figuring out if it was in the hot part of one of the ToolStripItems.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndrewMercer-9876 avatar image
0 Votes"
AndrewMercer-9876 answered

FYI the final form of the ToolStripItem.MouseEnter event handler is:

Private Sub ToolStripItemMouseEnter(sender As Object, e As EventArgs)
Dim tsi As ToolStripItem = CType(sender, ToolStripItem)
Dim ts As ToolStrip = tsi.GetCurrentParent
ts.Focus()
End Sub

It can be Shared (static) or not. As each MyToolStripItem as ToolStripItem is created, I do this:

AddHandler MyToolStripItem.MouseEnter, AddressOf ToolStripItemMouseEnter

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.