I need some help from the IT gurus on this forum. I'm having trouble with my MS Windows menu's Edit menu item. What I'm trying to do is enable or disable the Cut menu item depending on whether the selectionlength of my textbox's text is zero or greater than zero. But my code below doesn't work. Any ideas how to solve this? Thanks for reading my post!

<MenuItem Click="mnuedit_Click" FontSize="12" Foreground="Black" Header="Edit" Name="mnuedit">
<MenuItem Click="mnucut_Click" FontSize="12" Foreground="Black" Header="Cut" IsEnabled="False" Name="mnucut"/>
</MenuItem>

Private Sub mnuedit_Click(sender As Object, e As RoutedEventArgs)

If txtnote.SelectionLength = 0 Then
mnucut.IsEnabled = False
ElseIf txtnote.SelectionLength >= 1 Then
mnucut.IsEnabled = True
End If

End Sub

my code below doesn't work

How exactly do you want it to work?

Isn't there a MenuItemOpened event you can use?

Sorry, I should have said that I'm trying to make a plain text editor (like MS Notepad). And the menu items would be like this...

Edit (the top level menu item)
Cut (a dropdown menu item)
Copy (a dropdown menu item)
Paste (a dropdown menu item)

And (to answer your question) the way it would work is that if the text in the textbox was not highlighted (selectionlength = 0) then "Cut" and "Copy" wouldn't be enabled. And if the text was highlighted (selectionlength > 0) then the "Cut" and "Copy" would be enabled. And I was trying to check this state/condition when the user clicked on the "Edit" menu item.

Now, the good news is that I found a solution...

<MenuItem Command="Cut" FontSize="12" Foreground="Black"/>

This "Command" XAML code automatically checks to see if the text is highlighted or not and then activates or deactivates the Cut menu item. It also automatically adds a keyboard shortcut ("Ctrl+X") to the menu item. So now there's no need for VB.Net click event code to enable / disable the "Cut" menu items because it's already done.

Whoops! I forgot to say... Thanks pritaeas for trying to help me :)

Never think like a lazy one. Think deeply how could you do it by writing your own code which can give more grip on codification.

You can use selection changed event of text box to do your owns.

Thanks for helping solve this question. I also have the same problems.

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.