Label能否获得焦点?

发布于 2024-11-02 07:45:58 字数 151 浏览 0 评论 0原文

我大学的考试中有一道关于 C# 的问题。 标签可以获得焦点吗? 正如我在 MSDN 网站上看到的,所有控件都可以获得焦点,但其中一些控件不可选。所以在我看来,正确的答案是“标签可以获得焦点,但无法选择”。 Label 还有一个 Focus() 方法。请帮助我理解。谢谢。

I have a one question on my university's test about C#. Could label get a focus? As I can see on MSDN site, all Controls can get a focus, but some of them aren't selectable. So it's seems to me that the right answer is "Label could get a focus, but couldn't be selected". Also Label has a Focus() method. Please, help me understand. Thanx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

北城孤痞 2024-11-09 07:45:58

是的,Label 上有一个 Focus() 方法,是的,它绝对正确,它可以工作;但行为有所不同。让我尝试解释一下,

标签可以与某个输入控件关联,例如用户名文本字段的标签,因此存在与标签关联的控件的概念。 msdn 上的 AssociatedControlID

所以你可以将输入控件与标签相关联,并且每当选择标签时,控件就会传递到关联的输入控件。

示例 单击登录框中的电子邮件或密码标签,看看发生了什么,如果您在标签上调用 focus 方法,则类似焦点将传递到关联的控件。

Yes there is a Focus() method on Label and yes it is absolutely right it works; but behave differently. let me try to explain

A Label can be associated with some one input control, for instance a label for a user name text field, so there is concept of Associated Control with the label. AssociatedControlID on msdn

So you can associate an input control with a label and whenever label is selected the control passed to the associated input control.

Example here click on Email or Password labels in login box and see what happened, similarly if you call focus method on the label the focus will passed to the associated control.

我为君王 2024-11-09 07:45:58

来自文档

可以选择并接收一个控件
如果满足以下所有条件则输入焦点
true:Selectable
ControlStyles 设置为 true,它是
包含在另一个控件中,并且所有
它的父控件都是可见的
并启用。

由于 Label 控件不可选择,因此无法接收输入焦点,即使它从 Control 继承 Focus() 方法也是如此。因此,答案是否定的。

From the documentation:

A control can be selected and receive
input focus if all the following are
true: the Selectable value of
ControlStyles is set to true, it is
contained in another control, and all
its parent controls are both visible
and enabled.

Since a Label control is not selectable, it cannot receive input focus, even if it inherits a Focus() method from Control. Therefore, the answer is no.

铜锣湾横着走 2024-11-09 07:45:58

很容易发现控件是否无法获得焦点。只需读取

.CanFocus

从 Control 类继承的属性即可。

以下列表中的 Windows 窗体控件不可选择。从这些控件派生的控件也是不可选的。 (请参阅 MSDN 文档

  • 面板
  • GroupBox
  • PictureBox
  • ProgressBar
  • Splitter
  • Label
  • LinkLabel(当控件中不存在链接时)

另外:

如果满足以下条件,Focus 方法将返回 true
控件成功接收输入
重点。控件可以有输入
聚焦而不显示任何视觉效果
获得焦点的线索。这
行为主要是由
下面列出了不可选择的控件,
或从它们派生的任何控件。

可以选择并接收一个控件
如果满足以下所有条件则输入焦点
true:Selectable
ControlStyles 设置为 true,它是
包含在另一个控件中,并且所有
它的父控件都是可见的
并启用。

如果您需要一个可以聚焦的类似标签的控件,只需使用 TextBox 并将其设置为只读即可。设置一些其他属性(样式、不可选择等),就完成了。

It's easy to findo out if a control's ca get focus. Just read the

.CanFocus

property which is inherited from the Control class.

The Windows Forms controls in the following list are not selectable. Controls derived from these controls are also not selectable. (see MSDN documentation)

  • Panel
  • GroupBox
  • PictureBox
  • ProgressBar
  • Splitter
  • Label
  • LinkLabel (when there is no link present in the control)

Also:

The Focus method returns true if the
control successfully received input
focus. The control can have the input
focus while not displaying any visual
cues of having the focus. This
behavior is primarily observed by the
nonselectable controls listed below,
or any controls derived from them.

A control can be selected and receive
input focus if all the following are
true: the Selectable value of
ControlStyles is set to true, it is
contained in another control, and all
its parent controls are both visible
and enabled.

If you need a Label-like control that you can focus, just use a TextBox and make it readonly. Set a few other properties (styles, not selectable etc.) and you're done.

凑诗 2024-11-09 07:45:58

您将看到标签上有一个名为 CanFocus 的只读属性,如果您在调试时查看此属性,您会发现它是 false。

每个继承自Control的控件都有focus方法,但这并不意味着它可以获得焦点。

You will see that there is a read only property called CanFocus on a label, if you have a look at this property while debugging you will see it is false.

Every control that inherits from Control has the focus method, but that does not mean that it can be focused.

≈。彩虹 2024-11-09 07:45:58

Label 确实获得了焦点,但它将焦点升级到其“for”属性中指定的输入字段。例如:

<label for="firstname">First Name</label><input type="text" name="firstname" />

在这种情况下,如果您单击标签,它将把焦点转移到与其关联的输入字段“名字”。

Label does gets the focus but it escalates it to the input field specified in its "for" attribute. Like:

<label for="firstname">First Name</label><input type="text" name="firstname" />

In this scenario if you click on the label it will throw the focus to the input field "firstname" associated with it.

葬花如无物 2024-11-09 07:45:58

这是一年前的事了,但我也遇到了与 Op 类似的问题。就我而言,它是一个用户控件,上面有一个标签(停靠在填充处)(它在幕后还有其他功能 - 它是一个日历控件,并在任一面板中弹出一个日期选择器 - 不是标准的日期选择器) (弹出窗口)或表单(弹出窗口))。

