简体   繁体   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?

I have programmed a long time but I'm relatively new to vb.net. And I've avoided subroutines and functions in which I passed parameters because I always get stuck.我已经编程了很长时间,但我对 vb.net 比较陌生。我避免了传递参数的子程序和函数,因为我总是被卡住。 I'm trying to write a subroutine to pass information that will fill a TextBox or a checkbox with either the value from a table or clear the field or set to false.我正在尝试编写一个子例程来传递信息,这些信息将使用表中的值或清除字段或设置为 false 来填充文本框或复选框。 The first code below is an example of what I've been doing and this works.下面的第一个代码是我一直在做的事情的一个例子,它是有效的。 I trying to write a subroutine to pass 1.the name of the textbox or checkbox control on my form,2.the data row value, and 3.the column name in the table.我试图编写一个子例程来传递 1.表单上文本框或复选框控件的名称,2.数据行值,以及 3.表中的列名。 The problem is when I passed a checkbox I can't get the checked event to show on my control(CoreCol) that I passed.问题是当我通过一个复选框时,我无法让选中的事件显示在我通过的控件(CoreCol)上。 It knows it's a checkbox and it will set the text of the checkbox too true or false but it won't change the box checked.它知道它是一个复选框,它会将复选框的文本设置为太真或太假,但它不会更改选中的框。

This is an example of the old way that works.这是旧方法的一个例子。 For a TextBox and a checkbox对于文本框和复选框

' 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

This works这行得通

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

This doesn't这不

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

this is the sub routine I'm writing这是我正在写的子例程

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

You know that CoreCol is a CheckBox so you can cast it as one then use it as a CheckBox.您知道 CoreCol 是一个 CheckBox,因此您可以其转换为一个,然后将其用作 CheckBox。

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

Another cast in getting the boolean value out of CoreRow(BoxStage).从 CoreRow(BoxStage) 中获取 boolean 值的另一个转换。 The above code assumes this will work, but I am not sure what is in CoreRow(BoxStage).上面的代码假设这会起作用,但我不确定 CoreRow(BoxStage) 中有什么。 You may need to add some logic based on the value depending on what it is.您可能需要根据值添加一些逻辑,具体取决于它是什么。 For example:例如:

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

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

相关问题 在 VB.Net 中分配 Checkbox.checked 值调用已检查事件 - Assign Checkbox.checked value invoke checked event in VB.Net 如何防止CheckBox.Checked的赋值语句引发CheckChanged事件? - How can I prevent an assignment statement to CheckBox.Checked from raising the CheckChanged event? 如何根据“是”/“否”列更改 VB.NET 中的 CheckBox.Checked - How to change CheckBox.Checked in VB.NET based on “yes”/“no” column 在事件ASP.NET VB上重定向时,Button.Enabled和checkbox.Checked状态被删除 - Button.Enabled and checkbox.Checked status erased when redirecting on event ASP.NET VB 如何在VB.NET代码中触发事件? - How do I fire an event in VB.NET code? 在VB.NET中选中多个复选框时,如何显示复选框的最高平均值? - How do I display the highest average of checkbox when multiple checkboxes checked in VB.NET? 我如何检查vb.net的datagridview列中的复选框是否已选中 - how can i check if checkbox is checked in datagridview column in vb.net Vb.net更改相应checkbox.checked = true的picturebox的背景颜色 - Vb.net Change the back color of picturebox whose corresponding checkbox.checked = true 我可以将变量ByRef传递给vb.net中的计时器事件吗? - Can I Pass a variable ByRef to a timer event in vb.net? 如何在Vb.Net中获取Checked CheckBox标记值? - How to get Checked CheckBox tag value in Vb.Net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM