将焦点设置到.net用户控件...? [英] Setting Focus to a .NET UserControl...?

查看:162
本文介绍了将焦点设置到.net用户控件...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建从用户控件派生的自定义控制,我想将焦点设置到。

I'm creating a custom control derived from UserControl that I would like to set focus to.

在自定义的控件包含一个组合框控件,我画一些字符串旁边。

The custom control contains a ComboBox control and I draw some strings beside it.

组合框可以接收焦点,但我希望能够将焦点设置到该用户控件本身。我的OnPaint处理设置为绘制控件是否具有焦点略有不同,但我打电话CanFocus()从父窗体,当我创建我的自定义控制和返回false。

The ComboBox can receive focus, but I would like to be able to set focus to the UserControl itself. My OnPaint handler is set up to draw the control slightly differently if it has focus but I call CanFocus() from the parent form when I create my custom control and it returns false.

有一个属性什么的设置?

Is there a property or something to set?

推荐答案

用户控件会打你的牙齿和指甲,以避免焦点。它具有code,它会自动将焦点子控件(如果有的话),如果它确实获得焦点。你至少必须覆盖的WndProc()和捕获WM_SETFOCUS消息。有可能是需要其他手术一样,ControlStyles.Selectable和接受tab和tabIndex属性。

UserControl will fight you tooth and nail to avoid getting the focus. It has code that automatically passes the focus to a child control (if any) if it does get the focus. You'll at a minimum have to override WndProc() and trap the WM_SETFOCUS message. There might be other surgery needed, like ControlStyles.Selectable and the TabStop and TabIndex properties.

您的下一个问题是,用户控件不会有效应对,比如说,键盘消息当它具有焦点。你将需要检测的UC后台点击来处理鼠标消息,以及覆盖绘制,这是有目共睹的是,UC拥有焦点(使用ControlPaint.DrawFocusRectangle)的用户。如果这开始听起来没有吸引力,这是因为UC真的意味着是一个容器控件。

Your next issue is that UserControl won't respond meaningfully to, say, keyboard messages when it does have focus. You'll need to detect clicks on the UC background to handle mouse messages, as well as override the painting so it is obvious to the user that the UC has the focus (use ControlPaint.DrawFocusRectangle). If this starts to sound unattractive, it's because UC was really meant to be a container control.

这篇关于将焦点设置到.net用户控件...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