我在VB.net中编写程序代码,应该执行以下操作:
程序应该读取.txt文件并动态创建.txt文件中的问题数量的复选框(或者可能是CheckBoxList)。
所以,就个人而言,我创建了一个按钮,单击该按钮时会读取.txt文件。之后,我做了一个循环检测问题(及其数量),并根据我为每个问题制作了2个按钮(代表是或否)。
问题在于:如何实施If checkbox1.Checked = True Then checkbox2.Checked = False
。我的想法是制作两个复选框数组并比较它们(是否检查了第一个元素),但我不知道它是否正确或是否可能。
相关代码如下:
Dim xRead As System.IO.StreamReader
xRead = File.OpenText("survey.txt")
'Dim counter As Integer
Dim line As String
Dim counter1 As Integer
Do Until xRead.EndOfStream
line = xRead.ReadLine()
TextBox1.Text = TextBox1.Text & line & vbCrLf
If line.Contains("Question") Then
counter1 = counter1 + 1
Dim offset1 As Integer
offset1 = 500
Dim offset2 As Integer
offset2 = 200 + offset2
'counter = counter + 1, we use this at first to see if it works.
Dim checkBox1 As New CheckBox()
Me.Controls.Add(checkBox1)
checkBox1.Location = New Point(offset1 + 50, offset2)
checkBox1.Checked = False
checkBox1.Size = New Size(100, 20)
Dim array1(counter1) As CheckBox
array1(counter1) = checkBox1
Dim checkBox2 As New CheckBox()
Me.Controls.Add(checkBox2)
checkBox2.Location = New Point(offset1, offset2)
checkBox2.Size = New Size(100, 20)
Dim array2(counter1) As CheckBox
array2(counter1) = checkBox2
offset2 = 50 + offset2 - 200
End If
Loop
xRead.Close()
任何帮助都将受到高度赞赏。我事先对这篇长篇文章感到非常抱歉。
修改 如果我想一次提出1个问题并且一些问题有不同的答案怎么办(例如Q1也有答案是/否,但Q2有答案同意/不同意/不确定等等)我该怎么做? 我到目前为止所得到的结果是只发布了最后一个问题。我做了以下事情:
Imports System.IO
Public Class Form1
Dim counter1 As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim xRead As System.IO.StreamReader
xRead = File.OpenText("survey.txt")
'Dim counter As Integer
Dim line As String
Dim offset1 As Integer
Dim offset2 As Integer
Do Until xRead.EndOfStream
line = xRead.ReadLine() & vbCrLf
If line.Contains("Question") Then
line = line + xRead.ReadLine()
TextBox1.Text = line & vbCrLf
line = xRead.ReadLine()
If line.Contains("/") Then
counter1 = counter1 + 1
offset1 = 150
offset2 = 200
'counter = counter + 1, we use this at first to see if it
works.
Dim checkBox1 As New CheckBox()
Me.Controls.Add(checkBox1)
checkBox1.Location = New Point(offset1, offset2)
checkBox1.Checked = False
checkBox1.Size = New Size(100, 20)
checkBox1.Text = "Yes"
Dim checkBox2 As New CheckBox()
Me.Controls.Add(checkBox2)
offset1 = 250
checkBox2.Location = New Point(offset1, offset2)
checkBox2.Size = New Size(100, 20)
checkBox2.Text = "No"
AddHandler checkBox1.CheckedChanged, Sub()
checkBox2.Checked = Not checkBox1.Checked
End Sub
AddHandler checkBox2.CheckedChanged, Sub()
checkBox1.Checked = Not checkBox2.Checked
End Sub
Dim btn As Button = New Button
btn.Location = New Point(offset1 + 200, offset2)
btn.Name = "Next Question"
btn.Text = "Next Question"
Me.Controls.Add(btn)
AddHandler btn.Click, AddressOf Me.btn_Click
TextBox1.Text = ""
End If
End If
Loop
xRead.Close()
End Sub
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim checkCount As Integer = 0
For Each chkBox As CheckBox In Me.Controls.OfType(Of CheckBox)()
If chkBox.Checked Then
checkCount += 1
End If
Next
If checkCount = 1 Then
MessageBox.Show("Next question.")
Else
MessageBox.Show("Please answer the question and then hit Submit.")
TextBox1.Text = ""
End If
End Sub
结束班
答案 0 :(得分:0)
创建两个复选框后,使用AddHandler处理框的CheckedChanged事件:
AddHandler checkBox1.CheckedChanged, Sub()
checkBox2.Checked = Not checkBox1.Checked
End Sub
AddHandler checkBox2.CheckedChanged, Sub()
checkBox1.Checked = Not checkBox2.Checked
End Sub
不需要数组。
要检查每个问题是否有检查,请将counter1声明移到表单范围级别,并将其放在按钮的单击事件中:
Dim checkCount As Integer = 0
For Each chkBox As CheckBox In Me.Controls.OfType(Of CheckBox)()
If chkBox.Checked Then
checkCount += 1
End If
Next
If checkCount = counter1 Then
MessageBox.Show("all good")
Else
MessageBox.Show("not all answered.")
End If