如何将表单作为函数的参数传递给 vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26512857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a form as the parameter of a function vb.net
提问by OmnivorousOctopus
Currently in VB.NET I have two public subs like so:
目前在 VB.NET 中,我有两个公共潜艇,如下所示:
Public Sub addmember1()
'Stuff
End Sub
Public Sub furtherinfo1()
'suff
End Sub
I haven't included the code where "'stuff" is as it is very long and is exactly the samein each sub, however the underlying the principle remains the same.
我没有包含“'东西”所在的代码,因为它很长并且在每个子程序中都完全相同,但是基本原理保持不变。
A certain sub is ran depending on a boolean value. like so...
根据布尔值运行某个子程序。像这样...
If add_member = True Then
addmember1()
ElseIf add_member = False Then
furtherinfo1()
End If
How would I use one function to carry out the same procedure as above? (my current solution works but involves repeating the same section of code twice )
我将如何使用一个函数来执行与上述相同的过程?(我目前的解决方案有效,但涉及重复同一段代码两次)
I tried the following however was unsuccessful
我尝试了以下但没有成功
Public Function forms(ByVal frm As Windows.Forms.Form)
'stuff
End Function
and then run the function like so... (addmember and furtherinfo are the two forms I am working with)
然后像这样运行函数......(addmember和进一步信息是我正在使用的两种形式)
If add_member = True Then
forms(addmember)
ElseIf add_member = False Then
forms(furtherinfo)
End If
here is the paste bin of all the code for context it's in modual and I want to use it for writing information to a word document. Lines 20-71, 76-128, 160-164 is what I am on about.
这是所有上下文代码的粘贴箱,它处于模态,我想用它来将信息写入 Word 文档。第 20-71、76-128、160-164 行就是我要说的。
采纳答案by Samir Ibrahim
You can pass the form object to a Sub() in a module as below
您可以将表单对象传递给模块中的 Sub() ,如下所示
Module Printing
模块打印
Dim StrToAdd As String
Sub MySub(ByVal frm As Form)
'The first line is your code
StrToAdd = "Firstname: " & addmember.txtName.Text
'Change it to as below using frm.Controls("controlname").Text
StrToAdd = "Firstname: " & frm.Controls("txtName").Text
End Sub
End Module
终端模块
回答by barrypicker
After looking at your code at http://pastebin.com/xWD0RBuhit seems you have a global module with multiple sub routines. Each sub routine has references to controls (such as text boxes) on a form instance. This means each global module sub routine needs to have access to this form instance.
在http://pastebin.com/xWD0RBuh查看您的代码后,您似乎有一个包含多个子例程的全局模块。每个子例程都有对表单实例上的控件(例如文本框)的引用。这意味着每个全局模块子例程都需要访问此表单实例。
You have only copied in part of the application - the global module, but you have not copied in the form definition. I presume you have a form called addmember, but I don't see it in the example - aside from references in the global module.
您只复制了应用程序的一部分 - 全局模块,但您没有在表单定义中复制。我假设您有一个名为 addmember 的表单,但我在示例中没有看到它 - 除了全局模块中的引用。
Not sure how you use the sub routines - probably a click of a button. If it were me, I would create a class object with properties that hold the data to pass around - one property for each control on the form you want to print. On the click of the button, I would create an instance of the class and copy the values from the form controls into the class properties. I would then pass the instance of the class to the sub routines, and I would alter the sub routines to refer to an instance of the class instead of an instance of a form. That would provide a level abstraction between the form (UI) and the behavior (the sub routines that print). I may even go "crazy" and use an interface.
不确定您如何使用子程序 - 可能是单击一个按钮。如果是我,我会创建一个类对象,该对象具有保存要传递的数据的属性 - 您要打印的表单上的每个控件都有一个属性。单击按钮时,我将创建类的一个实例并将表单控件中的值复制到类属性中。然后我将类的实例传递给子例程,并且我将更改子例程以引用类的实例而不是表单的实例。这将提供表单(UI)和行为(打印的子例程)之间的级别抽象。我什至可能会变得“疯狂”并使用界面。
Does your code compile as-is?
您的代码是否按原样编译?
回答by dummy
The Problem is that your two Forms are two different classes. Even though you named your controls
the same, you can't just access them simply by frm.txtUsername.
问题是您的两个表单是两个不同的类。即使您将控件命名为相同,也不能仅通过frm.txtUsername.
What you could do is iterate through all controls of each form und find them by name:
您可以做的是遍历每个表单的所有控件并按名称找到它们:
Public Sub DoStuff(frm As Form)
Dim txtUsername As TextBox = GetControlByName(frm, "txtUsername")
txtUsername.Text = "Hello World"
End Sub
Private Function GetControlByName(container As Control, name As String) As Control
Dim retVal As Control = Nothing
If Not TryGetControlByName(container, name, retVal) Then Throw New ApplicationException("control not found")
Return retVal
End Function
Private Function TryGetControlByName(container As Control, name As String, ByRef ctl As Control) As Boolean
For Each item As Control In container.Controls
If item.Name = name Then
ctl = item
Return True
End If
'If item is a Container (like GroupBox, Panel) check its children
If TryGetControlByName(item, name, ctl) Then Return True
Next
Return False
End Function
If you want to get really fancy you could define a Class with the common Controls and fill them via a little bit of reflection magic. Though this might be overkill:
如果你想变得真正花哨,你可以定义一个带有通用控件的类,并通过一点点反射魔法来填充它们。虽然这可能有点矫枉过正:
Public Sub DoStuff2(frm As Form)
Dim wrapper As New CommonForm(frm)
wrapper.txtUsername.Text = "Hello Wolrd"
End Sub
Public Class CommonForm
Public Property txtUsername As TextBox
Public Property txtFoo As TextBox
Public Property txtBar As TextBox
'Add more Controls here...
Public Sub New(frm As Form)
For Each item In Me.GetType().GetProperties()
Dim value = GetControlByName(frm, item.Name)
item.SetValue(Me, value, Nothing)
Next
End Sub
Private Function GetControlByName(container As Control, name As String) As Control
Dim retVal As Control = Nothing
If Not TryGetControlByName(container, name, retVal) Then Throw New ApplicationException("control not found")
Return retVal
End Function
Private Function TryGetControlByName(container As Control, name As String, ByRef ctl As Control) As Boolean
For Each item As Control In container.Controls
If item.Name = name Then
ctl = item
Return True
End If
'If item is a Container (like GroupBox, Panel) check its children
If TryGetControlByName(item, name, ctl) Then Return True
Next
Return False
End Function
End Class

