locked
Avoid ToolstrimenuItem highlight RRS feed

  • Question

  • Hi all

    I have a Menustrip1 with several TollStripMenuItem in a WinForm

    When user click on one item I set :

    Menustrip1.enable = false (i don't want the user to be able to click on other command)

    Then the user do some job on textBoxes in the form and at  the end he confirms the job

    Then I set  Menustrip1.enable = True

    The problem is that the last ToolstripMenuItem clickked is highlighted again even if the mouse is not over it

    How to avoid this ?

    Thursday, January 11, 2018 10:56 AM

Answers

  •  Send the WM_KILLFOCUS message to the MenuStrip as shown below.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Const WM_KILLFOCUS As Integer = &H8
    
        <DllImport("user32.dll", EntryPoint:="SendMessageW")>
        Private Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
        End Function
    
        Private Sub Item1ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Item1ToolStripMenuItem.Click
            MenuStrip1.Enabled = False
            TextBox1.Select()
            SendMessageW(MenuStrip1.Handle, WM_KILLFOCUS, IntPtr.Zero, IntPtr.Zero)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MenuStrip1.Enabled = True
        End Sub
    End Class
    
     

     


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Cor Ligthert Thursday, January 11, 2018 7:09 PM
    • Marked as answer by Claudio111 Thursday, January 25, 2018 3:51 PM
    Thursday, January 11, 2018 6:49 PM

All replies

  • Hi

    I do not get the same behavior as you. The MenuStrip becomes Disabled after an Item clicked and Enabled when Button is clicked - no residual Item selection seen.

    Used this code to test.

      Private Sub MenuStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
    	MenuStrip1.Enabled = False
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	MenuStrip1.Enabled = True
      End Sub


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, January 11, 2018 1:54 PM
    Thursday, January 11, 2018 1:54 PM
  • HI les

    try this

    A winform with a Panel (Color blue) then Menustrip with 3 Item (color Trasparent)

    1 text box and 1 button

    then add this code

    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    	End Sub
    
    	Private Sub ITEM1ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ITEM1ToolStripMenuItem.Click
    		MenuStrip1.Enabled = False
    		TextBox1.Focus()
    	End Sub
    
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		MenuStrip1.Enabled = True
    	End Sub
    End Class

    Now,  click on ITEM 1,    then click on Button1

    The ITEM1 is highlighted and the mouse elsewhere

    Thursday, January 11, 2018 3:14 PM
  • Hi

    OK got the same. Can't change that behavior though.


    Regards Les, Livingston, Scotland

    Thursday, January 11, 2018 4:24 PM
  •  Send the WM_KILLFOCUS message to the MenuStrip as shown below.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Const WM_KILLFOCUS As Integer = &H8
    
        <DllImport("user32.dll", EntryPoint:="SendMessageW")>
        Private Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
        End Function
    
        Private Sub Item1ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Item1ToolStripMenuItem.Click
            MenuStrip1.Enabled = False
            TextBox1.Select()
            SendMessageW(MenuStrip1.Handle, WM_KILLFOCUS, IntPtr.Zero, IntPtr.Zero)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MenuStrip1.Enabled = True
        End Sub
    End Class
    
     

     


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Cor Ligthert Thursday, January 11, 2018 7:09 PM
    • Marked as answer by Claudio111 Thursday, January 25, 2018 3:51 PM
    Thursday, January 11, 2018 6:49 PM
  • There can as well be reflection be used, but I like the method of Ray much more. 


    Success Cor

    Thursday, January 11, 2018 7:09 PM
  • Hi Iron

    It works perfectly. I could never find the solution.

    Thank you so much

    Claudio

    • Marked as answer by Claudio111 Friday, January 12, 2018 12:00 PM
    • Unmarked as answer by Claudio111 Thursday, January 25, 2018 3:51 PM
    Friday, January 12, 2018 12:00 PM
  • Hi Iron

    It works perfectly. I could never find the solution.

    Thank you so much

    Claudio

     Claudio,

     If my post answered your question,  then please mark my post as the answer.  Thanks.


    If you say it can`t be done then i`ll try it

    Saturday, January 13, 2018 11:47 AM