.NET UserControl接受按钮最佳实践?

发布于 2024-07-25 15:09:35 字数 149 浏览 1 评论 0原文

我有一个 Windows 窗体应用程序,其中我将不同的功能拆分为多个用户控件。 我希望每个用户控件都有一个接受按钮。

这里有什么最佳实践吗?

我的想法是检测哪个用户控件具有焦点,然后将其设置在父窗体中。

还有其他想法吗?

I have a windows forms app where I have split different functionality into several user controls. I want each of these user controls to have an accept button.

Any best practices here?

My idèa is to detect which user control that has focus, and than set it in the parent Form.

Any other idèas?

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

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

发布评论

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

评论(5

颜漓半夏 2024-08-01 15:09:35

最佳实践通常是您的表单只有一个接受按钮,以便其行为保持一致。 如果根据表单的哪个部分获得焦点,按回车键会导致不同的操作,那么通常会让用户感到困惑。 但是,如果您有专门的应用程序或用户请求此功能,那么我认为您提出的解决方案是可行的。

The best practice is usually to only have one accept button for your form so that its behavior is consistent. It generally would be confusing for users if hitting return caused different actions depending on which section of the form had focus. However, if you have a specialized application or users have requested this feature then I think the solution you propose would work.

ゝ杯具 2024-08-01 15:09:35

Jan Miksovsky 有一篇关于 UI 设计的优秀博客,并写了一篇关于这件事的文章

大多数 UI 平台都允许设计者
指示对话框中的哪个按钮
应该是默认按钮:
如果出现以下情况,将按下该按钮
用户键入 Enter 键。 默认
按钮一般是按钮
用户最有可能按下一步,
通常是像“确定”这样的按钮可以关闭
对话。 在流量非常大的对话中,
你可能想动态考虑
更改默认按钮保存
击键并帮助加快用户的速度
任务。

他使用的示例是 Microsoft Outlook 中的“选择名称”对话框,该对话框根据您正在执行的操作更改默认按钮。

Jan Miksovsky has an excellent blog on UI design, and wrote an article about this very thing.

Most UI platforms allow a designer to
indicate which button in a dialog
should be the default button: the
button that will be pressed if the
user types the Enter key. The default
button is generally the button the
user is most likely to press next,
often a button like OK that closes the
dialog. In very high-traffic dialogs,
you may want to consider dynamically
changing the default button to save
keystrokes and help speed the user's
task.

The example he uses is the "Select Names" dialog in Microsoft Outlook, which changes the default button depending on what you are doing.

北凤男飞 2024-08-01 15:09:35

我假设每个用户按钮都是各个用户控件上自己的实例?

如果是这样,那么您可以捕获父窗体上的按钮事件。 如果通过属性公开各个按钮,则可以绑定到它们的 Click 事件。 与所有控件一样,它们也有一个 name 属性,因此您可以拥有一种在所有按钮单击事件上调用的方法。

下面我有部分示例代码。 我有两个用户控件,每个控件都有一个按钮。 UC1 上的按钮命名为“btn1”,UC2 上的按钮命名为“btn2”。 我将公开的属性称为“ButtonOK”,

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public Button ButtonOK
    {
        get { return btn1; }
    }
}

public partial class UserControl2 : UserControl
{
    public UserControl2()
    {
        InitializeComponent();
    }

    public Button ButtonOK
    {
        get { return btn2; }
    }
}

现在在父级(“Form1”)上加载时,它有一个与每个按钮的 Click 事件相关联的方法,但它调用相同的方法。 在方法内部,我测试“Name”属性。

    public Form1()
    {
        InitializeComponent();
    }

    void Form1_Load(object sender, EventArgs e)
    {
        RegisterButtonEvents();
    }

    void RegisterButtonEvents()
    {
        userControl11.ButtonOK.Click += new EventHandler(ButtonOK_Click);
        userControl21.ButtonOK.Click += new EventHandler(ButtonOK_Click);
    }

    void ButtonOK_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        if (btn != null)
        {
            if (btn.Name == "btn1")
            {
                Console.WriteLine(" ButtonOK from UserControl1 was pushed. The tag is " + btn.Tag.ToString());
            }
            else if (btn.Name == "btn2")
            {
                Console.WriteLine(" ButtonOK from UserControl2 was pushed. The tag is " + btn.Tag.ToString());
            }
        }
    }

您还可以使用控件的“Tag”属性。 该属性非常有用,因为它可以引用对象。

您不需要完全按照所示操作,但您可以使用任何“父”表单来获取对 UserControls 的引用,让它们公开其按钮,然后您可以使用这些按钮(或任何按钮)中的属性和事件执行任何您想要的操作对此问题进行控制)。

