在 vb.net 函数中将任何控件作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13207961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:00:33  来源:igfitidea点击:

passing any control as parameter in vb.net function

vb.netfunctionparameterscontrols

提问by user1819920

I have a following vb.net function as below in which i am passing a checkbox control name as parameter.Code is here

我有一个如下的 vb.net 函数,其中我将复选框控件名称作为参数传递。代码在这里

Public Function emaildata(ByVal grdv As GridView, ByVal ctrl As String, ByVal celpos As Integer) As GridView
        Dim comm As OleDbCommand = New OleDbCommand()
        Dim bpv As String = ""
        Dim gv As New GridView
        For Each gvrow As GridViewRow In grdv.Rows
            Dim chkbx As CheckBox = CType(gvrow.FindControl(ctrl), CheckBox)
            If chkbx.Checked Then
                If bpv <> "" Then
                    bpv += ","
                End If
                bpv += gvrow.Cells(celpos).Text
                comm.CommandText = "SELECT chq_num Cheque#,to_char(bpv_amt,'9,999,999,999') Amount,vch_nar Narration,bnf_nam PartyName,acc_des Bank from  CHECK_DATA where bpv_num in(" & bpv.ToString() & ") and BPV_DTE=to_date('" & TreeView2.SelectedValue & "')"
                comm.CommandType = CommandType.Text
                comm.Connection = con
                Dim da As New OleDbDataAdapter(comm)
                Dim ds As New DataSet
                da.Fill(ds)
                gv.DataSource = ds
                gv.DataBind()
            End If
        Next
        Return gv
    End Function

Problem is that i have to use same function with radiobutton also with text box and i don't want to write separate function for all type of controls.I want to detect the control as parameter and.For Example if i pass text box then function should behave like a text box and if radio then radion behaviour and if checkbox then same behaviour for this.I have these three control to pass the function and i want make a automatic detection method for these controls

问题是我必须对单选按钮和文本框使用相同的功能,我不想为所有类型的控件编写单独的函数。我想检测控件作为参数和。例如,如果我传递文本框然后函数应该表现得像一个文本框,如果是单选则是单选行为,如果复选框则是同样的行为。我有这三个控件来传递函数,我想为这些控件制定一个自动检测方法

采纳答案by andy

You need to send ctrl as CONTROL(its a base class for all controls) as parameter.

您需要将 ctrl 作为 CONTROL(它是所有控件的基类)作为参数发送。

You need to use latebinding and need to write separate cases for each control type....

您需要使用后期绑定并且需要为每个控件类型编写单独的案例....

Below code will work for checkbox only

以下代码仅适用于复选框

Dim chkbx As CheckBox = CType(gvrow.FindControl(ctrl), CheckBox)
If chkbx.Checked Then

For textbox and radiobutton you need write additional code

对于文本框和单选按钮,您需要编写额外的代码

Public Function emaildata(ByVal grdv As GridView, ByVal ctrl As Control, ByVal celpos As Integer) As GridView

 If TypeOf ctrl Is Button Then

 ElseIf TypeOf ctrl Is RadioButton Then 

 Else

 EndIf