:: Home

  login:         
  passwords:  

Winforms Interview Questions

Windows Forms Deployment
Windows Forms Controls
Windows Forms Data Binding
Windows Forms Datagrid
Windows Forms Docking
Windows Forms Keyboard Handling
Windows Forms Layout
Windows Forms Licensing
Windows Forms Menus
Windows Forms Mouse Handling
Windows Forms from MFC
Windows Forms from VB6
Windows Forms Patterns
Windows Forms Printing
Windows Forms Resources
Windows Form Scrolling
Windows Forms Tips
Windows Forms Common Dialogs
Windows Forms Listbox
Windows Forms ComboBox
Windows Forms Rich TextBox
Windows Forms ListView
Windows Forms TreeView
Windows Forms Button
Windows Forms TabControl
Windows Forms TextBox
Windows Forms MDI
Windows Forms Cursors
Windows Forms WebBrowser
Windows Forms PictureBox
Windows Forms Form
Windows Forms MDI
Windows Forms In IE
GDI+Bitmaps&Images
GDI+Font
GDI+Color
GDI+Brushes
GDI Drawing Tips
GDI+ from GDI
GDI Paths Regions
GDI+Pens
Interioerability WIn32
Tools Metadata Viewers
Design Time Serialization
Design Time Custom Designers
Design Time Tips
Design Time Type Editors
Vs.Net Tips
Vs.Net Debugging
Vs.Net Macros
Framework Tips General
Framework Tips Events
Framework Tips General IO
Framework Tips Strings
Framework Tips Threading
Tool Resource Editor
Design Time UI
Framework Tips CGI
Framework Tips XML

WPF Interview Qs

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

Windows Forms Keyboard Handling

The General Winforms Interview Questions consists the most frequently asked questions in Winforms. This list of 100+ questions guage your familiarity with the Winforms platform. The q&a have been collected over a period of time from various blogs, forums and other similar Winforms sites

6.Windows Forms Keyboard Handling

    6.1 How can I prevent a control from getting a particular keystroke?
   6.2 How can I tell if an ALT, Shift or CTL key is pressed without catching an event?
   6.3 How do I check the state of the virtual keys, Caps lock for example?
    6.4 How can I simulate keyboard input in my application?
   6.5 How can I catch keyboard messages on a application-wide basis?
   6.6 How can I listen for certain keys at the Form level irrespective of which Control has the focus?

6.1 How can I prevent a control from getting a particular keystroke?

You can handle the control's KeyPress event and indicate the key has been handled. Below is code that prevents a TextBox from getting an 'A' and the return key.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13 || e.KeyChar == 'A')
e.Handled = true;
}

6.2 How can I tell if an ALT, Shift or CTL key is pressed without catching an event?

Use the static property Control.ModifierKeys.

Console.WriteLine(Control.ModifierKeys);
if( (Control.ModifierKeys & Keys.Shift) != 0)
Console.WriteLine("the shift key is down");
if( (Control.ModifierKeys & Keys.Alt) != 0)
Console.WriteLine("the alt key is down");
if( (Control.ModifierKeys & Keys.Control) != 0)
Console.WriteLine("the control key is down");


 


6.3 How do I check the state of the virtual keys, Caps lock for example?

If the Control.ModifierKeys doesn't address your issue, then use Platform Invoke and call GetKeyState directly.
Declare this class first:
[
ComVisibleAttribute(false),
SuppressUnmanagedCodeSecurityAttribute()
]
internal class NativeMethods
{
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
public static int HIWORD(int n)
{
return ((n >> 16) & 0xffff/*=~0x0000*/);
}
public static int LOWORD(int n)
{
return (n & 0xffff/*=~0x0000*/);
}
}
Then when you want to check if Caps is down or ON, call:
short state = NativeMethods.GetKeyState(0x14 /*VK_CAPTIAL*/);
bool capsKeyDown = NativeMethods.HIWORD(state);
bool capsKeyON = NativeMethods.LOWORD(state);< BR >

6.4 How can I simulate keyboard input in my application?

This can be done through the SendKeys class in the System.Windows.Forms namespace. Check it out in the MS help documentation.

6.5 How can I catch keyboard messages on a application-wide basis?

You can implement the IMessageFilter interface in your main form. This amounts to adding an override for PreFilterMessage, and looking for the particular message you need to catch. Here are code snippets that catch an escape key on a keydown. You can download a sample project(C#, VB). In the sample, there are two forms, with several controls. You'll notice that no matter what form or control has input focus, the escape key is caught in the PreFilterMessage override.
[C#]
public class MyMainForm : System.Windows.Forms.Form, IMessageFilter
{
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
public bool PreFilterMessage(ref Message m)
{
Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;
if(m.Msg == WM_KEYDOWN && keyCode == Keys.Escape)
{
Console.WriteLine("Ignoring Escape...");
return true;
}
return false;
}
....
....
....
private void MyMainForm_Load(object sender, System.EventArgs e)
{
Application.AddMessageFilter(this);
}
}
[VB.NET]
Public Class MyMainForm
Inherits System.Windows.Forms.Form
Implements IMessageFilter
Private WM_KEYDOWN As Integer = &H100
Private WM_KEYUP As Integer = &H101
Public Function PreFilterMessage(ByRef m As Message) As Boolean
Dim keyCode As Keys = CType(CInt(m.WParam), Keys) And Keys.KeyCode
If m.Msg = WM_KEYDOWN And keyCode = Keys.Escape Then
Console.WriteLine("Ignoring Escape...")
Return True
End If
Return False
End Function 'PreFilterMessage
....
....

....
Private Sub MyMainForm_Load(sender As Object, e As System.EventArgs)
Application.AddMessageFilter(Me)
End Sub 'MyMainForm_Load
End Class 'MyMainForm

6.6 How can I listen for certain keys at the Form level irrespective of which Control has the focus?

When the Form.KeyPreview property is set to true, the Form's KeyPress, KeyDown and KeyUp events will be fired even before the Control with the focus' corresponding events. You may choose to forward these message to the Control after processing them in the Form's event handlers (this happens by default) or set the e.Handled property to true (in the event argument) to prevent the message from being sent to the Control with focus.

Copyright 2007, Megasolutions Ltd