【问题标题】:How to get values from a dialog form in VB.NET?如何从 VB.NET 中的对话框中获取值?
【发布时间】:2013-12-31 07:39:03
【问题描述】:

我有一个“frmOptions”表单,其中包含一个名为“txtMyTextValue”的文本框和一个名为“btnSave”的按钮,用于在单击时保存和关闭表单,

然后,当单击主窗体“frmMain”上的按钮“btnOptions”时,我将显示此对话框窗体“frmOptions”,如下所示

Private Sub btnOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOptions.Click
    ShowOptionsForm()
End Sub

Private Sub ShowOptionsForm()
    Dim options = New frmOptions
    options.ShowDialog()
End Sub

当单击“btnSave”时,如何在主窗体“frmMain”中获取插入到文本框“txtMyTextValue”中的值?

【问题讨论】:

    标签: vb.net winforms textbox dialog modal-dialog


    【解决方案1】:

    只有当结果为OK(用户按Save 而不是Cancel 或以其他方式关闭对话框)时,您才想从对话框中捕获信息,所以这样做:

    Private Sub ShowOptionsForm()
        Dim options = New frmOptions
    
        ' Did the user click Save?
        If options.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ' Yes, so grab the values you want from the dialog here
            Dim textBoxValue As String = options.txtMyTextValue.Text
        End If
    End Sub
    

    现在在您的对话框表单中,您需要在用户单击与对话框表单的OK 操作对应的按钮时设置结果Windows.Forms.DialogResult.OK,如下所示:

    Public Class frmOptions
        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
            ' Set the result to pass back to the form that called this dialog
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End Sub
    End Class
    

    【讨论】:

    • 感谢您简洁明了的回答。
    • @Max - 没问题,很高兴它帮助了你。祝你好运。 :-)
    【解决方案2】:

    您可以从 frmOptions 实例访问该值。但是,这违反了得墨忒耳定律。

    您应该在您的类中使用属​​性公开该值。 公共类 frmOptions

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

    结束类

    然后就可以访问值了:

     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
    

    最后,如果您使用的是对话框,请确保为按钮设置对话框结果。

    【讨论】:

      【解决方案3】:

      您可以使用事件来处理这个问题。使用这种方法,设置表单不必是模态的,用户可以随时单击保存按钮。

      在 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
      

      在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
      

      【讨论】:

        【解决方案4】:

        最简单的方法是在 frmOptions 表单中添加一个公共属性,该属性返回在 frmOptions 的全局级别声明的内部字符串

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

        然后,当您的用户单击“确定”按钮以确认其选择时,您将文本框的值复制到内部变量中

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

        最后在 frmMain 你使用这样的代码来检索插入的值

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

        我更愿意避免直接访问 frmOptions 的内部控件,该属性提供了一种间接方式,可用于更好地验证用户提供的输入。

        【讨论】:

          猜你喜欢
          • 2015-09-08
          • 2012-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多