如何在关闭之前从对话框表单访问属性,VB.NET winforms [英] How do I access a property from a dialog form before it gets closed, VB.NET winforms
问题描述
希望你们所有人都很好并且进入2018年。
我会把它分解成一些简单的项目的术语描述起来很复杂,所以这里是基础知识。
Form1创建一个新的Form2实例并将其显示为showdialog - 我无法访问List声明从Form2到Form1。
我不能有全局声明或变量 - 下面进一步解释。
我尝试过:
我有两种形式,都是在设计师中创建的。
Form1创建表格2的新实例/副本通过
Dim NewForm As New Form2
NewForm.ShowDialog()
我正在使用showdialog但是我很乐意使用show show但这给了我一组新的问题,就是从Form2中找到,那个叫Form,即创建Form2的新实例/副本,已关闭。
项目将包含更多的表单,即Form3,Form4,Form5 .... Form20,都创建了Form2的新副本,需要从Form2访问List(of String)的副本,因此全局声明List(of String)是不可能的。
那些调用Form2的表单也可以通过新的表单命令从其他表单创建。因此,通过变量来跟踪该表单是否已关闭对我来说是不利的。
在Form2上,我有一些数据存储在
List(Of String)
在Form2中,我已经在课堂上声明:
Dim _Keywords As New List(Of String)
Friend ReadOnly Property Keywords()As List(Of String)
获取
返回_关键词
结束获取
结束物业
我需要访问(来自Form1,3,4,5,6 ...... 20)Form2上的List(Of String)才能处理/关闭/设置为空,而不是任何更快。
目前我在showdialog之后在Form1中执行此操作:
Dim Kwords As List(Of String)= GlobalFind.Keywords
NewFrm.Dispose
NewFrm = Nothing
$ 1 $ b在Form1中,Kwords列出它总是空的,当我看时,我得到一个数= 0。希望有意义,谢谢你给我你的时间(希望我忽略了一个解决方案)。
在Form2上公开一个属性,然后得到结果Form1:
Form2
公共 类 Form2
私有 mResults 正如列表(> 字符串)
公开 属性结果()作为列表( Of 字符串)
获取
返回 mResults
结束 获取
设置( ByVal 值作为列表( 字符串))
mResults = value
结束 设置
结束 属性
私有 Sub butSave_Click(sender 作为 对象,e 作为 EventArgs)句柄但保留。点击
' 在此处设置结果
结果= 新列表( String )来自{ 111, 222,< span class =code-string> 333}
关闭()
结束 Sub
结束 班级
Form1
公开 类 Form1
私有 Sub Button1_Click(发件人 As Object ,e As EventArgs) Handles Button1.Click
Dim NewForm As 新 Form2
NewForm.ShowDialog()
Dim 结果 As List( 字符串)= NewForm.Results
End Sub
结束 类跨度>
Hi,
Hope all of you are well and settling into 2018 :)
I'll break this down into some simple terms as the project is quite complicated to describe, so here's the basics.
Form1 creates a new instance of Form2 and shows it as showdialog - I cannot access the List declaration from Form2, into Form1.
I cannot have global declarations or variables - explained further, below.
What I have tried:
I have two forms, both created in the designer.
Form1 creates a new instance/copy of Form 2 via
Dim NewForm As New Form2 NewForm.ShowDialog()
I am using showdialog but I am happy to use just show but that gives me a new set of problems, which is to find out from Form2, that calling Form, that is creating a new instance/copy of Form2, has been closed.
The project will contain many more forms i.e. Form3, Form4, Form5....Form20, all creating a new copy of Form2 and need to access "that copy of List(of String)" from Form2, so a global declaration of the List(of String) is not possible.
Those calling forms, to Form2 - may also be created via a new form command, from other forms. So keeping track via variables of whether that form has been closed, is no good to me.
On Form2, I have some data that I am storing in a
List(Of String)
In Form2, I have declared at the top of the class:
Dim _Keywords As New List(Of String) Friend ReadOnly Property Keywords() As List(Of String) Get Return _Keywords End Get End Property
I need to access (from Form1 ,3,4,5,6....20) the List(Of String) on Form2 before it gets disposed/closed/set to nothing and not any sooner.
At the moment I do this in Form1 after the showdialog:
Dim Kwords As List(Of String) = GlobalFind.Keywords NewFrm.Dispose NewFrm=Nothing
in Form1, Kwords list its always empty, I get a count=0 when I look. Hope that makes sense and Thank you for giving me your time (and hopefully a solution I have overlooked).
Expose a property on Form2, then get the results in Form1:
Form2
Public Class Form2 Private mResults As List(Of String) Public Property Results() As List(Of String) Get Return mResults End Get Set(ByVal value As List(Of String)) mResults = value End Set End Property Private Sub butSave_Click(sender As Object, e As EventArgs) Handles butSave.Click ' set results here Results = New List(Of String) From {"111", "222", "333"} Close() End Sub End Class
Form1
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim NewForm As New Form2 NewForm.ShowDialog() Dim results As List(Of String) = NewForm.Results End Sub End Class
这篇关于如何在关闭之前从对话框表单访问属性,VB.NET winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!