How to get values from a dialog form in VB.NET?

前端 未结 4 1080
慢半拍i
慢半拍i 2021-02-14 04:39

I have a \"frmOptions\" form with a textbox named \"txtMyTextValue\" and a button named \"btnSave\" to save and close the form when it\'s clicked,

then, I\'m showing thi

4条回答
  •  囚心锁ツ
    2021-02-14 05:25

    You can access the value from the frmOptions instance. However, this breaks the law of demeter.

    You should expose the value with a property within your class. Public Class frmOptions

    Public ReadOnly Property MyTextValue As String
        Get
            Return Me.txtMyTextValue.Text
    
        End Get
    End Property
    

    End Class

    Then you can access the value:

     Private Sub ShowOptionsForm()
            Dim options = New frmOptions
            Dim frmOptionTextValue As String
            Dim frmOptionsDiagResult As DialogResult
            frmOptionsDiagResult = options.ShowDialog()
            If frmOptionsDiagResult = Windows.Forms.DialogResult.OK Then
                frmOptionTextValue = options.MyTextValue
            Else
                '...
            End If
        End Sub
    

    Finally, if you are using a Dialog then make sure to set the Dialog Result for the button.

提交回复
热议问题