Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to pass values between 2 mdi childs?(without new form)

How to pass values between 2 mdi childs?(without new form)

Scheduled Pinned Locked Moved Visual Basic
questioncssalgorithmshelptutorial
14 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D DaveAuld

    You could create a public method on formb that returns the value you want. Then call it from forma.

    Dave Find Me On: Web|Facebook|Twitter|LinkedIn


    Folding Stats: Team CodeProject

    D Offline
    D Offline
    DeDelva
    wrote on last edited by
    #4

    Hey, First of all thx for the reply,reallllly appreciate that :) Second my apologizes if i'm working on your nerve system. I'm an advanced noob with vb.net But can somebody explain it with some example's? I checked the event tutorial but there must be a faster way no?(don't like to add classes for a single transport). So i like the public method more, but can you show me an example how i should write the method in form b and how to access my values in form a? Thanks again

    D 1 Reply Last reply
    0
    • D DeDelva

      Hey, First of all thx for the reply,reallllly appreciate that :) Second my apologizes if i'm working on your nerve system. I'm an advanced noob with vb.net But can somebody explain it with some example's? I checked the event tutorial but there must be a faster way no?(don't like to add classes for a single transport). So i like the public method more, but can you show me an example how i should write the method in form b and how to access my values in form a? Thanks again

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #5

      You could do something like this; Form 2 Code (The one you want to get some value from):

      Private aValue as String = "Hello World!"

      Public Function GetTheValue() As String

      return aValue

      End Function

      Form 1 Code (The one wanting the value, code inside a button or where ever)

      Public Sub DoSomething
      Dim theOtherForm as new Form1
      MessageBox(theOtherForm.GetTheValue)
      End Sub

      Dave Find Me On: Web|Facebook|Twitter|LinkedIn


      Folding Stats: Team CodeProject

      D 1 Reply Last reply
      0
      • D DaveAuld

        You could do something like this; Form 2 Code (The one you want to get some value from):

        Private aValue as String = "Hello World!"

        Public Function GetTheValue() As String

        return aValue

        End Function

        Form 1 Code (The one wanting the value, code inside a button or where ever)

        Public Sub DoSomething
        Dim theOtherForm as new Form1
        MessageBox(theOtherForm.GetTheValue)
        End Sub

        Dave Find Me On: Web|Facebook|Twitter|LinkedIn


        Folding Stats: Team CodeProject

        D Offline
        D Offline
        DeDelva
        wrote on last edited by
        #6

        Thx 4 the reply, Your solution works but not how i wanted... avalue = "hello world" if i load the form where i want to get my data from i do: avalue = "changethevaluethatiwanttohave" so i load that form and avalue is changed (on the form) then if i go to the form where i want to get my data in i do : GetTheValue() but still giving me "hello world" back instead of changetevaluethatiwanttohave... How come?because avalue is declared global but if i change my global var it wil not change when a call the function in the other form?? regards

        D 1 Reply Last reply
        0
        • D DeDelva

          Thx 4 the reply, Your solution works but not how i wanted... avalue = "hello world" if i load the form where i want to get my data from i do: avalue = "changethevaluethatiwanttohave" so i load that form and avalue is changed (on the form) then if i go to the form where i want to get my data in i do : GetTheValue() but still giving me "hello world" back instead of changetevaluethatiwanttohave... How come?because avalue is declared global but if i change my global var it wil not change when a call the function in the other form?? regards

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #7

          This is occurring because you are creating a new form each time. You would need to find the form you want to work worth and then call its method. If you have a number of forms open, you could use the ApplicationOpenForms to find the reference e.g.

              For Each theForm As Form In Application.OpenForms
          
                  If theForm.Name = "FormOrder" Then
                      MessageBox(theForm.GetSomeValue)
                  End If
          
              Next
          

          Or if you know the instance of the form exists you can just reference it directly;

          Dim theForm as Form2 ' Note No NEW
          MessageBox(theForm.GetTheValue)

          You would want to do some error checking to make sure you do end up with a reference to the target form and not a null.

          Dave Find Me On: Web|Facebook|Twitter|LinkedIn


          Folding Stats: Team CodeProject

          D 1 Reply Last reply
          0
          • D DaveAuld

            This is occurring because you are creating a new form each time. You would need to find the form you want to work worth and then call its method. If you have a number of forms open, you could use the ApplicationOpenForms to find the reference e.g.

                For Each theForm As Form In Application.OpenForms
            
                    If theForm.Name = "FormOrder" Then
                        MessageBox(theForm.GetSomeValue)
                    End If
            
                Next
            

            Or if you know the instance of the form exists you can just reference it directly;

            Dim theForm as Form2 ' Note No NEW
            MessageBox(theForm.GetTheValue)

            You would want to do some error checking to make sure you do end up with a reference to the target form and not a null.

            Dave Find Me On: Web|Facebook|Twitter|LinkedIn


            Folding Stats: Team CodeProject

            D Offline
            D Offline
            DeDelva
            wrote on last edited by
            #8

            Dave, Thanks again you really are a patient man right?... Anyway, I can find the form and i know that it exists i will pass some code to give you a better view before i stop working with mdi containers.(last question hehe). what i realllllyy need to accomplish is this: Form A is a list of costumers where i can fill in textboxes with data. If i want to change a costumers data i click on a button search. This will go to form B where i can search through a datagridview of custumers.If i click a celrow the row data has to go to form A in the txt's. Form B code:

            If Not (e.RowIndex = -1) Then
            DataGridView1.Rows(e.RowIndex).Selected = True
            End If
            i = DataGridView1.CurrentRow.Index
            formA.TextBox1.Text = DataGridView1.Item(0, i).Value.ToString()

            enso.... simply explained is that i need a formA.show() before it will pass the dgvitems in the txt. I work between 2 mdichildrens so i cannot do this, also not possible the work with new because the form is already opened. Can somebody help me out of this misery??? :( Sorry for bothering greetz & thx dave;)

            D 1 Reply Last reply
            0
            • D DeDelva

              Dave, Thanks again you really are a patient man right?... Anyway, I can find the form and i know that it exists i will pass some code to give you a better view before i stop working with mdi containers.(last question hehe). what i realllllyy need to accomplish is this: Form A is a list of costumers where i can fill in textboxes with data. If i want to change a costumers data i click on a button search. This will go to form B where i can search through a datagridview of custumers.If i click a celrow the row data has to go to form A in the txt's. Form B code:

              If Not (e.RowIndex = -1) Then
              DataGridView1.Rows(e.RowIndex).Selected = True
              End If
              i = DataGridView1.CurrentRow.Index
              formA.TextBox1.Text = DataGridView1.Item(0, i).Value.ToString()

              enso.... simply explained is that i need a formA.show() before it will pass the dgvitems in the txt. I work between 2 mdichildrens so i cannot do this, also not possible the work with new because the form is already opened. Can somebody help me out of this misery??? :( Sorry for bothering greetz & thx dave;)

              D Offline
              D Offline
              DaveAuld
              wrote on last edited by
              #9

              Ok, let me show you 2 methods; The first is the cheap and easy dirty method............ In this example, there are 2 forms the first has a textbox and a button, the second a listbox and a button, the first calls the second to find the customer the user wants, then returns this top the first form;

              Public Class FormMain
              Private Sub ButtonLookup_Click(sender As System.Object, e As System.EventArgs) Handles ButtonLookup.Click

                  Dim selectForm As New FormCustomerLookup
              
                  TextSelectedCustomer.Text = selectForm.FindCustomer
              
                  'Finished with the dialog
                  selectForm.Close()
              
              End Sub
              

              End Class

              Public Class FormCustomerLookup
              Private ItemHasBeenSelected = False

              Private Sub FormCustomerLookup\_Load(sender As Object, e As System.EventArgs) Handles Me.Load
                  'Add 10 customer to the listbox
              
                  For x As Integer = 0 To 9
                      ListBoxCustomer.Items.Add("Customer" + x.ToString)
                  Next
              
              End Sub
              
              Private Sub ButtonSelect\_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSelect.Click
                  If ListBoxCustomer.SelectedIndices.Count > 0 Then
                      ItemHasBeenSelected = True
                  End If
              End Sub
              
              
              Public Function FindCustomer() As String
              
                  'Show the form to the user
                  Me.Visible = True
              
                  Do While Not ItemHasBeenSelected
                      'Keep the form waiting for the user to make their selection
                      'but Don't hang the interface by using the Nasty DoEvents :)
                      Application.DoEvents()
                  Loop
              
                  Me.Visible = False
              
                  'Return the selected customer
                  Return ListBoxCustomer.Items(ListBoxCustomer.SelectedIndices(0))
              
              End Function
              

              End Class

              The second method uses events;

              Public Class FormMain

              Private WithEvents selectForm As FormCustomerLookup = Nothing
              
              Private Sub ButtonLookup\_Click(sender As System.Object, e As System.EventArgs) Handles ButtonLookup.Click
              
                  selectForm = New FormCustomerLookup
                  selectForm.Show()
              
              End Sub
              
              Private Sub selectForm\_CustomerSelected(item As String) Handles selectForm.CustomerSelected
                  TextSelectedCustomer.Text = item
              
                  'Finished with the dialog
                  selectForm.Close()
              End Sub
              

              End Class

              Public Class FormCustomerLookup

              Public Event CustomerSelected(ByVal item As String)
              
              Private Sub FormCust
              
              D 2 Replies Last reply
              0
              • D DaveAuld

                Ok, let me show you 2 methods; The first is the cheap and easy dirty method............ In this example, there are 2 forms the first has a textbox and a button, the second a listbox and a button, the first calls the second to find the customer the user wants, then returns this top the first form;

                Public Class FormMain
                Private Sub ButtonLookup_Click(sender As System.Object, e As System.EventArgs) Handles ButtonLookup.Click

                    Dim selectForm As New FormCustomerLookup
                
                    TextSelectedCustomer.Text = selectForm.FindCustomer
                
                    'Finished with the dialog
                    selectForm.Close()
                
                End Sub
                

                End Class

                Public Class FormCustomerLookup
                Private ItemHasBeenSelected = False

                Private Sub FormCustomerLookup\_Load(sender As Object, e As System.EventArgs) Handles Me.Load
                    'Add 10 customer to the listbox
                
                    For x As Integer = 0 To 9
                        ListBoxCustomer.Items.Add("Customer" + x.ToString)
                    Next
                
                End Sub
                
                Private Sub ButtonSelect\_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSelect.Click
                    If ListBoxCustomer.SelectedIndices.Count > 0 Then
                        ItemHasBeenSelected = True
                    End If
                End Sub
                
                
                Public Function FindCustomer() As String
                
                    'Show the form to the user
                    Me.Visible = True
                
                    Do While Not ItemHasBeenSelected
                        'Keep the form waiting for the user to make their selection
                        'but Don't hang the interface by using the Nasty DoEvents :)
                        Application.DoEvents()
                    Loop
                
                    Me.Visible = False
                
                    'Return the selected customer
                    Return ListBoxCustomer.Items(ListBoxCustomer.SelectedIndices(0))
                
                End Function
                

                End Class

                The second method uses events;

                Public Class FormMain

                Private WithEvents selectForm As FormCustomerLookup = Nothing
                
                Private Sub ButtonLookup\_Click(sender As System.Object, e As System.EventArgs) Handles ButtonLookup.Click
                
                    selectForm = New FormCustomerLookup
                    selectForm.Show()
                
                End Sub
                
                Private Sub selectForm\_CustomerSelected(item As String) Handles selectForm.CustomerSelected
                    TextSelectedCustomer.Text = item
                
                    'Finished with the dialog
                    selectForm.Close()
                End Sub
                

                End Class

                Public Class FormCustomerLookup

                Public Event CustomerSelected(ByVal item As String)
                
                Private Sub FormCust
                
                D Offline
                D Offline
                DeDelva
                wrote on last edited by
                #10

                Dave You are the sh*t (positif ofcourse) Maybe for the people who browse to do the same with a datagridview :

                If DataGridView1.SelectedRows.Count = 0 Then
                RaiseEvent CustomerSelected(DataGridView1.Item(columnindex, rowindex).Value.ToString())
                End If

                Or something... Dave is there is anything i can do 4 you just mail me on facebook (already send you a message) Succes with everything and greetings from belgium

                1 Reply Last reply
                0
                • D DeDelva

                  Hey peepz, After a big torture of searching i came 2 my last hope. Situation : I have a mdiparent where i open mdichild A. From mdichild A, i click a button to open mdichild B. I "simply" want to pass a value from mdichild B to mdichild A(not a new form). Question: How can i get a value from form B to retreive this in form A(bouth mdichilds). form.show =>opens a new form dim formbla as new blabla =>opens also a new form already tried a less best practice : formA.lblDate.Text = DataGridView1.Item(9, i).Value.ToString() but this only works when i use the show method... I'm begging 4 help Somebody? Thx in advance

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #11

                  There are two ways of looking at this situation: 1. mdiChildA creates the instance of mdiChildB so it can hold a reference to it. 2. mdiParent holds the children so it has a reference to all of them. I prefer number 2 and when creating mdiChildB I would make mdiChildA raise an event that mdiParent responds to and creates mdiChildB. All communication from the children I do by events to the mdiParent. The parent can then call methods/set properties in any other children that need to respond to events raised by others.

                  Dave
                  Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                  1 Reply Last reply
                  0
                  • D DaveAuld

                    Ok, let me show you 2 methods; The first is the cheap and easy dirty method............ In this example, there are 2 forms the first has a textbox and a button, the second a listbox and a button, the first calls the second to find the customer the user wants, then returns this top the first form;

                    Public Class FormMain
                    Private Sub ButtonLookup_Click(sender As System.Object, e As System.EventArgs) Handles ButtonLookup.Click

                        Dim selectForm As New FormCustomerLookup
                    
                        TextSelectedCustomer.Text = selectForm.FindCustomer
                    
                        'Finished with the dialog
                        selectForm.Close()
                    
                    End Sub
                    

                    End Class

                    Public Class FormCustomerLookup
                    Private ItemHasBeenSelected = False

                    Private Sub FormCustomerLookup\_Load(sender As Object, e As System.EventArgs) Handles Me.Load
                        'Add 10 customer to the listbox
                    
                        For x As Integer = 0 To 9
                            ListBoxCustomer.Items.Add("Customer" + x.ToString)
                        Next
                    
                    End Sub
                    
                    Private Sub ButtonSelect\_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSelect.Click
                        If ListBoxCustomer.SelectedIndices.Count > 0 Then
                            ItemHasBeenSelected = True
                        End If
                    End Sub
                    
                    
                    Public Function FindCustomer() As String
                    
                        'Show the form to the user
                        Me.Visible = True
                    
                        Do While Not ItemHasBeenSelected
                            'Keep the form waiting for the user to make their selection
                            'but Don't hang the interface by using the Nasty DoEvents :)
                            Application.DoEvents()
                        Loop
                    
                        Me.Visible = False
                    
                        'Return the selected customer
                        Return ListBoxCustomer.Items(ListBoxCustomer.SelectedIndices(0))
                    
                    End Function
                    

                    End Class

                    The second method uses events;

                    Public Class FormMain

                    Private WithEvents selectForm As FormCustomerLookup = Nothing
                    
                    Private Sub ButtonLookup\_Click(sender As System.Object, e As System.EventArgs) Handles ButtonLookup.Click
                    
                        selectForm = New FormCustomerLookup
                        selectForm.Show()
                    
                    End Sub
                    
                    Private Sub selectForm\_CustomerSelected(item As String) Handles selectForm.CustomerSelected
                        TextSelectedCustomer.Text = item
                    
                        'Finished with the dialog
                        selectForm.Close()
                    End Sub
                    

                    End Class

                    Public Class FormCustomerLookup

                    Public Event CustomerSelected(ByVal item As String)
                    
                    Private Sub FormCust
                    
                    D Offline
                    D Offline
                    DeDelva
                    wrote on last edited by
                    #12

                    Dave, Can you tell me what changes if i want to send my values to destination form?(using events)? Before it was form 1 opens form2 you select a value and send back to form1 close form2 now i want form1 opens form2 and sends the values immediantly. Can you provide my info about the change in code? Thx in advance agian greetz

                    D 1 Reply Last reply
                    0
                    • D DeDelva

                      Dave, Can you tell me what changes if i want to send my values to destination form?(using events)? Before it was form 1 opens form2 you select a value and send back to form1 close form2 now i want form1 opens form2 and sends the values immediantly. Can you provide my info about the change in code? Thx in advance agian greetz

                      D Offline
                      D Offline
                      DaveAuld
                      wrote on last edited by
                      #13

                      You have all the information about events in the message I previously posted. Providing your Form1 has subscribed to the events of form1, you can raise the event on Form2 from anywhere you want within Form2. Cheers, Dave

                      Dave Find Me On: Web|Facebook|Twitter|LinkedIn


                      Folding Stats: Team CodeProject

                      D 1 Reply Last reply
                      0
                      • D DaveAuld

                        You have all the information about events in the message I previously posted. Providing your Form1 has subscribed to the events of form1, you can raise the event on Form2 from anywhere you want within Form2. Cheers, Dave

                        Dave Find Me On: Web|Facebook|Twitter|LinkedIn


                        Folding Stats: Team CodeProject

                        D Offline
                        D Offline
                        DeDelva
                        wrote on last edited by
                        #14

                        hmm Dave i'm not that good that i can apply your last comment into my project... I searched whole day like crazy: Your example:

                        Public Class FormMain

                        Private WithEvents selectKlant As FormCustomerLookup = Nothing

                        Private Sub ButtonLookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLookup.Click
                        selectKlant = New FormCustomerLookup
                        selectKlant.Show()

                        End Sub
                        

                        Private Sub selectForm_CustomerSelected(ByVal item As String) Handles selectKlant.CustomerSelected
                        TextSelectedCustomer.Text = item
                        selectKlant.Close()
                        End Sub
                        End Class

                        Public Class FormCustomerLookup
                        Public Event CustomerSelected(ByVal item As String)

                        Private Sub ButtonSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSelect.Click
                        If DataGridView1.SelectedRows.Count = 0 Then
                        RaiseEvent CustomerSelected(DataGridView1.Item(2, 1).Value.ToString())
                        End If
                        End Sub
                        End Class

                        I just want to change your givin example. So actually i start on formcustomerlookup and before i raise my event i want to open "formmain" and fill the textbox with the value. do i have to switch the with event or??because i cannot move the

                        selectKlant = New FormCustomerLookup
                        selectKlant.Show()

                        Greetz Dave and hoping 4 some patience again...

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups