无法在VB.Net中生成多个单选按钮

问题描述 投票:0回答:3

我正在处理的程序的一部分将涉及我为存储在数组中的每个条目制作多行单选按钮。这将涉及以编程方式创建具有三个单选按钮的组框。但是,我似乎无法让我的代码每盒生成超过1个按钮。我的沙箱代码如下。

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim rb As RadioButton
    Dim gb As GroupBox

    For i As Integer = 1 To 3

        gb = New GroupBox

        With gb
            .Size = New System.Drawing.Size(400, 100)
            .Name = "gb" & i
            .Top = 30 + 100 * (i - 1)
            .Left = 30
        End With

        Me.Controls.Add(gb)

        For j As Integer = 1 To 3

            rb = New RadioButton

            With rb
                .Top = 50
                .Left = 40 * j
                .Name = "rb" & i & j
            End With

            Me.Controls("gb" & i).Controls.Add(rb)
        Next

    Next

    Form2.Show()

End Sub

值得注意的是:用'文本框'查找/替换'单选按钮'给了我三个文本框 - 是否有一些我不知道的单选按钮属性?

提前致谢!!

vb.net visual-studio radio-button dynamically-generated
3个回答
0
投票

定义width属性后,将显示复选框。

我发现至少在我的机器上的默认大小是104.所以,向右移动40是不够的。

            With rb
                .Top = 50
                .Left = 40 * j
                .Name = "rb" & i & j
                .BackColor = System.Drawing.Color.Orange
                '.Text = "rb" & i & j
                .Width = 40
            End With

背景颜色(可以删除,有助于显示复选框实际位于何处)。


0
投票

您的单选按钮可能位于组框后面。顶部和左侧是相对于父级的。

RadioButton1.Parent = GroupBox1
RadioButton1.Top = 30
RadioButton1.Left = 30
GroupBox1.Controls.Add(RadioButton1)

0
投票

由于宽度的原因,显示的代码存在问题。不确定你要完成什么,但这将“修复”你的代码。

    Dim rb As RadioButton
    Dim gb As GroupBox

    For i As Integer = 1 To 3

        gb = New GroupBox

        With gb
            .Size = New System.Drawing.Size(400, 100)
            .Name = "gb" & i
            .Top = 30 + 100 * (i - 1)
            .Left = 30
            .Anchor = .Anchor Or AnchorStyles.Right
        End With

        Me.Controls.Add(gb)

        For j As Integer = 1 To 3

            rb = New RadioButton

            With rb
                .Top = 50
                .Left = 40 * j
                .Name = "rb" & i & j
                .AutoSize = True 'added <<<<<<<<<<<<<<<<<<<
                '.Text = .Name
                '.BackColor = Color.AliceBlue
            End With

            Me.Controls("gb" & i).Controls.Add(rb) 'note rb.Width changes here

            Debug.Write(rb.Name)
            Debug.WriteLine("  Top: {0}, Left: {1}, Width: {2}, Height: {3}",
                            rb.Top, rb.Left, rb.Width, rb.Height)

        Next

    Next
© www.soinside.com 2019 - 2024. All rights reserved.