Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
get value of parent form when form is opened using ShowDialog

MDI -> FORM1 -> FORM2 using (Showdialog)

I have tried this...

This is a usercontrol placed on FORM1
VB
Dim frmnm As String = System.Reflection.Assembly.GetEntryAssembly.GetName.Name & "." & AddbtnForm
          Dim arg As Object() = {Me.CurrentValue}
          Dim frm As Form = DirectCast(System.Reflection.Assembly.GetEntryAssembly.CreateInstance(frmnm, True, Reflection.BindingFlags.CreateInstance, Nothing, arg, Nothing, Nothing), Form)
          Dim d As New DialogResult
          frm.Tag = Me.ParentValue
              d = frm.ShowDialog(Me.ParentForm)'When I Debug it passes Form in side which this control is placed (FORM1)
          If d = DialogResult.Cancel Then
              Refresh()
          End If

in Form2...
VB
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       Dim f = DirectCast(Me.Owner, FORM1) 'BUT IT GIVES ME MDI form here while I had passed FORM1
       Me.Id = f.Id
   End Sub


Please help...
Thanks in adv.
Posted
Updated 27-Dec-12 22:52pm
v3

Hi,

There are many ways to do that, one of the simplest way is to use static variable. but it's depend on what information you would like to share and it is not preferable.

Another best way is to overload ShowDialog method and pass the value in the argument. i think this is the best way to pass values between forms.

Best luck.
 
Share this answer
 
Comments
Aarti Meswania 28-Dec-12 5:56am    
I can't pass argument or can't do with property or static variable
Main reason is,
It's user control my ownlistcontrol that have Add-edit-del-up-down buttons

I set AddbtnForm property each time the form will new
so, I am dynamically calling forms and each time form name will differ they all will need a key value but some of them will need ID also (NOT ALL OF THEM)
so, I have do common thing 1 arg. that's a problem
and if I declare variable some time it will found and if any form do not require it and i don't declare in that form then it will give me error that time

so, I m stuck now,
I have also tried AddOwnerForm but not worked due to top-level property
:(
AmitGajjar 28-Dec-12 5:59am    
if you have usercontrol then you can add one property that can hold the value. let me know if it can work for you.
Aarti Meswania 28-Dec-12 6:02am    
yes, but then all forms will not have that property ID
some will have only Key
some will have id and key both
so, dynamically if going to set id, if form doesn't contain ID prop then it will throw error
AmitGajjar 28-Dec-12 6:04am    
you can add some condition for those records which do not have key. check if it exit or not then proceed.
Aarti Meswania 28-Dec-12 6:14am    
I get solution thanks you try to help me
I was just trying and it worked please see solution-2
thanks again for support. :)
I have found solution :)
hope it will be useful to them who have same problem
VB
Dim frmnm As String = System.Reflection.Assembly.GetEntryAssembly.GetName.Name & "." & AddbtnForm
          Dim arg As Object() = {Me.CurrentValue}
          Dim frm As Form = DirectCast(System.Reflection.Assembly.GetEntryAssembly.CreateInstance(frmnm, True, Reflection.BindingFlags.CreateInstance, Nothing, arg, Nothing, Nothing), Form)
          Dim d As New DialogResult
          frm.Tag = Me.ParentValue
          Dim pf = Me.ParentForm
          pf.AddOwnedForm(frm) ' ------------ *** see note -------------
          d = frm.ShowDialog()
          If d = DialogResult.Cancel Then
              Refresh()
          End If


Note if I will write Me.ParentForm.AddOwnForm(frm) directly it give error top-level control...

Happy Coding!
:)
 
Share this answer
 
v3
Comments
Joe#9 23-Jun-15 0:27am    
Dim arg As Object() = {Me.CurrentValue} ??? what is it?
Cannot use this statement :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900