【问题标题】:Cannot set focus to textbox无法将焦点设置到文本框
【发布时间】:2016-11-13 23:57:23
【问题描述】:

我正在使用 VB 并尝试在单独表单的文本框中选择部分文本。但是,我似乎找不到从其他表单访问文本框的好方法,尽管文本框是公共的(我是 VB 新手)。

目前,我正在尝试通过调用位于表单(带有文本框的表单)中的函数来执行此操作,然后专注于文本框并选择/突出显示文本。但是还是不行:

    Public Sub GetFindLoc(ByVal lngStart As Long, ByVal intLen As Integer)
        frmFind.Hide()
        MessageBox.Show(ActiveForm.Name)
        MessageBox.Show(txtNotes.CanFocus())
        txtNotes.Focus()
        txtNotes.Select(lngStart, intLen)
        frmFind.Show()
    End Sub

有了这个,我先把原来的表格隐藏起来,然后试着选择文字,把表格带回来。它表明活动表单是我试图选择文本的表单,但它在 CanFocus() 上返回 false。

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: vb.net winforms textbox focus highlight


    【解决方案1】:

    嗯。这比我想象的要复杂。您需要传递对其他表单的引用:

    主要形式:

    Public Class frmNotes
      'This is the main form
      'This form has a textbox named txtNotes and a button called btnShowFind
      'txtNotes has .MultiLine=True
    
      Private mfrmFind As frmFind
      Private Sub btnShowFind_Click(sender As Object, e As EventArgs) Handles btnShowFind.Click
        If mfrmFind Is Nothing OrElse mfrmFind.IsDisposed Then
          mfrmFind = New frmFind(Me)
          mfrmFind.Show()
        Else
          mfrmFind.BringToFront()
        End If
      End Sub
    End Class
    

    查找器表单:

    Public Class frmFind
      'This form has a textbox called txtFind and a button called btnFind
      Private mfrmParent As frmNotes
      Sub New(parent As frmNotes)
    
        ' This call is required by the designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        mfrmParent = parent
      End Sub
    
      Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
        If txtFind.Text = "" Then
          MsgBox("Please enter text to find", MsgBoxStyle.Exclamation)
          Exit Sub
        End If
        Dim intSearchBegin As Integer = mfrmParent.txtNotes.SelectionStart + 1
        Dim intStart As Integer = mfrmParent.txtNotes.Text.IndexOf(txtFind.Text, intSearchBegin)
        If intStart > -1 Then
          mfrmParent.txtNotes.Select(intStart, txtFind.Text.Length)
          mfrmParent.txtNotes.Focus()
          mfrmParent.BringToFront()
        Else
          mfrmParent.txtNotes.Select(0, 0)
          MsgBox("No more matches")
        End If
      End Sub
    End Class
    

    【讨论】:

    • 它仍然在 CanFocus() 上返回 False 并且没有突出显示 -- Visual Studio 让我将 frmNotes 更改为 Me,因为这个函数是在 frmNotes 中声明的。
    • 你是对的,我原来的答案没有奏效。我已经编辑了我的答案。
    • 非常感谢您的工作!这里的关键点是在 frmFind 中将 frmNotes 设为“父级”还是我错过了什么?
    • ClassObject 的定义。 ObjectClass 的运行时实例。 Class 是千篇一律,Object 是 Cookie。 VB 简化了Controls 的操作,允许您通过类名引用对象(这就是GenerateMember 属性的意义所在)。我想你可以使用它们的类名来引用Form 对象,但看起来你不能再这样做了。所以这里我们需要明确并提供对父表单的引用,而不仅仅是使用它的类名。
    • 非常感谢您的澄清!
    【解决方案2】:
    Public Class frmFind
    
        Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
    
            Dim search As String = TextBox1.Text.Trim
            Dim pos As Integer = frmNotes.txtNotes.Text.IndexOf(search)
    
            If pos > 0 Then
                frmNotes.txtNotes.Focus()
                frmNotes.txtNotes.Select(pos, search.Length)
            End If
    
        End Sub
    
    End Class
    

    这只是一个带有 1 个文本框和 1 个按钮的“查找”表单,它将突出显示它在另一个表单的 txtNotes 中找到的 TextBox1 中第一次出现的字符串。如果您希望它也能找到空格,请删除 Trim 函数。您可以添加代码以查找其他事件或前进/后退。

    【讨论】:

    • 我已经尝试过这种方法,但没有任何反应,它仍然没有在其他表单上突出显示。 CanSelect() 和 CanFocus() 都是 False
    • 您会注意到CanSelectCanFocus 与我的解决方案无关,也不需要它们。从最初的 frmNotes(即记事本主表单),您只需调用 frmFind.Show(),我在上面显示的该表单的代码虽然我看到我的编辑弄乱了它的顶部,所以我现在将修复它。添加 TextBox1 和 btnFind。
    • 事实上,fmrNotes 上的唯一代码是 frmFind.Show(),为了简单起见,我在按钮单击事件中使用了该代码。您很可能会将它放在菜单条或热键组合中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 2014-04-24
    • 1970-01-01
    相关资源
    最近更新 更多