Showing posts with label c# winforms key events. Show all posts
Showing posts with label c# winforms key events. Show all posts

November 4, 2007

Using KeyDown Event On A Grid/List Control

This code adds keyboard handling to a grid or list control. The Delete Key performs a Delete, the Insert Key performs an Insert, the Return Key performs a an edit when a single row is selected

private void xxxxxxxxxxxxx_KeyDown(object sender, KeyEventArgs e)
{
   switch (e.KeyCode)
   {
       // Delete with one or more rows selected 
       // performs a Remove on those rows
       case Keys.Delete: 
         e.Handled = true;
         DeleteSelectedRows();
           break;
       case Keys.Insert: // Insert performs an Add
         e.Handled = true;
         AddRow();
           break;
       // RETURN When a single row is selected 
       // perform an Edit, editing the currently 
       // selected row
       case Keys.Return: 
           
         e.Handled = true;
         EditSelectedRow());
           break;
       default:
           break;
   }
}

April 13, 2007

Discarding Pending Mouse Or Keyboard Messages in C#

public static class Win32Helper
{

/// Window messages
private enum WindowMessage : uint
{
    // Keyboard messages
    KeyboardFirst = 0x0100,
    KeyboardLast = 0x0108,

    // Mouse messages
    //MouseMove = 0x0200,
    MouseFirst = 0x0201, // Skip mouse move, it happens 
                         //a lot and there is another message for that
    MouseLast = 0x020d,
}

public static void DiscardMousebMessages(IntPtr hWnd)
{
    Message msg;
    while (PeekMessage(out msg, hWnd, (uint)WindowMessage.MouseFirst,
        (uint)WindowMessage.MouseLast, (uint)PeekMessageFlags.PM_REMOVE)) ;
}

public static void DiscardKeyboardMessages(IntPtr hWnd)
{
    Message msg;
    while (PeekMessage(out msg, hWnd, (uint)WindowMessage.KeyboardFirst,
        (uint)WindowMessage.KeyboardLast, (uint)PeekMessageFlags.PM_REMOVE)) ;
}

[StructLayout(LayoutKind.Sequential)]
private struct Message
{
    public IntPtr hWnd;
    public WindowMessage msg;
    public IntPtr wParam;
    public IntPtr lParam;
    public uint time;
    public System.Drawing.Point p;
}

[System.Security.SuppressUnmanagedCodeSecurity] 
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern bool PeekMessage(
    out Message msg, IntPtr hWnd, 
    uint messageFilterMin, uint messageFilterMax, 
    uint flags);

}

January 2, 2007

Controlling TextBox Character Input

Good set opf samples
/// <summary>
/// Ensure only alpha numeric characters and
/// backspace are acceptable characters for the Thingey
/// </summary>
/// <param name="sender"></param>
/// <param name="kpea"></param>
private void OnSomeTextBox_KeyPressEvent(object sender, 
    KeyPressEventArgs kpea)
{
    const char BACKSPACE = '\b';
    const char FULLSTOP = '.';
    if (textBox.Text.Length > 0) // AFter first character
    {
        if ( !Char.IsLetterOrDigit(kpea.KeyChar) && 
            (kpea.KeyChar != BACKSPACE) && 
            (kpea.KeyChar != FULLSTOP))
        { // input is not passed on to the control(TextBox)
            kpea.Handled = true; 
        }
    } // First character must be an alphabetic character
    else if (!Char.IsLetter(kpea.KeyChar)) 
    {
 // input is not passed on to the control(TextBox)
        kpea.Handled = true; 
    }
}

// Another example
    switch(kpea.KeyChar) 
    {  
        case 'a': 
        case 'b': 
        case 'c': 
        case '#': 
        case '*': 
        case '1': 
            e.Handled=true; //event is handled.
            this.errorProvider.SetError(this.textboxChars, 
               "not allowed chars: 'a','b','c','#','*','1'");
            this.statusBar.Text="not allowed char..."+e.KeyChar;
            break; 
       default:
            //clear error
            this.errorProvider.SetError(this.textboxChars, "");
            break;
    } //switch 


    private static class FilterCharacters
    {
        public static void FilterSample(TextBox tb, 
     KeyPressEventArgs kpea)
        {
            const char BACKSPACE = '\b';
            const char FULLSTOP = '.';
            const char HYPHEN = '-';
            const char UNDERSCORE = '_';
            if (tb.Text.Length > 0)
            {
                if (!Char.IsLetterOrDigit(kpea.KeyChar) && 
                    (kpea.KeyChar != BACKSPACE) && 
                    (kpea.KeyChar != FULLSTOP) &&
                    (kpea.KeyChar != HYPHEN) &&
                    (kpea.KeyChar != UNDERSCORE) )
                { // input is not passed on to the control(TextBox)
                    kpea.Handled = true; 
                }
            }// First character must be an alphabetic character
            else if (!Char.IsLetter(kpea.KeyChar)) 
            {// input is not passed on to the control(TextBox)
                kpea.Handled = true; 
            }
        }

        public static void NumericOnlyFilter(TextBox tb,
      KeyPressEventArgs kpea)
        {
            const char BACKSPACE = '\b';
            //const char FULLSTOP = '.';
            if (!Char.IsDigit(kpea.KeyChar) && 
                // If you want decimal numbers
                //(kpea.KeyChar != FULLSTOP) && 
                (kpea.KeyChar != BACKSPACE) ) 
            {
               kpea.Handled = true; 
            }
        }
    }