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

前端 未结 4 1085
慢半拍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:11

    The simplest method is to add a public property to the frmOptions form that returns an internal string declared at the global level of the frmOptions

    Dim strValue As String
    Public Property MyStringValue() As String
        Get
           Return strValue
        End Get
    End Property
    

    Then, when your user clicks the OK button to confirm its choices you copy the value of the textbox to the internal variable

    Private Sub cmdOK_Click(sender As Object, e As System.EventArgs) Handles cmdOK.Click
        strValue = txtMyTextValue.Text
    End Sub
    

    Finally in the frmMain you use code like this to retrieve the inserted value

    Private Sub ShowOptionsForm()
        Using options = New frmOptions()
           if DialogResult.OK = options.ShowDialog() Then
              Dim value = options.MyStringValue
           End If
        End Using
    End Sub
    

    I prefer to avoid direct access to the internal controls of the frmOptions, a property offer a indirection that could be used to better validate the inputs given by your user.

提交回复
热议问题