The Dialog Form remembers last button focus. How to reset it?

前端 未结 1 1874
挽巷
挽巷 2020-12-20 00:44

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 previousl

相关标签:
1条回答
  • 2020-12-20 01:31

    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.

    0 讨论(0)
提交回复
热议问题