I am creating a "Department Selection" form that will serve as a modal form of pop-ups with many of my "primary" forms of the Winforms application. Ideally, the user will click on the icon next to the text field in which the form appears, they will select the department they need, and when they click OK, the dialog will close and I will have the selected value for me. Update the text field with.
I have already done the route by transferring the owner of the dialog box to the dialog form and clicking OK, click OK to make the corresponding update, but this forces me to do DirectCast for the type of form, and then I can only reuse the collector in the current form.
I managed to use the ByRef variable in the constructor and successfully update the value, but it only works in the constructor. If I try to assign the ByRef value to some internal variable in the Picker class of the class, I will lose its reference aspect. This is my main code attached to my form:
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
Can someone tell me what I'm missing here, or is there a different approach for this to happen?
Note. The sample code is in VB.NET, but I will also answer any C # answers. 8 ^ D
source
share