请记住,如果您还绑定到用户控件上的单击事件(除了父窗体之外),您将必须注意它将枚举委托列表并在单击事件之后执行代码的顺序。事件已启动。

希望有帮助。

I assume each user button is its own instance on the individual user controls?

If so then you can trap the button events on the Parent form. If you expose the individual buttons through a property you can tie into their Click events. Like all controls they have a name property so you can have one method that is called on all button click events.

Below I have a partial sample code. I have two user controls that have one button each. The button on UC1 is named "btn1" and "btn2" for UC2. I call the exposed property "ButtonOK"

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public Button ButtonOK
    {
        get { return btn1; }
    }
}

public partial class UserControl2 : UserControl
{
    public UserControl2()
    {
        InitializeComponent();
    }

    public Button ButtonOK
    {
        get { return btn2; }
    }
}

Now on the parent ("Form1") when it loads have a mthod that ties into the Click events of each button but it calls the same method. Inside the method I test for the "Name" property.

    public Form1()
    {
        InitializeComponent();
    }

    void Form1_Load(object sender, EventArgs e)
    {
        RegisterButtonEvents();
    }

    void RegisterButtonEvents()
    {
        userControl11.ButtonOK.Click += new EventHandler(ButtonOK_Click);
        userControl21.ButtonOK.Click += new EventHandler(ButtonOK_Click);
    }

    void ButtonOK_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        if (btn != null)
        {
            if (btn.Name == "btn1")
            {
                Console.WriteLine(" ButtonOK from UserControl1 was pushed. The tag is " + btn.Tag.ToString());
            }
            else if (btn.Name == "btn2")
            {
                Console.WriteLine(" ButtonOK from UserControl2 was pushed. The tag is " + btn.Tag.ToString());
            }
        }
    }

You can also user the "Tag" property of a control. This property can be very useful as it can reference objects.

You don't need to do exactly as shown but you can use any "Parent" form to get a reference to the UserControls, have them expose their Buttons, then you can do anything you want with properties and events from those Buttons (or any control for that matter).

Keep in mind that if you are tying into the click event on the user control also (in addition to the parent form), you will have to be mindful of the order in which it will enumerate through it list of delegates and execute code after the event is intiated.

Hope that helps.

未央 2024-08-01 15:09:35

我知道这是一篇旧文章,但我想我已经明白了。

在主窗体中的每个用户控件上使用“Enter”事件,以便当用户“进入”(聚焦)用户控件时,this.AcceptButton = myUserControlButton。 您还可以使用每个用户控件上的“离开”事件将接受按钮设置回默认值(如果有的话)。

I know this is an old post, but I think I figured it out.

Use the "Enter" event on each user control from the main form, such that when the user "enters" (focuses on) the user control, this.AcceptButton = myUserControlButton. You can also use the "Leave" event on each user control to set the accept button back to the default, if you have one.

墨小墨 2024-08-01 15:09:35

我不确定我是否正确理解了你的问题。
如果您想将一个事件分配给多个按钮:

为此,您可以:
- 获取 Button_Click 事件上的按钮名称。
- 枚举名称之间
- 迭代控件。

示例如下:
“如何从 Button_Click 事件获取按钮名称”。

// First; dont forget to assign the "Button_Click" event to your Button(s).

private void Button_Click(object sender, EventArgs e)
{
    // The line bellow assigns to "btn" variable the currently clicked button.
    Button btn = (Button)sender;

    // Then using a switch block; you can compare the button name and
    // perform the action desired for the clicked button.
    switch(btn.Name)
    {
        case "buttonName1": /* Do Something Here */ break;
        case "buttonName2": /* Do Something Here */ break;
        // etc
    }
 }

此外; 如果您需要; 总有一种方法可以通过公开按钮来检索表单类之外的按钮。

I'm not sure if I understood your question correctly.
If you want to assign one event to several buttons:

For this you could for instance:
- Get the button name on the Button_Click event.
- Enumerate between names
- Iterate over the controls.

Example bellow:
"How to get the button name from the Button_Click event".

// First; dont forget to assign the "Button_Click" event to your Button(s).

private void Button_Click(object sender, EventArgs e)
{
    // The line bellow assigns to "btn" variable the currently clicked button.
    Button btn = (Button)sender;

    // Then using a switch block; you can compare the button name and
    // perform the action desired for the clicked button.
    switch(btn.Name)
    {
        case "buttonName1": /* Do Something Here */ break;
        case "buttonName2": /* Do Something Here */ break;
        // etc
    }
 }

Additionally; if you require; there's always the way to retrieve the Button outside the form class by exposing them.

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