繁体   English   中英

当我在 vb.net 中将它作为参数传递时,如何触发 checkbox.checked 事件?

[英]How can I get the checkbox.checked event to fire when I pass it as a parameter in vb.net?

我已经编程了很长时间,但我对 vb.net 比较陌生。我避免了传递参数的子程序和函数,因为我总是被卡住。 我正在尝试编写一个子例程来传递信息,这些信息将使用表中的值或清除字段或设置为 false 来填充文本框或复选框。 下面的第一个代码是我一直在做的事情的一个例子,它是有效的。 我试图编写一个子例程来传递 1.表单上文本框或复选框控件的名称,2.数据行值,以及 3.表中的列名。 问题是当我通过一个复选框时,我无法让选中的事件显示在我通过的控件(CoreCol)上。 它知道它是一个复选框,它会将复选框的文本设置为太真或太假,但它不会更改选中的框。

这是旧方法的一个例子。 对于文本框和复选框

' A Machine
If Not IsDBNull(r("A Machine")) Or Not IsNothing(r("A Machine")) Then
    TbXMachA.Text = r("A Machine")
Else
    TbXMachA.Text = ""
End If
If Not IsDBNull(r("A CO2 Box?")) Or Not IsNothing(r("A CO2 Box?")) Then
    CkbxCO2BoxA.Checked = r("A CO2 Box?")
Else
    CkbxCO2BoxA.Checked = False
End If

这行得通

LoadData2TextBox(Me. TbXMachA, r, "A Machine ")

这不

LoadData2TextBox(Me.CkbxCO2BoxA, r, "A CO2 Box?")

这是我正在写的子例程

Private Sub LoadData2TextBox(ByRef CoreCol As Control, CoreRow As DataRow, BoxStage As String)                
    If Not IsDBNull(CoreRow(BoxStage)) Then
        If TypeOf CoreCol Is TextBox Then
            CoreCol.Text = CoreRow(BoxStage)
        End If 
        If TypeOf CoreCol Is CheckBox Then
            CoreCol.??? = CoreRow(BoxStage)
        End If
    Else
        CoreCol.Text = ""
    End If

您知道 CoreCol 是一个 CheckBox,因此您可以其转换为一个,然后将其用作 CheckBox。

If TypeOf CoreCol Is CheckBox Then
    Dim myCheckBox = DirectCast(CoreCol, CheckBox)
    myCheckBox.Checked = DirectCast(CoreRow(BoxStage), Boolean)
End If

从 CoreRow(BoxStage) 中获取 boolean 值的另一个转换。 上面的代码假设这会起作用,但我不确定 CoreRow(BoxStage) 中有什么。 您可能需要根据值添加一些逻辑,具体取决于它是什么。 例如:

myCheckBox.Checked = CoreRow(BoxStage) = "somevalue"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM