2016-11-13 2 views
0

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를 반환한다는 것을 보여줍니다.

도움을 주시면 감사하겠습니다.

답변

1

흠. 이것은 내가 생각했던 것보다 더 까다 롭다. 단지 형태 1 텍스트 상자와 "찾기"

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 
+0

CanFocus()에서 여전히 False를 반환하고 강조 표시하지 않습니다. Visual Studio에서 frmNotes를 frmNotes로 변경 했으므로 frmNotes를 Me로 변경했습니다. – abacles

+0

정확합니다. 제 원래 대답이 작동하지 않았습니다. 나는 나의 대답을 편집했다. – SSS

+0

정말 고마워요! frmFind에서 frmNotes를 "부모"로 만드는 핵심 포인트입니까, 아니면 내가 놓친 것이 있습니까? – abacles

0
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 

이것은 :

기본 양식 :

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 

찾기 양식 당신은 다른 형태에 대한 참조를 전달해야 1 단추는 다른 양식의 txtNotes에서 찾은 TextBox1에서 문자열의 첫 번째 항목을 강조 표시합니다. 공백을 찾으려면 Trim 기능을 제거하십시오. 다른 발생을 찾거나 앞뒤로 이동하는 코드를 추가 할 수 있습니다.

+0

이 방법을 시도했지만 아무 일도 일어나지 않습니다. 다른 양식에서도 여전히 강조 표시되지 않습니다. CanSelect()와 CanFocus() 모두 False입니다. – abacles

+0

CanSelect와 CanFocus는 내 솔루션과 아무 관련이 없으며 필요하지 않습니다. 원래의 frmNotes (메인 노트 패드 양식)에서 frmFind.Show()를 호출합니다. 위의 폼에 대한 코드입니다. 편집 결과가 맨 위에 엉망이되어서 지금 고칠 것입니다. TextBox1 및 btnFind를 추가하십시오. – topshot

+0

사실 fmrNotes의 유일한 코드는 간단히하기 위해 버튼 클릭 이벤트에있는'frmFind.Show()'입니다. 당신은 메뉴 띠 또는 핫키 조합에서 가장 가능성이 높습니다. – topshot