locked
Selecting text on one form from another? RRS feed

  • Question

  • I'm having a little problem getting the text selected in one form highlighted in another.  Here's the background of the application.  The main from has a Tabed interface with a text box on it filled with a bunch of stuff.  There is a button that launches a "Find" Dialog form.  I pass as a property the TextBox to the Find form.

        Private _txtBoxToSearch As TextBox
        Public Property [txtBoxToSearch]() As TextBox
            Get
                Return _txtBoxToSearch
            End Get
            Set(ByVal Value As TextBox)
                _txtBoxToSearch = Value
            End Set
        End Property

    The my find dialog goes through it's iteration's and find's the text.  I know it's finding the text because I've gone through the debugger and I can see that it is actually matching it in the "found" string...

    Then I get down to the part of my code where I thought I was selecting the code in my text box on the main form, but this just isn't working.  I thought since I had the entire text box passed to me in the find form I could do it here, but it doesn't seem that way.  Here is the code I'm using:

           If targetIndex > -1 And foundFlag = True Then
                _txtBoxToSearch.SelectionStart = targetIndex - 1
                _txtBoxToSearch.SelectionLength = targetLength
                _txtBoxToSearch.ScrollToCaret()
                _txtBoxToSearch.Focus()
            Else
                startAt = 0
            End If

    It executes all the code just fine, but the code on the main form isn't selected.  Any ideas on what I'm doing wrong?  Can I not do it this way?

    Thanks for any input.

    Rodney

    Wednesday, September 26, 2012 12:53 PM

Answers

  • Rodney,

    If you use a dialog (that is a form showed with ShowDialog) there is nothing to search. 

    You can simply get the properties from that as soon as the user clicks OK as long as those are made friend or public.

    Somehow I get the idea you are showing the dialog with show, the problems you tell are then normal and almost impossible to overcome because of the internal OS messaging system. 

    (You see seldom programs like that if thy are not in the MDI range like Office).


    Success
    Cor

    • Marked as answer by RodneyB Wednesday, October 3, 2012 10:20 AM
    Wednesday, October 3, 2012 9:04 AM

