简体   繁体   中英

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. 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. 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. The problem is when I passed a checkbox I can't get the checked event to show on my control(CoreCol) that I passed. 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.

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). The above code assumes this will work, but I am not sure what is in 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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