那里的问题是 UserControls 实际上是作为容器并阻止焦点(将其推到子控件) - 由于标签是唯一的子控件,它会阻止 UserControl 获得焦点。使用只读 TextBox 是一个很差的替代品,因为它缺乏垂直对齐并且必须是多行来调整高度。

我在这里添加以下内容作为答案的原因是因为它是可能的(抱歉,在这里说不可能的人),我发现这篇文章和许多类似的文章在我查看时几乎没有帮助。不管怎样,方法是覆盖 Label 并设置 SetStyle - 还有 OnPaint 来绘制焦点矩形(我手动绘制了我的,因为 DrawFocusRectangle 似乎没有做任何事情) - 如下所示:

internal class SelectableLabel: Label
{
    public SelectableLabel():base()
    {
        SetStyle(ControlStyles.Selectable, true);            
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        //ControlPaint.DrawFocusRectangle(e.Graphics, ClientRectangle);
        if (!Focused) return;
        if (BorderStyle == BorderStyle.Fixed3D)
        {
            e.Graphics.DrawLines(Pens.CadetBlue, new[] { new Point(1, Height - 1), new Point(1, 1), new Point(Width - 1, 1) });
            e.Graphics.DrawLines(Pens.Aquamarine, new[] { new Point(2, Height - 1), new Point(Width - 1, Height - 1), new Point(Width - 1, 2) });
        }
        else
        {
            e.Graphics.DrawRectangle(Pens.Aquamarine, 0, 0, Width - 1 , Height - 1 );
        }
    }        
}

我不关心我自己这样做在学术上(纯粹主义观点)是否正确,但有充分的理由允许输出控制(如标签)有时获得关注。

This is a year old, however I had a similar issue as the Op. In my case it was a user control that had a single label (docked at fill) on it (it has other functions behind the scenes - it is a calendar control and pops up a date picker - not the standard one - in either a panel (popunder) or a form (popup)).

The issue there was that UserControls are really intended as containers and resist focus (pushing it off to child controls) - as the label is the only child control, it stops the UserControl getting focus. Using readonly TextBox is a poor substitute as it lacks vertical alignment and must be multiline to size the height.

The reason I add the following as an answer here is because it IS possible (sorry guys who said here it is not) and I found this post and many like it that were little help when I looked. Anyway, the way to do it is to override the Label and set the SetStyle - also the OnPaint to draw a focus rectangle (I manually drew mine as DrawFocusRectangle didn't seem to do anything) - so as below:

internal class SelectableLabel: Label
{
    public SelectableLabel():base()
    {
        SetStyle(ControlStyles.Selectable, true);            
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        //ControlPaint.DrawFocusRectangle(e.Graphics, ClientRectangle);
        if (!Focused) return;
        if (BorderStyle == BorderStyle.Fixed3D)
        {
            e.Graphics.DrawLines(Pens.CadetBlue, new[] { new Point(1, Height - 1), new Point(1, 1), new Point(Width - 1, 1) });
            e.Graphics.DrawLines(Pens.Aquamarine, new[] { new Point(2, Height - 1), new Point(Width - 1, Height - 1), new Point(Width - 1, 2) });
        }
        else
        {
            e.Graphics.DrawRectangle(Pens.Aquamarine, 0, 0, Width - 1 , Height - 1 );
        }
    }        
}

I am not concerning myself on whether it is accademically (purist view) right to do so, but that there are valid reasosn to allow an output control (like label) to sometimes gain focus.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文