How to pass values between 2 mdi childs?(without new form)
-
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
-
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
Have a read of these Step by Step: Event handling in VB.NET[^] Delegates[^] and choose which one that best suits your needs
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
-
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
-
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
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
-
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
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 SubDave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
-
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 SubDave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
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
-
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
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
-
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
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;)
-
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;)
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.ClickDim selectForm As New FormCustomerLookup TextSelectedCustomer.Text = selectForm.FindCustomer 'Finished with the dialog selectForm.Close() End Sub
End Class
Public Class FormCustomerLookup
Private ItemHasBeenSelected = FalsePrivate 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
-
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.ClickDim selectForm As New FormCustomerLookup TextSelectedCustomer.Text = selectForm.FindCustomer 'Finished with the dialog selectForm.Close() End Sub
End Class
Public Class FormCustomerLookup
Private ItemHasBeenSelected = FalsePrivate 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
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 IfOr 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
-
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
There are two ways of looking at this situation: 1.
mdiChildA
creates the instance ofmdiChildB
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 creatingmdiChildB
I would makemdiChildA
raise an event thatmdiParent
responds to and createsmdiChildB
. All communication from the children I do by events to themdiParent
. 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) -
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.ClickDim selectForm As New FormCustomerLookup TextSelectedCustomer.Text = selectForm.FindCustomer 'Finished with the dialog selectForm.Close() End Sub
End Class
Public Class FormCustomerLookup
Private ItemHasBeenSelected = FalsePrivate 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
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
-
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
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
-
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
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 ClassPublic 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 ClassI 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...