SurplusCode

C#, PHP, CSS, DHTML, AJAX

Archive for the ‘Tips’ Category

Thursday
Nov 2,2006

I’ve recently needed to find what control currently has focus in my WinForms application. Google gave me two answers. The first depends on unmanaged code, and the second is fully managed.

Reference
[csharp]
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetFocus();

private Control GetFocusedControl()
{
Control ctl = FromHandle(CreateProjectPanel.GetFocus());

//MessageBox.Show(ctl.Name);

return ctl;
}
[/csharp]

I can’t remember where I found this second solution, but it is the one I use in my code. It uses the Control.Focused and Control.ContainsFocus properties to recursively find the focused control.
[csharp]
private System.Windows.Forms.Control GetFocusedControl(System.Windows.Forms.Control.ControlCollection Controls)
{
// store focused control…
foreach (System.Windows.Forms.Control clsControl in Controls)
{
if (clsControl.Focused)
{
return clsControl;
}
if (clsControl.ContainsFocus)
{
if (clsControl.Controls.Count == 0)
{
return clsControl;
}
else
{
return GetFocusedControl(clsControl.Controls);
}
}
}
// no focus…
return null;
}

// usage
Control hasFocus = GetFocusedControl(this.FindForm().Controls);
[/csharp]

  • Comments Off
  • Tuesday
    Jun 27,2006

    I rolled my own dialog in WinForms 2.0 and wanted to use the images that is available when using the MessageBox by way of the MessageBoxIcon enum. To do what I wanted, I used the System.Drawing.SystemIcons class, which exposed static references to several system icons, of course. Here’s an example use of the Warning icon in a PictureBox:

    [csharp]
    System.Windows.Forms.PictureBox pb = new System.Windows.Forms.PictureBox();
    pb.Location = new System.Drawing.Point(14, 7);
    pb.Size = new System.Drawing.Size(48, 41);
    pb.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
    pb.BackgroundImage = System.Drawing.SystemIcons.Warning.ToBitmap();
    [/csharp]

    System.Drawing.SystemIcons also has Application, Asterisk, Error, Exclamation, Hand, Information, Question, Warning, and WinLogo.

    The System.Drawing namespace also has SystemBrushes, SystemColors, SystemFonts, and SystemPens.

  • Comments Off
  • Thursday
    Jun 15,2006

    I was asked by our user interaction designer to make Enter key presses mimic a Tab key press by moving focus to the next control where TabStop=true. Sounds simple enough. But after several fruitless searches on Google, I thought I would have to resort to using unmanaged code. Luckly, I finally found a blog post with the answer that I was looking. Well, the post itself didn’t solve my problem, but the comments pointed me in the right direction.

    The initial suggestion to override the form’s OnKeyUp event doesn’t apply to my case because my text fields were in a panel. ProcessKeyTab is a protected method that can only be called when you extend a control that is derived from ContainerControl, such as Form or Panel. Since I didn’t extend the Panel I was using, I couldn’t call ProcessKeyTab.

    After reading the comments, I found that the answer lies in using the static SendKeys.Send method. In each TextBox that you want to treat Enter as Tab, simply handle the KeyPress event and call SendKeys.Send(”{TAB}”) if the key pressed is an Enter key.

    (more…)

  • Comments Off