我正在创建一个“部门选择器”表单,它将作为一个模式弹出表单,其中包含我的许多 Winforms 应用程序的“主要”表单。理想情况下,用户将单击将弹出表单的文本框旁边的图标,他们将选择所需的部门,当他们单击确定时,对话框将关闭,我将为我选择值更新文本框。
我已经完成了将对话框的所有者传递到对话框表单并让 OK 按钮单击事件进行正确更新的路线,但这迫使我对表单类型进行 DirectCast,然后我只能重用当前表单上的选择器。
我已经能够在构造函数中使用 ByRef 变量并成功更新了一个值,但它仅在构造函数中有效。如果我尝试将 ByRef 值分配给 Department Picker 类中的某个内部变量,则会丢失它的引用方面。这是附加到我的表单的基本代码:
Public Class DeptPicker
Private m_TargetResult As String
Public Sub New(ByRef TargetResult As String)
InitializeComponent()
' This works just fine, my "parent" form has the reference value properly updated.
TargetResult = "Booyah!"
' Once I leave the constructor, m_TargetResult is a simple string value that won't update the parent
m_TargetResult = TargetResult
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
DialogResult = Windows.Forms.DialogResult.OK
' I get no love here. m_TargetResult is just a string and doesn't push the value back to the referenced variable I want.
m_TargetResult = "That department I selected."
Me.Close()
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
End Class
有人可以告诉我我在这里缺少什么或不同的方法来实现这一点吗?
注意:代码示例在 VB.NET 中,但我也会接受任何 C# 答案。8^D