【问题标题】:Display selected radio button and check box on a msgbox when button1 is clicked VB单击按钮1时在msgbox上显示选定的单选按钮和复选框VB
【发布时间】:2018-01-06 22:39:13
【问题描述】:

所以我要做的是在单击 Button1 时创建一个 MessageBox,并在其上选择单选按钮和复选框。

这是设计:

我希望输出是这样的:

谢谢

【问题讨论】:

  • 所以你想获取选中框的文本或者它们的状态?
  • @MousaAlfhaily 是的,类似于 Picture2 上的内容。只是所选 RadioButtons 和 CheckBoxes 的文本。
  • 是的,这是可以做到的,我将为您提供答案。
  • @Mousa Alfhaily:请不要鼓励使用 SO 作为免费的代码编写服务。
  • @Mousa Alfhaily:别误会我的意思,你想做他的功课这一事实很棒。但这不是 SO 的本意。我们不想要“这是我的作业的一些文本,给我代码”之类的问题,也不应该鼓励人们提出这些问题。就这么简单。

标签: vb.net oracle vba visual-studio vbscript


【解决方案1】:

这样做的更好方法是遍历 groupbox 控件并检查复选框是否已选中,如果是,则将其附加到某个字符串。在所有检查完成后,使用 messagebox 显示字符串。很简单。请通过以下代码为解决方案提供更多亮点

通过控件循环

        Dim strfinal As String
        For Each gb As Control In Me.Controls
            If gb.GetType() Is GetType(GroupBox) Then
                Dim str As String
                str = gb.Text
                For Each c As CheckBox In gb.Controls
                    If c.Checked = True Then
                        str = str + vbNewLine + c.Text
                    End If
                Next
                If str <> "" Then
                    strfinal = strfinal + vbNewLine + str
                End If
            End If
        Next

并在消息框中显示

If strfinal <> "" Then
            MessageBox.Show(strfinal, "somecaption", MessageBoxButtons.OK)
End If

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    这段代码将给出与上图2完全相同的结果:

    Dim Toppings As String = "Toppings:" & VbCrlf
    Dim TSize As String = "Size:"
    Dim CrustType As String = "Crust Type:"
    

    这里当你按下 Button1 时,当包含 toppings 的 groubBox 的名称是 'ToppingsGroupBox' 并且其他 groupBoxes 的名称相同时:

    For Each CB As CheckBox In ToppingsGroupBox.Controls
        If CB.Checked Then
            Toppings &= "-" & CB.Text & VbCrlf
        End If
    End Each
    
    For Each RB As RadioButton In SizesGroupBox.Controls
        If RB.Checked Then
            TSize &= RB.Text
        End If
    End Each
    
    For Each RB As RadioButton In CurstTypeGroupBox.Controls
        If RB.Checked Then
            CurstType &= RB.Text
        End If
    End Each
    
    If DineInRadioBox.Checked Then
        MsgBox(TSize & VbCrlf & CurstType & Toppings & "*Dine In")
    Else
        MsgBox(TSize & VbCrlf & CurstType & Toppings & "*Take Out")
    End If
    

    希望对您有所帮助:)

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 2016-12-07
      相关资源
      最近更新 更多