在表单点击上捕获焦点事件

时间:2019-07-18 15:05:16

标签: vb.net winforms

我在VB.Net 2015项目中有三种形式,都需要一起移动。用户可以单击并拖动三个中的任何一个,其他两个应随它们一起移动。我的动作很慢,每种形式都有代码来指导其他形式。但我在努力确定应采用哪种形式为主(因为如果其他两种都不是,则其他两种不应实现其Move()代码)。

我的想法是建立一个全局String变量,在单击表单时将其加载为表单名称,并在发布时将其清除。每个表单都会检查它是否具有move()事件的优先级,如果没有,它将忽略它。

但是我似乎无法弄清楚如何重点识别表单。

我尝试了MouseDown和MouseUp事件,Click事件以及其他一些事件,但是没有触发。我了解到,如果表单上具有任何可见或启用的控件,它们将不会对这些事件做出反应。但是除了Move()事件之外,我必须要使用某些东西来帮助解决这个问题?

Private Sub frm1_Move(sender As Object, e As EventArgs) Handles Me.Move

    ' Causes the Product List and Event Monitor windows to move with the Induct Status form when it is moved  CJB 6/12/2019
    ' Global variable for new form movement -- gsTriFormFocus (String) -- Place Form Name in field when Form MouseDown event fires, clear with "" when MouseUp event fired  ' CJB 6/24/2019

    If gsTriFormFocus <> "frm1" Then
        Exit Sub
    Else

        Dim frm As New Form

        frm.Tag = "frm2"
        If gcolProjectForm.Count > 0 Then
            For i As Integer = 1 To gcolProjectForm.Count
                If gcolProjectForm(i).tag.ToString.ToUpper = (frm.Tag.ToUpper) Then
                    gcolProjectForm(i).Left = Me.Left
                    gcolProjectForm(i).Top = Me.Top + Me.Height + 10
                    Exit For
                End If
            Next
        End If

        frm.Tag = "frm3"
        If gcolProjectForm.Count > 0 Then
            For i As Integer = 1 To gcolProjectForm.Count
                If gcolProjectForm(i).tag.ToString.ToUpper = (frm.Tag.ToUpper) Then
                    gcolProjectForm(i).Left = Me.Left + Me.Width + 10
                    gcolProjectForm(i).Top = Me.Top
                    Exit For
                End If
            Next
        End If
    End If

End Sub

2 个答案:

答案 0 :(得分:1)

使用LocationChanged事件处理程序来处理此问题:

Private frm2 As New Form2
Private frm3 As New Form3

Public Sub New()
  InitializeComponent()
  AddHandler frm2.LocationChanged,
    Sub()
      Me.Location = New Point(frm2.Left - Me.Width, frm2.Top)
    End Sub
  frm2.Show()

  AddHandler frm3.LocationChanged,
    Sub()
      Me.Location = New Point(frm3.Left - Me.Width, frm3.Top - frm2.Height)
    End Sub
  frm3.Show()
End Sub

Protected Overrides Sub OnLocationChanged(e As EventArgs)
  MyBase.OnLocationChanged(e)
  frm2.Location = New Point(Me.Left + Me.Width, Me.Top)
  frm3.Location = New Point(frm2.Left, frm2.Top + frm2.Height)
End Sub

答案 1 :(得分:1)

在我看来,您希望frm2下方是frm1,而frm3右侧是frm1

这是frm1的代码:

Public Class frm1

    Public Shared Ignore As Boolean = False

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim f2 As New frm2
        f2.Show()

        Dim f3 As New frm3
        f3.Show()

        UpdateForms(Me)
    End Sub

    Private Sub frm1_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
        UpdateForms(Me)
    End Sub

    Private Sub frm1_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
        UpdateForms(Me)
    End Sub

    Public Shared Sub UpdateForms(ByVal sourceForm As Form)
        If Not Ignore Then
            Ignore = True

            Dim f1 As frm1 = Application.OpenForms.OfType(Of frm1).FirstOrDefault
            Dim f2 As frm2 = Application.OpenForms.OfType(Of frm2).FirstOrDefault
            Dim f3 As frm3 = Application.OpenForms.OfType(Of frm3).FirstOrDefault

            If sourceForm Is f1 Then
                If Not IsNothing(f2) Then
                    f2.Location = New Point(f1.Bounds.Left, f1.Bounds.Bottom + 10) ' below frm1
                End If
                If Not IsNothing(f3) Then
                    f3.Location = New Point(f1.Bounds.Right + 10, f1.Bounds.Top) ' to the right of frm1
                End If
            ElseIf sourceForm Is f2 Then
                If Not IsNothing(f1) Then
                    f1.Location = New Point(f2.Bounds.Left, f2.Bounds.Top - f1.Bounds.Height - 10) ' above frm2
                End If
                If Not IsNothing(f3) AndAlso Not IsNothing(f1) Then
                    f3.Location = New Point(f1.Bounds.Right + 10, f1.Bounds.Top) ' to the right of frm1
                End If
            ElseIf sourceForm Is f3 Then
                If Not IsNothing(f1) Then
                    f1.Location = New Point(f3.Bounds.Left - f1.Bounds.Width - 10, f3.Bounds.Top) ' to the left of frm3
                End If
                If Not IsNothing(f2) AndAlso Not IsNothing(f1) Then
                    f2.Location = New Point(f1.Bounds.Left, f1.Bounds.Bottom + 10) ' below frm1
                End If
            End If

            Ignore = False
        End If
    End Sub

End Class

然后frm2frm3更简单:

Public Class frm2

    Private Sub frm2_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
        frm1.UpdateForms(Me)
    End Sub

End Class

Public Class frm3

    Private Sub frm3_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
        frm1.UpdateForms(Me)
    End Sub

End Class