【问题标题】:RadioButton does not show up when inside a panel within a panel在面板内的面板内时,RadioButton 不显示
【发布时间】:2020-02-27 17:14:02
【问题描述】:

这是我的第一个问题,如果我做错了什么,请原谅我。

我有一个小型调查表,它会自动从 SQL 服务器表中提取问题和答案,并创建一个问题标签 (Label_Questionnaire(i)),一个用于嵌套每个问题答案的所有单选按钮的面板 (Panel_Response(i) ) 和 3 个单选按钮(是、否、不适用),名为 RadioButton_Answers(i)_1 。所有问题和答案都在一个大面板 (Panel_Survey) 内,允许用户上下滚动(大约 50 个问题)。

当我运行程序时,我只能看到问题,但没有显示任何单选按钮。我所做的尝试是:

  • 使用 .BringToFront 将 Panel_Response 和所有单选按钮置于前面。
  • 将 .Parent 更改为 Controls.Add 。我没有使用 .Parent,而是使用 Panel_Survey.Controls.Add(Panel_Response) 和 Panel_Response.Controls.Add(RadioButton_Answers_1)
  • Force Panel_Response.Visible = True 并且所有单选按钮可见 = true (我知道这听起来可能很愚蠢,但我没有把戏)

如何让这些单选按钮显示出来?如果没有,这种调查表有没有更好的设计?提前感谢您的任何建议!

下面是我的代码:

