Vb.net 有没有可能在一个类内定义和控制事物,而不用在VB中以“形式”(类外)赋值?

Vb.net 有没有可能在一个类内定义和控制事物,而不用在VB中以“形式”(类外)赋值?,vb.net,Vb.net,我的问题是: 我有一个复选框,用来控制某些文本框是否启用,我需要做30多次。我以数字/顺序将文本框命名为TB_name_1、TB_name_2等,因此如果我知道复选框名称,我就知道哪些文本框受到影响 我的问题: 我可以为我的复选框创建一个类,说明如果这个复选框被选中/取消选中,那么启用/禁用这3个文本框,而不必告诉类哪些文本框是自己找到的吗 这是我目前使用的复制/粘贴代码,显然不是一个类。我更改了前2个值,代码的其余部分自行求解。我看见你在笑 Private Sub T1_cb_c_1_Chec

我的问题是:

我有一个复选框,用来控制某些文本框是否启用,我需要做30多次。我以数字/顺序将文本框命名为TB_name_1、TB_name_2等,因此如果我知道复选框名称,我就知道哪些文本框受到影响

我的问题:

我可以为我的复选框创建一个类,说明如果这个复选框被选中/取消选中,那么启用/禁用这3个文本框,而不必告诉类哪些文本框是自己找到的吗

这是我目前使用的复制/粘贴代码,显然不是一个类。我更改了前2个值,代码的其余部分自行求解。我看见你在笑

Private Sub T1_cb_c_1_CheckedChanged(sender As Object, e As EventArgs) Handles T1_cb_c_1.CheckedChanged

    'change here for current checkbox
    Dim b As CheckBox = T1_cb_c_1

    'change here for start value of first textbox (of 3), the next 2 will be in sequence
    Dim a As Integer = 1

    'How much of the below code can be moved to, and controlled from, a class?
    Dim a1 As Integer = a + 1
    Dim a2 As Integer = a + 2

    Dim TB_PtNum As TextBox = Me.Controls.Find("T1_tb_c_" & a, True).FirstOrDefault
    Dim TB_Qty As TextBox = Me.Controls.Find("T1_tb_c_" & a1, True).FirstOrDefault
    Dim TB_Seq As TextBox = Me.Controls.Find("T1_tb_c_" & a2, True).FirstOrDefault

    If b.Checked = True Then
        TB_PtNum.Enabled = True
        TB_Qty.Enabled = True
        TB_Seq.Enabled = True
    Else
        TB_PtNum.Enabled = False
        TB_Qty.Enabled = False
        TB_Seq.Enabled = False
    End If

End Sub
我会使用相关的控件。 假设为第一组文本框和控制它们的复选框将此属性设置为值line1。 下一行控件复选框+文本框的属性将设置为line2,依此类推,直到最后一行。您可以通过Winforms设计器或代码来完成此操作


此时,您可以为所有复选框使用单个事件处理程序

Private Sub onCheckedChanged(sender As Object, e As EventArgs) _
    Handles T1_cb_c_1.CheckedChanged, T2_cb_c_2.CheckedChanged, _
    ..... add other checkbox events here .......

    ' Get whatever checkbox has been clicked and extract its tag
    Dim b As CheckBox = DirectCast(sender, CheckBox)
    Dim tag = b.Tag.ToString()

    ' Find the textbox controls in this form with the same Tag
    Dim ctrls = Me.Controls.OfType(Of TextBox).Where(Function(x) x.Tag.ToString() = tag)

    ' Enabled status matches the status of the Checked property
    For Each c as TextBox in ctrls 
       c.Enabled = b.Checked
    Next
End Sub

这里有一个只在设计时使用的类可以实现这一点。您只需在设计器中设置AssociatedCheckbox属性:

Public Class TextBoxWithCheckboxProperty
    Inherits TextBox

    Private m_CheckBox As CheckBox

    Public Property AssociatedCheckBox As CheckBox
        Get
            Return m_CheckBox
        End Get
        Set(value As CheckBox)
            If Not m_CheckBox Is Nothing Then
                RemoveHandler m_CheckBox.CheckedChanged, AddressOf OnCheckBoxChanged
            End If
            m_CheckBox = value
            If Not value Is Nothing Then
                AddHandler m_CheckBox.CheckedChanged, AddressOf OnCheckBoxChanged
            End If

            OnCheckBoxChanged(m_CheckBox, Nothing)
        End Set
    End Property

    Private Sub OnCheckBoxChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not sender Is Nothing Then
            Me.Enabled = CType(sender, CheckBox).Checked
        Else
            Me.Enabled = False

        End If
    End Sub

End Class
下面是使用它的示例Form1:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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()
        Me.CheckBox1 = New System.Windows.Forms.CheckBox()
        Me.TextBoxWithCheckboxProperty1 = New WindowsApp4.TextBoxWithCheckboxProperty()
        Me.SuspendLayout()
        '
        'CheckBox1
        '
        Me.CheckBox1.AutoSize = True
        Me.CheckBox1.Location = New System.Drawing.Point(293, 131)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.Size = New System.Drawing.Size(81, 17)
        Me.CheckBox1.TabIndex = 0
        Me.CheckBox1.Text = "CheckBox1"
        Me.CheckBox1.UseVisualStyleBackColor = True
        '
        'TextBoxWithCheckboxProperty1
        '
        Me.TextBoxWithCheckboxProperty1.AssociatedCheckBox = Me.CheckBox1
        Me.TextBoxWithCheckboxProperty1.Location = New System.Drawing.Point(428, 131)
        Me.TextBoxWithCheckboxProperty1.Name = "TextBoxWithCheckboxProperty1"
        Me.TextBoxWithCheckboxProperty1.Size = New System.Drawing.Size(100, 20)
        Me.TextBoxWithCheckboxProperty1.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 450)
        Me.Controls.Add(Me.TextBoxWithCheckboxProperty1)
        Me.Controls.Add(Me.CheckBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents CheckBox1 As CheckBox
    Friend WithEvents TextBoxWithCheckboxProperty1 As TextBoxWithCheckboxProperty
End Class

您是否正在寻找一种通用方法来启用/禁用链接到复选框的3个文本框,例如,如果用户取消选中该行开头的复选框,则不应编辑的数据行?此时,您可以为所有复选框使用单个事件处理程序-这是我尝试实现的最终目标,我只是在尝试“类”路径,因为我根本没有使用它们,并且认为尝试会很有趣。黄金法则-K.I.S.S.保持简单和愚蠢是的,你可以为所有复选框使用一个事件处理程序。只需将复选框列表添加到处理程序中。