对话框窗体记得最后一个按钮的焦点。如何重置呢? [英] The Dialog Form remembers last button focus. How to reset it?

查看:196
本文介绍了对话框窗体记得最后一个按钮的焦点。如何重置呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的形式是开放为Form.ShowDialog()

这种形式作为一个确认单。它问一个问题,你是否想接受或拒绝previously输入的输入的组合框和放大器;文本框。

如果您单击确定,输入被保存到Excel文件。

如果您单击取消,输入不会被保存。

我遇到的问题是:

当您单击取消。所述form.ShowDialog()是封闭的。 (这很好。)

但是,当form.ShowDialog()恢复通车。它保留焦点上的取消按钮。所以,如果你试图确认条目Enter键,就取消吧。

我的问题是。为何Form.ShowDialog()保留的焦点上的按钮关闭后?

在Form.ShowDialog()已接受OK按钮[的tabindex = 1],并取消按钮取消[的tabindex = 2],它被设置为<大骨节病>输入键,<大骨节病> Esc键键。

(要再次注)按钮的重点仍然关闭窗体后。

的code。使用对话框的部分:

  elseif的ComboBoxBP.SelectedItem = ComboBoxBP.SelectedItem而TextBoxBP.Text = TextBoxBP.Text然后

        form.Label1.Text = ComboBoxBP.SelectedItem
        form.Label2.Text = TextBoxBP.Text
        form.ShowDialog()

        如果form.DialogResult = Windows.Forms.DialogResult.Yes然后

            SIE()

        elseif的form.DialogResult = Windows.Forms.DialogResult.No然后

            LabelBPBot.Text =取消。

        结束如果

    结束如果
 

解决方案

在使用 .ShowDialog()关闭窗体不处理它与通常的形式。这是因为一旦一个对话框关闭它实际上只是隐藏,所以我们可以从它那里得到的信息才真正消失。

第二个问题是,表格类(它是这么说的,在他们每个人的顶部:)

 公共类Form1中
    ...
 

因此​​,实例的人应该被创建。 VB允许 Form1.Show Form1.ShowDialog()来使用默认实例,这是一种耻辱,它的作用。

结合这2花絮和你有这样的情况,你显示,上一次的形式仍是各地的在相同的状态的是,当你最后一次使用它,包括最后的集中控制。您只使用一个全新副本形式的第一次,在那之后,你只是重复使用旧的实例。解决方法:

 使用了Dlg作为新Form1的form1的是类,DLG是实例
   ...做的东西

   昏暗的水库作为的DialogResult = Dlg.ShowDialog()

   如果解析度= Windows.Forms.DialogResult.OK然后
       ......做的东西
   结束如果

使用完处置多元学习津贴
 

最后,你会遇到使用的其他形式的默认实例类似的问题( LForm.Show )。只是说没有到默认窗体实例。

I have a custom form which is open as Form.ShowDialog()

This form acts as a confirmation form. It asks a question whether you want to accept or decline the previously entered input in ComboBox & TextBox.

If you click OK, the input is saved into Excel File.

If you click Cancel, the input is not saved.

The problem I am having is that:

When you click cancel. The form.ShowDialog() is closed. (Which is fine.)

But when the form.ShowDialog() is open again. It retains the focus on the Cancel Button. So if you try to confirm the entry with "Enter" key, you cancel it instead.

My question is. Why does the Form.ShowDialog() retain the focus on the buttons after closing?

The Form.ShowDialog() has accept button "OK" [tabindex = 1], and cancel button "Cancel" [tabindex = 2] which are set to Enter key, and Esc key.

(To note again)The focus of the buttons remains after closing the form.

The portion of the code using the Dialog:

    ElseIf ComboBoxBP.SelectedItem = ComboBoxBP.SelectedItem And TextBoxBP.Text = TextBoxBP.Text Then

        form.Label1.Text = ComboBoxBP.SelectedItem
        form.Label2.Text = TextBoxBP.Text
        form.ShowDialog()

        If form.DialogResult = Windows.Forms.DialogResult.Yes Then

            SiE()

        ElseIf form.DialogResult = Windows.Forms.DialogResult.No Then

            LabelBPBot.Text = "Canceled."

        End If

    End If

解决方案

When you use .ShowDialog() closing the form does not dispose of it as with a normal form. This is because once a Dialog "closes" it actually just hides so we can get info from it before it actually goes away.

The second issue is that forms are classes (it says so at the top of every one of them:)

Public Class Form1
    ...

So, instances of them should be created. VB allows Form1.Show or Form1.ShowDialog() to use a "default instance" and it is a shame that it does.

Combine these 2 tidbits and what you have is a case where the form you showed last time is still around in the same state as when you last used it, including the last focused control. You are only using a "fresh copy" of the form the first time, after that, you are just reusing the old instance. Remedy:

Using Dlg As New Form1             ' form1 is the class, dlg is the instance
   ... do stuff

   Dim res As DialogResult = Dlg.ShowDialog()

   If res = Windows.Forms.DialogResult.OK Then
       '... do stuff
   End If

End Using                          ' dispose of Dlg

Eventually, you will run into similar issues using the default instance of the other forms (LForm.Show). Just Say No to Default Form instances.

这篇关于对话框窗体记得最后一个按钮的焦点。如何重置呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