All replies

  • I cant really follow your logic in the explanation but i will try and explain what i understood.

    1) on the main form you have a textbox with filled stuff (text)

    2) you have a button 

    3) you have a form called "find"

    You want to select the text in the textbox on the main form and send it to the other form? Is this correctly understood.

    If the idea looks this simple you can pick up in the dialog form the information from the main form as code down below shows.

    Private Sub DialogForm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    Dim textfrom_mainform as string
    
    textfrom_mainform = mainform.textbox1.text
    
    'and here you can do whatever you want with the text
    
    End sub

    If there are several textboxes you need to scan through and check if there are text in them you can follow the same idea.

    I hope it helps ya! Otherwise come back.


    Leif

    Thursday, September 27, 2012 1:15 AM
  • Leif,

    You followed my logic pretty well.  But I'm not following your answer exactly. I used to do it this way, but changed because as I said I have a tab control with a text box on each tab.  I only want to look at the text on the active tab and couldn't figure out how to get the name of the control into my find form so that it would know which was the correct one to scan.

    Rodney

    Thursday, September 27, 2012 10:35 AM
  • you can find the textbox of the your mainForm on another form as :

    Suppose the name of your main form is mainForm and text box name is TextBox1, then you can find this textbox on any other form as:

     For Each frm As Windows.Forms.Form In My.Application.OpenForms()
                If frm.Name = "mainForm" Then
                    For Each ctrl As Control In frm.Controls
                        If TypeOf (ctrl) Is TextBox Then
                            If ctrl.Name = "TextBox1" Then
                                Dim txtbox As TextBox
                                txtbox = ctrl
                                'txtbox is the textbox on the main form.
                            End If
                        End If
                    Next
                End If
            Next

    I have write a very traditional code for finding controls because i am little confuse about your actual requirement.

    Thanks


    Hirendra Sisodiya from authorcode.com

    • Proposed as answer by Mike Feng Friday, September 28, 2012 10:10 AM
    Thursday, September 27, 2012 1:07 PM
  • Hello,

    I think that Hirendras answer is pretty close to what you need. Now that you very good explained what you needed :)

    However as I can see in his code that you do not get the active control that you are looking for.

        Function FindFocussedControl(ByVal ControlCheck As Control) As Control
            Dim container As ContainerControl = TryCast(ctr, ContainerControl)
            Do While (container IsNot Nothing)
                ControlCheck = container.ActiveControl
                container = TryCast(ControlCheck , ContainerControl)
            Loop
            Return ctr
        End Function
    
    'And you can call this function from anywhere:
    'For instance you look at form1 from form1 you refere to it as Me,
    MsgBox(FindFocussedControl(Me).ToString)
    
    'or if you know the control your are looking for you just point to it, like
    MsgBox(FindFocussedControl(form2.textbox1).ToString)

    I hope this will be helpful :)


    Leif

    • Proposed as answer by Leffeq Saturday, September 29, 2012 1:34 AM
    Saturday, September 29, 2012 1:32 AM
  • Leffeq,

    OK, this looks great, but I'm a little confused on the TryCast(ctr, ContainerControl).

    Where is the ctr coming from?  I'm getting a ctr is not defined... did I miss something that I was supposed to do somewhere?

    Thanks

    Rodney

    Tuesday, October 2, 2012 12:33 PM
  • Leffeq,

    OK, this looks great, but I'm a little confused on the TryCast(ctr, ContainerControl).

    Where is the ctr coming from?  I'm getting a ctr is not defined... did I miss something that I was supposed to do somewhere?

    Thanks

    Rodney

    Hi Rodney,

    Based on my understanding, ctr should be "

    ControlCheck 

    "

    Before Leif comes back, it is worth to try.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 3, 2012 5:16 AM
  • I think that the way you were originally doing it was the better approach.   That method is independent of the forms - the Find form will work with any form that needs the find function, which is the preferable way to construct it.

    Just to clarify:

    txtBoxToSearch is a property of the Find form.  In the Find form, this property is save in _txtBoxToSearch.

    In the Main form, there are several text boxes (at least one on each tab).  Therefore, you need to set the text box property of the Find form before searching so that the search occurs in the correct text box.  Your code would be something like this:

      Dim myFindForm As New Find
      myFindForm.txtBoxToSearch = TabControl1.SelectedTab.TextBox1 'or whatever the text box is
      myFindForm.ShowDialog

    The result after the search is that some text should be selected in TabControl1.SelectedTab.TextBox1 (or whatever).

    You have confirmed that the txtBoxToSearch property of the Find form is being correctly set, because the text is being found.

    Therefore the problem is probably happening as the dialog closes and control returns to the main form.  I suspect that the SetFocus is disturbing the selection.

    Firstly, change your original code so that SetFocus occurs before the selection is set.

    If that doesn't solve it, try this:
    Create a property in the Find form that stores the result of the search:

    Private _TargetIndex As Integer
    Private _TargetCount As Integer

    Public ReadOnly Property TargetIndex As Integer
        Get
            Return _TargetIndex
        End Get

    Public ReadOnly Property TargetCount As Integer

        Get
            Return _TargetCount
        End Get

    When the search concludes, update _TargetIndex and _TargetCount with the search results and close the dialog.

    Move the selecting text back into Main, so the code becomes:

            ...
    myFindForm.ShowDialog
    If myFindForm.targetIndex > -1 Then
    myFindForm.txtBoxToSearch.Focus()
    myFindForm.txtBoxToSearch.SelectionStart = myFindForm.TargetIndex - 1 myFindFomr.txtBoxToSearch.SelectionLength = myFindForm.TargetCount myFindForm.txtBoxToSearch.ScrollToCaret() End If

    Of course, you can also use the local reference to the textbox instead of myFindForm.txtBoxToSearch, and your read-only property that returns the search results could be a structure so that it is easily expanded with other fields if required.


    • Edited by Acamar Wednesday, October 3, 2012 7:04 AM sp
    Wednesday, October 3, 2012 6:59 AM
  • Rodney,

    If you use a dialog (that is a form showed with ShowDialog) there is nothing to search. 

    You can simply get the properties from that as soon as the user clicks OK as long as those are made friend or public.

    Somehow I get the idea you are showing the dialog with show, the problems you tell are then normal and almost impossible to overcome because of the internal OS messaging system. 

    (You see seldom programs like that if thy are not in the MDI range like Office).


    Success
    Cor

    • Marked as answer by RodneyB Wednesday, October 3, 2012 10:20 AM
    Wednesday, October 3, 2012 9:04 AM
  • Thank you Cor, something so simple...

    Rodney

    Wednesday, October 3, 2012 10:20 AM