使用ShowDialog打开表单时获取父表单的值 [英] get value of parent form when form is opened using ShowDialog

查看:71
本文介绍了使用ShowDialog打开表单时获取父表单的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ShowDialog打开表单时获取父表单的值



MDI - > FORM1 - > FORM2使用(Showdialog)



我试过这个...



这是放在FORM1上的用户控件

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


Form2中的
...


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





请帮忙...

感谢adv。



Please help...
Thanks in adv.

推荐答案





有很多方法可以这样做,最简单的方法之一是使用静态变量。但这取决于你想分享哪些信息,这是不可取的。



另一个最好的方法是重载ShowDialog方法并传递值论点。我认为这是在表单之间传递值的最佳方式。



祝你好运。
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.


我找到了解决办法:)

希望对有相同问题的人有用

I have found solution :)
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





注意 如果我直接写Me.ParentForm.AddOwnForm(frm)它会给出错误的顶级控件......



快乐编码!

:)



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

Happy Coding!
:)


这篇关于使用ShowDialog打开表单时获取父表单的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