0

I have a first form (form_notice_hashtag) called like this:

Public Sub afficher_hashtag(hashtag As String, plateforme_hashtag As String)
        Dim form_notice_hashtag_1 As New form_notice_hashtag
        form_notice_hashtag_1.StartPosition = FormStartPosition.CenterScreen
        form_notice_hashtag_1.Show()
End Sub

In form_notice_hashtag_1, i have a button calling a 2nd form (form_recherche_thesaurus) like this:

Private Sub hashtag_thesaurus_search_button_Click(sender As Object, e As EventArgs) Handles hashtag_thesaurus_search_button.Click
        Dim form_recherche_thesaurus_1 As New form_recherche_thesaurus With {
            .StartPosition = FormStartPosition.Manual,
            .Location = New Point(Me.Left + Me.Width, Me.Top)
        }
        form_recherche_thesaurus_1.ShowDialog(Me)
End Sub

In form_recherche_thesaurus, i have a datagridview listing some words. The user can select one word, then by clicking a button in form_recherche_thesaurus, the word which will be added to a textbox in form_notice_hashtag

Private Sub thesaurus_ok_button_Click(sender As Object, e As EventArgs) Handles thesaurus_ok_button.Click
        Dim list_terms_array As String()
         Select Case Owner.Name.ToString
            Case "form_notice_hashtag"
                list_terms_array = Split(Remove_Duplicates_From_Strings_With_SemiColon(form_notice_hashtag.hashtag_descripteurs_txtbox.Text & ";" & selected_term), ";")
                form_notice_hashtag.hashtag_descripteurs_txtbox.Text = (String.Join(";", list_terms_array.Where(Function(s) Not String.IsNullOrEmpty(s))))
          End Select
    End Sub

I used a select because this mechanism would be used in the same way with other forms than form_notice_hashtag.

Problem: the textbox in form_notice_hashtag is not filled with the selected keywords. I guess it's because of the way form_notice_hashtag is called.

I can't use the solution as explained here Send values from one form to another form because i understood (maybe badly) that this solution works only if the 2nd form (form_recherche_thesaurus in my case) is closed (i.e closing was the trigger) which i don't want.

How can I proceed?

8oris
  • 320
  • 2
  • 12
  • 1
    The final form should not know anything about the caller. It should simply expose the data via a public property and that is it. It is then the responsibility of the calling form to get the required data from that property. There is then no need for a `Select Case` because each different calling form knows exactly how it needs to use the data. I suggest that you read all three parts of [this](http://jmcilhinney.blogspot.com.au/2012/04/managing-data-among-multiple-forms-part.html) blog post of mine. – jmcilhinney Apr 27 '21 at 09:41
  • Thanks, your article is very clear and well-written for noobs like me (but the example in the third part was a bit tricky). I used the solution in part 3. – 8oris Apr 27 '21 at 10:29

1 Answers1

0

Thanks to jmcilhinney and this page of his blog, here is the solution that allows to transfer several data from a called form (form_recherche_thesaurus) to a calling form (form_notice_hashtag) without closing the called form .

Public Class form_notice_hashtag
    Private WithEvents form_recherche_thesaurus_1 As form_recherche_thesaurus
    Private selected_thesaurus_term As String
    
    Private Sub form_recherche_thesaurus_1_TextBoxTextChanged(sender As Object, e As EventArgs) Handles form_recherche_thesaurus_1.TextBoxTextChanged
        Dim list_terms_array As String() = Split(Remove_Duplicates_From_Strings_With_SemiColon(Me.hashtag_descripteurs_txtbox.Text & ";" & form_recherche_thesaurus_1.selected_term), ";")
        Me.hashtag_descripteurs_txtbox.Text = (String.Join(";", list_terms_array.Where(Function(s) Not String.IsNullOrEmpty(s))))
    End Sub
    
    Private Sub hashtag_thesaurus_search_button_Click(sender As Object, e As EventArgs) Handles hashtag_thesaurus_search_button.Click
        Dim form_recherche_thesaurus_1 As New form_recherche_thesaurus With {
            .StartPosition = FormStartPosition.Manual,
            .Location = New Point(Me.Left + Me.Width, Me.Top)
        }
        If Me.form_recherche_thesaurus_1 Is Nothing OrElse Me.form_recherche_thesaurus_1.IsDisposed Then

            Me.form_recherche_thesaurus_1 = New form_recherche_thesaurus With {
            .StartPosition = FormStartPosition.Manual,
            .Location = New Point(Me.Left + Me.Width, Me.Top)
        }
            Me.form_recherche_thesaurus_1.Show()
        End If

        Me.form_recherche_thesaurus_1.Activate()
    End Sub

End Class
Public Class form_recherche_thesaurus
    Public Event TextBoxTextChanged As EventHandler
    Private term_thesaurus As String
    
    Public Property selected_term() As String
        Get
            Return term_thesaurus
        End Get
        Set(ByVal value As String)
            term_thesaurus = value
        End Set
    End Property
    
    Private Sub thesaurus_ok_button_Click(sender As Object, e As EventArgs) Handles thesaurus_ok_button.Click
        Dim list_terms_array As String()        
        Me.selected_term = Me.thesaurus_search_results_datagrid.Item(0, Me.thesaurus_search_results_datagrid.CurrentRow.Index).Value
        Me.DialogResult = DialogResult.OK
        RaiseEvent TextBoxTextChanged(Me, EventArgs.Empty)
    End Sub
8oris
  • 320
  • 2
  • 12