【发布时间】: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