Protected Overrides Sub OnLoad(e As EventArgs)
    Dim PanelCount As Integer
    Dim QuestionName As String
    Dim Response1 As String
    Dim Response2 As String
    Dim Response3 As String
    Dim InitialX As Integer = Panel_Survey.Left
    Dim InitialY As Integer = Panel_Survey.Top
    Dim SizeX As Integer = 1000
    Dim SizeY As Integer = 25

    'Load the survey

    Try
        'Get a list of questions and answers into array of list

        Dim ListofQuestionandAnswers As New List(Of List(Of String))
        Dim conn As New SqlClient.SqlConnection
        conn.ConnectionString = ConnectionString
        Dim CommandString As String = "SELECT [QuestionID], [QuestionName] ,[Response1],[Response2],[Response3] FROM [Question_List] ORDER BY [QuestionID]"
        Dim Command As New SqlClient.SqlCommand
        Command.CommandText = CommandString
        Command.Connection = conn
        Dim dr As SqlClient.SqlDataReader
        conn.Open()
        dr = Command.ExecuteReader
        While dr.Read
            Dim ls As New List(Of String)
            ls.Add(dr.GetValue(0).ToString)
            ls.Add(dr.GetValue(1).ToString)
            ls.Add(dr.GetValue(2).ToString)
            ls.Add(dr.GetValue(3).ToString)
            ls.Add(dr.GetValue(4).ToString)
            ListofQuestionandAnswers.Add(ls)
        End While
        conn.Close()

        PanelCount = ListofQuestionandAnswers.Count

        For i = 0 To ListofQuestionandAnswers.Count - 1

            QuestionName = ListofQuestionandAnswers(i)(1)
            Response1 = ListofQuestionandAnswers(i)(2)
            Response2 = ListofQuestionandAnswers(i)(3)
            Response3 = ListofQuestionandAnswers(i)(4)

            Dim Label_Questionnaire As New Label
            Dim Panel_Response As New Panel
            Dim RadioButton_Answers_1 As New RadioButton
            Dim RadioButton_Answers_2 As New RadioButton
            Dim RadioButton_Answers_3 As New RadioButton



            'Condition the label 

            With Label_Questionnaire
                .Parent = Panel_Survey
                .Name = "Label_Questionnaire" + i.ToString
                .Font = New Font("Calibri", 11, FontStyle.Regular)
                .Text = QuestionName
                .ForeColor = Color.Black
                .Location = New Point(InitialX, InitialY)
                .AutoSize = True

            End With

            'Condition the panel

            With Panel_Response
                'Panel_Survey.Controls.Add(Panel_Response)
                .Parent = Panel_Survey
                .Name = "Panel_Questionnaire" + i.ToString
                .Location = New Point(InitialX + 880, InitialY)
                .Width = 250
                .Height = 25
                .BringToFront()
            End With

            Dim j As Integer
            Dim h As Integer
            j = Panel_Response.Left
            h = Panel_Response.Top



            'Condition the radiobuttons for answers

            With RadioButton_Answers_1
                .Parent = Panel_Response
                .Name = "RadioButton_Answers" + i.ToString + "_1"
                .Font = New Font("Calibri", 11, FontStyle.Regular)
                .Text = Response1
                .ForeColor = Color.Black
                .Location = New Point(j, h)
                .AutoSize = True


                h += RadioButton_Answers_1.Height
            End With

            With RadioButton_Answers_2
                .Parent = Panel_Response
                .Name = "RadioButton_Answers" + i.ToString + "_2"
                .Font = New Font("Calibri", 11, FontStyle.Regular)
                .Text = Response2
                .ForeColor = Color.Black
                .Location = New Point(RadioButton_Answers_1.Right, h)
                .AutoSize = True
            End With



            With RadioButton_Answers_3
                .Parent = Panel_Response
                .Name = "RadioButton_Answers" + i.ToString + "_3"
                .Font = New Font("Calibri", 11, FontStyle.Regular)
                .Text = Response3
                .ForeColor = Color.Black
                .Location = New Point(RadioButton_Answers_2.Right, h)
                .AutoSize = True
            End With

            InitialY = InitialY + SizeY + 10

        Next

    Catch ex As Exception
        MessageBox.Show(String.Format("Error: {0}", ex.Message), "Error while creating questions and answers", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

【问题讨论】:

  • 1.您的查询是否实际返回结果? 2. 我只看到Panel_Survey.Controls.Add(Panel_Response) 但我不确定这一行是否足以在面板中添加控件。
  • 如果您只使用 FlowLayoutPanel,您的生活肯定会更轻松吗?
  • ...并构建一个已经包含所有这些子控件的 UserControl。设置后,您应该检查这些 RadioButtons 的位置。 (顺便说一句,不仅如此,所有的位置都是错误的)
  • 设置 .Parent 是不够的....您需要在所有级别使用 .Controls.Add()... 将标签添加到面板,将面板添加到面板,并将单选按钮添加到面板中的面板...为了了解它的外观,请向您的项目添加一个新表单,向其中添加控件,就像您希望它看起来一样,然后采取查看 .Designer.VB 文件...然后您将看到它如何创建控件并将它们相互添加...这就是您需要做的。
  • 另外,我不确定你的 h 和 j 值是你认为你想要的...... h 是面板的高度......这意味着你的位置点刚刚掉下来面板......这可能就是为什么什么都没有出现。位置将与集装箱相关...所以尝试从 10,10 之类的东西开始玩...看看您的 RB 是否出现...它们可能一直都在那里,但您让它们晃来晃去一直处于低谷。

标签: sql-server vb.net


【解决方案1】:

这里有几个基本问​​题:

  • 您似乎认为当控件放置在面板内时,它必须放置在相对于窗体的位置。它没有;它相对于具有自己的坐标系的面板放置,该坐标系从面板左上方的 0,0 开始 - 您将 jh (用于单选按钮的位置)初始化到面板的左侧和顶部它们在,但如果您希望单选按钮从面板的左上角开始,它们应该被初始化为 0,0。如果面板放置在表单上的 300,300,并且您在面板内也放置了一个单选按钮 300,300(因为您复制了它的左侧和顶部),那么单选按钮将看起来像它在表单上的 600,600,因为它在 300,300 内300,300 的面板

  • 您只需将面板设置为 250 宽 - 它的宽度几乎不足以显示单个单选按钮

  • 您将 h 增加一次,其数量意味着下一个单选按钮从面板底部消失(即 25 像素高)

这里:

With Panel_Response
  'Panel_Survey.Controls.Add(Panel_Response)
  .Parent = Panel_Survey
  .Name = "Panel_Questionnaire" + i.ToString
  .Location = New Point(InitialX + 880, InitialY)
  .Width = 2500
  .Height = 25
  .BringToFront()
  .BackColor = Color.Red
End With

Dim j As Integer
Dim h As Integer
j = 0
h = 0

将其粘贴到您的应用代码上并再次运行。我将面板 BackColor 设置为红色,以便您更轻松地查看面板的位置

我想你需要决定你希望你的 UI 看起来如何。如果单选按钮垂直布局,则在将它们添加到面板时不要增加 X(通过设置 parent 属性)。使面板足够高以容纳它们(25 px 不够)


改用 FlowLayoutPaanel 或 TableLayoutPanel

【讨论】:

  • 感谢您的建议。我今晚会试试看。
  • 我刚刚尝试了 j=0 和 h=0,它成功了。现在我完全理解在子面板中使用位置 0,0 作为顶部和左侧的意思。我也会尝试 FlowLayoutPanel 和 TableLayoutPanel。感谢您的帮助!
猜你喜欢
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 2021-06-12
  • 2013-05-21
  • 2012-04-28
  • 1970-01-01
相关资源
最近更新 更多