Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET WinForm
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
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...
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 27-Dec-12 23:32pm
Edited 27-Dec-12 23:52pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Aarti Meswania at 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 at 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 at 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 at 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 at 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. :)
@AmitGajjar at 28-Dec-12 6:34am
   
nice..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I have found solution Smile | :)
hope it will be useful to them who have same problem
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!
Smile | :)
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 384
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 CPallini 170
4 Shweta N Mishra 165
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100