用vb怎么控制数位三用电表_在VB.net中实现控制数组

用vb怎么控制数位三用电表

PROBLEM

问题

The project I am currently working on is MMPI test module. One of the most challenging parts was to develop a user interface that will accommodate answers for over 560 questions with three options (True, False, Don't care / pass) each. This would have been done this in VB6 using three control arrays of radio buttons. But in VB.Net, control arrays are no longer supported.

我当前正在从事的项目是MMPI测试模块。 最具挑战性的部分之一是开发一个用户界面,该界面可容纳560多个问题的答案,每个都有三个选项(“真”,“假”,“不在乎/通过”)。 在VB6中,这将使用三个单选按钮控制数组来完成。 但是在VB.Net中,不再支持控制数组。

SOLUTION

Then I found out that VB.Net supports classes. I was fascinated as always with concept of OOPS, and applied it to my need. The result was fabulous. Here is the code for the class and a function which uses the code from a VB.Net form. Without the class I would have ended up drawing manually 600*3=1800 radio buttons on the form and then linking them with appropriate check changed event, which would have been a nightmare for a lazy fellow like me.

然后我发现VB.Net支持类。 我一如既往地对OOPS的概念着迷,并将其应用于我的需要。 结果很棒。 这是该类和使用VB.Net表单中的代码的函数的代码。 如果没有这个班级,我最终将在表格上手动绘制600 * 3 = 1800个单选按钮,然后将它们与适当的检查更改事件相关联,这对于像我这样的懒惰的家伙来说将是一场噩梦。

You can vary this code in any form to accommodate controls other than radiobuttons.

您可以以任何形式更改此代码,以容纳单选按钮以外的控件。

Code explanation:

代码说明:

The Code for this array is very simple.

该数组的代码非常简单。

1. I made a Groupbox control using code inside the class Questionarraysa and attached four controls to the groupbox(3 radio buttons and a label). No need to load the controls just use Add function of Controls collection of Groupbox to attach controls

1.我使用类Questionarraysa中的代码制作了一个Groupbox控件,并将四个控件附加到groupbox(3个单选按钮和一个标签)。 无需加载控件,只需使用Groupbox的控件集合的添加功能来附加控件

2. After which I Positioned each of the controls within the Groupbox by setting their Top and Left properties.

2.然后,通过设置控件的“顶部”和“左侧”属性,将每个控件放置在“组框”中。

3. Finaly I attached the Checkchanged Event to each of the radiobuttons, to the functions I wrote. Now one question template class is ready. The calls function AddNewQuestion will return a Groupbox control (which will contain the label and radiobutton controls in it).

3.最后,我将Checkchanged事件附加到我编写的每个单选按钮上。 现在已经准备好一个问题模板类。 调用函数AddNewQuestion将返回一个Groupbox控件(其中将包含标签和单选按钮控件)。

4. In the form then, an array of the template class (Questionarray) was declared and for each element Addnewquestion was called.

4.然后在表单中,声明模板类的数组(Questionarray),并为每个元素调用Addnewquestion。

5. The groupbox which was returned by the call was then attached to the form using Add function of the Controls collection.

5.然后,使用Controls集合的Add函数将调用返回的groupbox附加到表单。

The effect is the array shares code for the Eventhandler and we can identify each element uniquely using Array index as in VB 6.0

效果是数组共享事件处理程序的代码,我们可以像VB 6.0中一样使用数组索引唯一地标识每个元素

Public Class QuestionsArray
    Inherits System.Collections.CollectionBase
    Private mywindow As System.Windows.Forms.Form
    Public TrueButton As System.Windows.Forms.RadioButton
    Public FalseButton As System.Windows.Forms.RadioButton
    Public DButton As System.Windows.Forms.RadioButton
    Public AnswerGroup As System.Windows.Forms.GroupBox
    Public QstNo As System.Windows.Forms.Label
    Private Questionno As Integer
    Private Answer As String
    Sub New(ByVal Questionno1 As Integer, ByVal QSTWIND As System.Windows.Forms.Form)
        Questionno = Questionno1
        mywindow = QSTWIND
        TrueButton = New System.Windows.Forms.RadioButton
        FalseButton = New System.Windows.Forms.RadioButton
        DButton = New System.Windows.Forms.RadioButton
        AnswerGroup = New System.Windows.Forms.GroupBox
        QstNo = New System.Windows.Forms.Label
    End Sub
 
 
 
    Public Function AddNewQuestion(ByVal Questionno As Integer, ByVal toppos As Integer, ByVal leftpos As Integer) As System.Windows.Forms.GroupBox
 
        Dim a As EventArgs
        Dim leftst = 10
        AnswerGroup.Controls.Add(QstNo)
        AnswerGroup.Controls.Add(TrueButton)
        AnswerGroup.Controls.Add(FalseButton)
        AnswerGroup.Controls.Add(DButton)
        Dim ctlwidth As Integer
        Dim ctlspacing As Integer
 
        ctlwidth = 50
        ctlspacing = 60
        QstNo.Top = 15
        QstNo.Left = leftst
        QstNo.Width = 0.6 * ctlwidth
        QstNo.Text = Questionno.ToString() & "."
        TrueButton.Top = 10
        leftst = leftst   0.6 * ctlspacing
        TrueButton.Left = leftst
        TrueButton.Width = ctlwidth
        TrueButton.Height = 24
        TrueButton.Text = "True"
 
        AddHandler TrueButton.CheckedChanged, AddressOf AnswerkeysT
 
        FalseButton.Top = 10
        leftst = leftst   ctlspacing
        FalseButton.Left = leftst
        FalseButton.Width = ctlwidth
        FalseButton.Height = 24
        FalseButton.Text = "False"
        AddHandler FalseButton.CheckedChanged, AddressOf AnswerkeysF
        DButton.Top = 10
        leftst = leftst   ctlspacing
        DButton.Left = leftst
        DButton.Width = 2.5 * ctlwidth
        DButton.Height = 24
        DButton.Text = "Don't care/Pass"
        leftst = leftst   2.7 * ctlwidth
        AddHandler DButton.CheckedChanged, AddressOf AnswerkeysD
        AnswerGroup.Width = leftst
        AnswerGroup.Height = 40
        AddNewQuestion = AnswerGroup
    End Function
    Private Sub AnswerkeysT(ByVal sender As Object, ByVal e As System.EventArgs)
        Answer = "True"
    End Sub
 
    Private Sub AnswerkeysF(ByVal sender As Object, ByVal e As System.EventArgs)
        Answer = "False"
    End Sub
    Private Sub AnswerkeysD(ByVal sender As Object, ByVal e As System.EventArgs)
        Answer = "Do'nt Care"
    End Sub
End Class
 
Public Class Form1
    Inherits System.Windows.Forms.Form
 
#Region " Windows Form Designer generated code "
 
    Public Sub New()
        MyBase.New()
 
        'This call is required by the Windows Form Designer.
        InitializeComponent()
 
        'Add any initialization after the InitializeComponent() call
 
    End Sub
 
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
 
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
 
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.AutoScroll = True
        Me.ClientSize = New System.Drawing.Size(408, 414)
        Me.Name = "Form1"
        Me.Text = "Form1"
 
    End Sub
 
#End Region
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadRadioButtons()
    End Sub
 
 
    Private Sub LoadRadioButtons()
        Dim tempgroup As System.Windows.Forms.GroupBox
        Dim Thissession(600) As QuestionsArray
        Dim i As Integer
        Dim topst As Integer
        topst = 10
        i = 1
        For i = 0 To 599
            Thissession(i) = New QuestionsArray(i   1, Me)
            tempgroup = Thissession(i).AddNewQuestion(i   1, topst, 10)
            Me.Controls.Add(tempgroup)
            tempgroup.Top = topst
            topst = topst   50
            tempgroup.Left = 10
        Next
 
 
    End Sub
End Class

翻译自: https://www.experts-exchange.com/articles/1176/Implementing-control-arrays-in-VB-net.html

用vb怎么控制数位三用电表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值