简体   繁体   English

Windows窗体:多个默认按钮?

[英]Windows Forms: Multiple default buttons?

In a simple Guess-The-Number game I'm making, the user inputs a couple of numbers and hits a button. 在我正在制作的一个简单的Guess-The-Number游戏中,用户输入几个数字并按下一个按钮。 A second panel becomes enabled and the original one is disabled. 第二个面板变为启用状态,而原始面板被禁用。 The user now enters a number and hits another button, multiple times. 用户现在输入一个数字并多次点击另一个按钮。 Since the two panels will never be enabled at the same time, I'd like for both buttons to be "default", ie, they will be pushed if Enter is pressed. 由于两个面板永远不会同时启用,因此我希望两个按钮都为“默认”,即,如果按下Enter键,则会将其按下。 Is this possible? 这可能吗? Apparently, I can only set one of these per window. 显然,每个窗口只能设置其中之一。

Nope, no way to accomplish that that I know of. 不,没有办法实现我所知道的。 You'll have to switch the default button when one button is pressed to the next button. 当一个按钮按下时,您必须将默认按钮切换到下一个按钮。

A window in Windows by definition has only one default button. 根据定义,Windows中的一个窗口只有一个默认按钮。 This is a functionality intended for dialog windows where you would never disable the default button. 这是用于对话框窗口的功能,在此您永远不会禁用默认按钮。

What you can do instead is to switch the default button when you disable one panel. 您可以做的是禁用一个面板时切换默认按钮。

Another option would be to throw out default buttons altogether and use KeyPreview on the form and handle the enter key yourself, sending it to the appropriate button that is currently active. 另一种选择是完全丢弃默认按钮,并在窗体上使用KeyPreview并亲自处理Enter键,然后将其发送到当前处于活动状态的适当按钮。

You can only have one default button per window. 每个窗口只能有一个默认按钮。 However, this can be changed at runtime, so when you activate a specific panel, you can make it's button the default one at that point. 但是,可以在运行时更改此设置,因此当您激活特定面板时,可以将其设置为默认按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM