.NET UserControl接受按钮最佳实践?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最佳实践通常是您的表单只有一个接受按钮,以便其行为保持一致。 如果根据表单的哪个部分获得焦点,按回车键会导致不同的操作,那么通常会让用户感到困惑。 但是,如果您有专门的应用程序或用户请求此功能,那么我认为您提出的解决方案是可行的。
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.
Jan Miksovsky 有一篇关于 UI 设计的优秀博客,并写了一篇关于这件事的文章。
他使用的示例是 Microsoft Outlook 中的“选择名称”对话框,该对话框根据您正在执行的操作更改默认按钮。
Jan Miksovsky has an excellent blog on UI design, and wrote an article about this very thing.
The example he uses is the "Select Names" dialog in Microsoft Outlook, which changes the default button depending on what you are doing.
我假设每个用户按钮都是各个用户控件上自己的实例?
如果是这样,那么您可以捕获父窗体上的按钮事件。 如果通过属性公开各个按钮,则可以绑定到它们的 Click 事件。 与所有控件一样,它们也有一个 name 属性,因此您可以拥有一种在所有按钮单击事件上调用的方法。
下面我有部分示例代码。 我有两个用户控件,每个控件都有一个按钮。 UC1 上的按钮命名为“btn1”,UC2 上的按钮命名为“btn2”。 我将公开的属性称为“ButtonOK”,
现在在父级(“Form1”)上加载时,它有一个与每个按钮的 Click 事件相关联的方法,但它调用相同的方法。 在方法内部,我测试“Name”属性。
您还可以使用控件的“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"
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.
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.
我知道这是一篇旧文章,但我想我已经明白了。
在主窗体中的每个用户控件上使用“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.我不确定我是否正确理解了你的问题。
如果您想将一个事件分配给多个按钮:
为此,您可以:
- 获取 Button_Click 事件上的按钮名称。
- 枚举名称之间
- 迭代控件。
示例如下:
“如何从 Button_Click 事件获取按钮名称”。
此外; 如果您需要; 总有一种方法可以通过公开按钮来检索表单类之外的按钮。
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".
Additionally; if you require; there's always the way to retrieve the Button outside the form class by exposing them.