检查动态创建的richtextbox是否不在焦点上

时间:2012-05-08 05:29:19

标签: vb.net winforms

如果创建字典程序。当使用在剪贴板上复制一些文本时,它将在系统尝试中以可见的形式给出复制文本的含义。我想在用户点击他/她的屏幕上的任何位置时关闭表单。但是如果用户想要从意义中复制一些文本将不会关闭我在选项卡控件中添加了多个动态创建的richtextbox,以显示含义的数量。当用户滚动richtextboxes表单时,我的代码工作得非常好。看来滚动条不是richtextbox的一部分。帮我解决这个问题我的代码如下。

Dim s As Boolean = True

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If MouseButtons.ToString = "Left" Or MouseButtons.ToString = "Right" Then
        If s = True Then
            If InStr(LCase(Me.ActiveControl.ToString), LCase("Label")) Then
                Me.Close()
            End If
        End If
    End If
End Sub

Private Sub Label1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Label1.Click
    Me.Close()
End Sub
Private Sub frmdict_MouseEnter(ByVal sender As Object, ByVal e As EventArgs) Handles Me.MouseEnter
    s = False
    Button1.Focus()
End Sub

Private Sub frmdict_MouseLeave(ByVal sender As Object, ByVal e As EventArgs) Handles Me.MouseLeave
    s = True
    Label1.Focus()
End Sub

==更新==

我有form1,其中包含此代码以向tabcontrl

添加richtextboxes和tabs
Dim myTabPage As New TabPage()
Dim myrichtext As New RichTextBox()
myrichtext.Name = "RichTextBox" & i
myTabPage.Text = StrSearch & i
frmdict.TabControl1.TabPages.Add(myTabPage)
myTabPage.Controls.Add(myrichtext)
myrichtext.RightToLeft = Windows.Forms.RightToLeft.No
myrichtext.Dock = DockStyle.Fill
myrichtext.Font = New Font("Urdulink", 14)

最后打开frmdict来表示意思

If frmdict.TabControl1.TabPages.Count > 0 Then
  frmdict.TabControl1.RightToLeftLayout = True
  frmdict.Show()
  frmdict.Label1.Focus()
  ' frmdict.TabControl1.Focus()
Else
  frmdict.Close()
End If

1 个答案:

答案 0 :(得分:0)

只要鼠标越过表单的某个子控件,您的MouseLeave就会触发,这可能不是您所期望的。

我不确定你的标签和按钮是怎么回事,但这样的事情可能会让你觉得有用:

If s = True Then
  If Not rtb1.Bounds.Contains(Me.PointToClient(Cursor.Position)) AndAlso _
     TypeOf Me.ActiveControl Is Label Then
    Me.Close()
  End If
End If

<强>更新

就动态富文本控件而言,您并不需要该名称。这样的事情应该有效(没有经过全面测试):

If TabControl1.SelectedTab IsNot Nothing Then
  For Each rtb As RichTextBox In TabControl1.SelectedTab.Controls.OfType(Of RichTextBox)()
    If Not rtb.Bounds.Contains(rtb.PointToClient(Cursor.Position)) Then
      Me.Close()
    End If
  Next
End If