[英]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.