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

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

    You can use Events to take care of this. With this approach the Settings Form does not have to be Modal and the user can click the Save Button at any time.

    In frmOptions:

    'You can expand the signature to take more than just a single String.
    Friend Event SavedOptions(ByVal strData As String)
    
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    
        RaiseEvent SavedOptions(txtMyTextValue.Text)
    
    End Sub
    

    In frmMain:

    Private Sub ShowOptionsForm()
    
        Dim options = New frmOptions
    
        AddHandler options.SavedOptions, AddressOf OnOptionsSave
    
        options.ShowDialog()
    
    End Sub
    
    Private Sub OnOptionsSave(ByVal strData As String)
    
        'Or whatever you want to do on frmMain with Options Data.
        MsgBox(strData)
    
    End Sub
    

提交回复
热议问题