获取选定的控件

发布于 2024-08-07 11:03:41 字数 692 浏览 3 评论 0原文

我有一个“画布”(只是一个面板控件),用户可以单击按钮将某些控件添加到画布,即标签、链接标签、图像等...然后他们可以编辑这些控件,就像它们一样可以编辑他们刚刚添加的标签的文本...

但我试图让他们为他们单击的控件选择新字体和新颜色,但它并不总是有效,即使它应该是...

我的代码是:

private string SelectedControl;

当我单击控件时:

private void label_Click(object sender, EventArgs e)
{
    Label label = (Label)sender;
    SelectedControl = label.Name;
}

当用户选择字体时:

private void setfont()
{
    foreach(Control control in Canvas.Controls)
    {
        if(control.Name == SelectedControl)
        {
            control.Font = selectedfont;
        }
    }
}

所以,这段代码确实有效,但并非总是有效。有谁知道任何其他方法可以以某种方式跟踪上次单击的控件,以便以后可以引用它?

I have a "canvas" (which is just a Panel Control), and the user can click a button to add certain controls to the canvas i.e. labels, link labels, images etc... And then they can edit those controls, like they can edit the text of the label they just added...

But I'm trying to let them choose a new font and a new color for the control that they clicked on, but it doesn't always work, even though it should be...

the code i have is:

private string SelectedControl;

when i click on a control:

private void label_Click(object sender, EventArgs e)
{
    Label label = (Label)sender;
    SelectedControl = label.Name;
}

when the user selects a font:

private void setfont()
{
    foreach(Control control in Canvas.Controls)
    {
        if(control.Name == SelectedControl)
        {
            control.Font = selectedfont;
        }
    }
}

So, This code does work BUT just not all the time. Does anybody know of any other way to somehow keep track of the Last-Clicked control, so it can be referenced later?

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

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

发布评论

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

评论(1

小红帽 2024-08-14 11:03:41

为什么不存储对上次单击的控件本身的引用,而不是存储字符串名称?

所以你的代码就变成了:

private Control SelectedControl;

private void label_Click(object sender, EventArgs e)
{
    Control ctrl = sender as Control;
    if(ctrl != null)
        SelectedControl = ctrl;
}

private void setfont()
{
    SelectedControl.Font = selectedfont;
}

存储引用的成本非常低,并且你可以直接访问引用。

编辑:通过更改对控件的引用,您可以引用许多不同类型的控件,而无需进行类型转换。
我编写这段代码的原因

Control ctrl = sender as Control;
if(ctrl != null)
        SelectedControl = ctrl;

是:它是一种更安全的类型转换。如果发送者由于某种原因没有从 Control 继承,那么它不会被类型转换,并且 label 将为 null,但不会引发异常。

您可以为您希望能够通过选择进行更改的所有控件创建一个单击事件处理程序。如果您在代码中创建控件并手动连接事件,这会更容易。

您可能想要考虑做一些边框或效果来显示已选择哪个控件。
这可以通过这样做来执行:

if(ctrl != null)
{
    Deselect(SelectedControl); //Deselects the old control
    SelectedControl = ctrl;
    Select(SelectedControl); //Selects the new control
}

取消选择和选择执行一些奇特的效果或边框。

希望这有帮助。

instead of storing the string name, why not store a reference to the last clicked control itself?

so your code becomes:

private Control SelectedControl;

private void label_Click(object sender, EventArgs e)
{
    Control ctrl = sender as Control;
    if(ctrl != null)
        SelectedControl = ctrl;
}

private void setfont()
{
    SelectedControl.Font = selectedfont;
}

It costs very little to store a reference, and you can access the reference directly.

EDIT: By changing the reference to being a Control, you are able to reference many different types of controls without having to typecast around.
The reason I do this bit of code:

Control ctrl = sender as Control;
if(ctrl != null)
        SelectedControl = ctrl;

is it's a safer typecast. If the sender, for some reason, doesn't inherit from Control, then it won't be typecast and label will be null, but an exception won't be raised.

You could have a single click event handler for all of the controls that you want to be able to be changed via being selected. This will work much easier if you are creating the controls in code and hooking up the events manually.

You may want to look into doing some border or effect to show which control has been selected.
This could be performed by doing this:

if(ctrl != null)
{
    Deselect(SelectedControl); //Deselects the old control
    SelectedControl = ctrl;
    Select(SelectedControl); //Selects the new control
}

where Deselect and Select do some fancy effects or border.

Hope this helps.

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