0

我在网上搜索了这个问题的答案,但找不到。这似乎是一个很常见的问题,我很困惑找不到任何东西。

我有 3 个表格 A 表格,用户将使用该表格;表格 B 用于选择要显示的表格 C 的菜单;表格 C 数据库中的数据“快速列表”,以帮助输入表格 A。这些表格 C 中有几个可以在会话期间显示。

  • 用户在表单 A 上工作时可以按 control-Q,这将显示表单 B 菜单。

  • 控制自动传递到表单 B,用户按下一个键,所需的表单 C 弹出,表单 B 关闭。

  • 控制权传递给表单 C,它是“活动”表单。

表格 C 上没有要填写的内容,所以我想自动返回表格 A 上的最后一个活动控件。

一切正常,但我最终以表格 C 作为活动表格。

如何强制控制回到表单 A 上的最后一个活动控件?

4

2 回答 2

0

OK I got it. And there were several issues to solve. Not sure if this is the best way but it does work.

The first problem is to be able to refer to the form you want to have focus from some other form. I overloaded the ‘show’ event for both Form B and Form C to be able to pass the original calling form. When I called Form B from Form A, I supplied a reference to Form A. Then when I called Form C I passed the reference to Form A again. At that point both Form B and Form C knew who the originator was.

I tried to set the focus back to Form A within Form C. So in both Form B and Form C, I would set a variable (Dim ‘callingform’ as Form). In each of Form B and C I used the following:

Dim callingform as Form

Overloads Sub Show(ByVal f1 As Form)

Callingform = f1

So each of Form B and Form C had a reference to the original Form A that was to eventually get the focus.

But you can’t refer to the form with that variable. The following is a proper reference to the form and ‘focus’ is the method to change focus.

CType(callingform, windows.forms.form).focus()

That statement seems straightforward enough but I was trying to use it in the wrong place. I was using that statement at the end of the ‘Load’ event for Form C. The problem there is that Form C hasn’t yet gotten focus so executing that statement would be subsequently overridden as Form C was actually displayed. I didn’t know then but do now, Focus isn’t transferred to the new form until AFTER the load event. So if you put that statement in the Form C ‘GotFocus’ event it works like a charm.

After considering things for awhile, I’ve decided that Form C might be called from anywhere and it shouldn’t be deciding where focus should be set. It should be decided by whoever decided to Display Form C.

So I have put the following in the end of Form B’s Menu selection code:

FormC.Show(formA) ‘Display Form C and pass the originating form)

CType(formA, windows.forms.form).focus() ‘Change focus to FormA, the originating form

formB.Close (actually - me.close) - ‘ Close up the menu form, FormB

(Aside: I don’t really understand why the ‘ctype’ is necessary. I passed the form as a ‘form’ and defined the variable ‘callingform’ as a form. I expected to be able to just say callingform.focus(). )

于 2013-04-17T21:39:07.603 回答
0

这是我的答案的相关代码。

部分代码:在表格 A 中(原始调用表格)

If CurMode = Browse Then

  'Check for Quick List menu CRTL-Q
  If e.Control AndAlso e.KeyCode = Keys.Q Then

   ' Call the 'Menu form, FormB
    CType(Me.MdiParent, frmRECORDmain).ShowQuickListMenu(Me, Me.ActiveControl)
    Exit Sub
  End If

在 QuickList Menu 表单中(原始表单 B)

Public Class frmQuickListMENU
Dim CallingForm As Form
Dim CallingControl As Control
Dim Selection As String

' This is so that Form B will know who Form A was.
Overloads Sub Show(ByVal f As Form,
                 ByVal c As Control,
                 Optional PreSelection As String = "")
CallingForm = f
CallingControl = c
Selection = PreSelection
Show()
End Sub

Private Sub frmQuickListMENU_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
  Case Keys.D1, Keys.NumPad1
  . .       
  Case Keys.D3, Keys.NumPad3
  . . .
  Case Keys.D6, Keys.NumPad6
  . . .
  ' One of 12 Menu choices which will open 1 of 12 Form C's
  Case Keys.D7, Keys.NumPad7
    Dim frmquickListPHD1 As New frmQuickListPHD()
    frmquickListPHD1.MdiParent = MasterParentForm
    frmquickListPHD1.StartPosition = FormStartPosition.Manual
    frmquickListPHD1.Location = New Point(QLPHD.Left, QLPHD.Top)
    ' Show Form C  
    frmquickListPHD1.Show(CallingForm, CallingControl, Me)
End Select
'    ************************
'    ************************
'    THE NEXT LINE IS THE 'MAGIC' I WAS LOOKING FOR
'    This line returnbs focus back to the original Form A
'    ************************
CType(CallingForm, Windows.Forms.Form).Focus()
Me.Close()
End Sub

Private Sub frmQuickListMENU_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  Dim x1 As Integer
  x1 = Me.Width
End Sub
End Class

最后是“快速列表”的代码 - 表格 C 但是,由于我决定更改表格 B 代码而不是表格 C 代码中的焦点,因此表格 C 代码都无关紧要。我只表明它与我的原始帖子一致。

  Dim callingform As Form
  Dim callingcontrol As Control
  Dim MenuForm As Form


' The following was initially required so that the change of focus could be made within this 
' Class but I have since changed my mind and the Overload is unnecessary.  I only left 
' this here  so the code would be consistent with my original note.  I'll  be removing
' thisOverload of the 'Show' event. 
Overloads Sub Show(ByVal f1 As Form,
                   ByVal c As Control,
                   ByVal f2 As Form)
  callingform = f1
  MenuForm = f2
  callingcontrol = c
  Show()
End Sub

Private Sub frmQuickListPHD_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Try
  conn.Open()
  DA.SelectCommand = New SqlCommand(SQL, conn)
  ' 
'
'
Catch ex As Exception
  Console.WriteLine("Error" & ex.ToString)
Finally
  conn.Close()
End Try
Me.Height = QLPHD.Height
Me.Width = QLPHD.Width
'
'
'  More form layout code
'
' 
'
 End Sub
End Class
于 2013-04-18T03:11:27.450 回答