Like it

Showing posts with label Windows Forms. Show all posts
Showing posts with label Windows Forms. Show all posts

Sunday, July 17, 2011

WPF Menu - How to implement Shortcut Keys

WPF Menu - How to implement Shortcut Heys
There are many ways to add shortcut keys in WPF
# Using Underscore - In the Header Property, put the "_" before the Text, and you can use "ALT+<Key>". This is not Shortcut key, it is normally called "HotKey"

   1:  <MenuItem Header="_Open" /> 
# Using InputGestureText - This is the way Visual Studio gives directly. - You can set a Key combination directly over here. 

   1:  <MenuItem Header="_Open" InputGestureText="Ctrl + O" /> 
But it will invoke the click event, even if we use the Key combination. This is for display purpose only.
#Using CommandBindings - - I found this way is better and easier one
  1. In the markup create the command Bindings using Command Collections.
    <CommandBinding Command="ApplicationCommands.Open" 
    CanExecute="CommandCanExecute" Executed="CommandExecuted" /> 
    <CommandBinding Command="ApplicationCommands.Close" 
    CanExecute="CommandCanExecute" Executed="CommandExecuted" />
    
    
  2. In the code implement the event Handlers.
       1:  Sub CommandExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
       2:  'Add the commands to be executed.
       3:  If e.Command.Equals(ApplicationCommands.Open) Then
       4:  MessageBox.Show("Open Menu item clicked")
       5:  ElseIf e.Command.Equals(ApplicationCommands.Close) Then
       6:  MessageBox.Show("Close menu item clicked")
       7:  End If
       8:  End Sub
       9:  Sub CommandCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
      10:  e.CanExecute = True 'I am always returning true, based on this the menu item will enabled / disabled
      11:  End Sub
  3. Assign the Commands to the Command property of our menu items.
    <MenuItem Command="ApplicationCommands.Open" >  <Separator />   
    <MenuItem Command="ApplicationCommands.Close" />
    Compile it run it. You will see "Ctrl + O" added to the right side Open menu item.
    You can see Ctrl + O added into the Open Menu item(Screenshot)
menu_shortcuts_in_wpf
Happy WPF Programming :)

Freehand drawing with .Net Windows forms

Few days back, I got a requirement to implement Free Hand drawing in .Net. I searched a lot but I didn't get any good solution, the one I found from code project was Invalidating the picture box every time. So today I got a solution, it not seems to be a perfect solution because Paint events occurs, we need to re-paint the whole lines again.
I am attaching the code here. I will update the post once I get some perfect solution for this.
Private m_Drawing As Boolean = False
    Private m_List As List(Of Point) = Nothing
    Private Sub MyPic_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            m_Drawing = True
        End If
    End Sub
    Private Sub MyPic_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseUp
        If m_Drawing AndAlso m_List IsNot Nothing Then
            If m_List.Count >= 2 Then
                MyPic.CreateGraphics.DrawLines(New Pen(Color.Blue, 1), m_List.ToArray())
            End If
            m_Drawing = False
            m_List.Clear()
        End If
    End Sub
    Private Sub MyPic_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseMove
        If m_List IsNot Nothing AndAlso m_Drawing Then
            m_List.Add(e.Location)
        End If
    End Sub
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_List = New List(Of Point)
    End Sub