首页 > 其他 > 详细

根据所需要设定的值,设置 ComboBox 或者 CheckedListBox的选中项,比如: setBoxes(cboDETR, "01")

时间:2020-01-02 13:01:30      阅读:84      评论:0      收藏:0      [点我收藏+]
  ‘‘‘ <summary>
    ‘‘‘    根据所需要设定的值,设置 ComboBox 或者 CheckedListBox的选中项
    ‘‘‘ </summary>
    ‘‘‘ <typeparam name="T">泛型</typeparam>
    ‘‘‘ <param name="_control">控件</param>
    ‘‘‘ <param name="_setValue">需要设定的值</param>
    ‘‘‘ <returns></returns>
    ‘‘‘ <remarks></remarks>
    Private Function setBoxes(Of T)(_control As T, _setValue As String) As Boolean
        Dim ty As Type = _control.GetType()    ‘  这种控件的具体类型                ‘MessageBox.Show(dd.ToString)
        Dim o As Object = _control
        Dim strsType As String() = ty.ToString.Split(".")   ‘  转化为字符串数组
        Select Case strsType(strsType.Length - 1).Trim
            Case "ComboBox", "myComboBox"      ‘  如果控件类型是 ComboBox   或者  myComboBox
                Dim pCombo As ComboBox = DirectCast(o, ComboBox)
                Dim ie As System.Collections.IEnumerator = pCombo.Items.GetEnumerator()
                ie.Reset()
                Dim ii As Integer = -1
                While (ie.MoveNext)
                    ii += 1
                    ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
                    ‘  ComboBox,其有些是直接设定其item的,比如:  cboDETR.Items.Add("01 非特别紧急")
                    ‘                                             cboDETR.Items.Add("02 特别紧急")
                    ‘     对于这种 ComboBox ,用下面的方法处理
                    Dim strs As String() = ie.Current.GetType().ToString().Split(".")
                    If strs(strs.Length - 1).Trim = "String" Then  ‘  如果是字符串类型
                        If getItemFirstPart(ie.Current) = _setValue Then
                            pCombo.SelectedIndex = ii
                            Exit While   ‘   脱离while循环
                        End If
                        Continue While   ‘  跳过去
                    End If

                    ‘ 如果 ComboBox 的数据源是来源于数据库查询的,用下面的方法来处理
                    Dim r As DataRowView = ie.Current
                    ‘Dim arr As Object() = r.Row.ItemArray
                    ‘For ii = 0 To arr.Length - 1
                    ‘    MessageBox.Show(ii & "--" & arr(ii))
                    ‘Next
                    ‘Dim strNo = r.Row.ItemArray(0).ToString.Trim
                    ‘  上面, r.Row.ItemArray(0)的格式:   99
                    ‘  下面, r.Row.ItemArray(1)的格式:   99  报告人民银行*** 
                    ‘    效果是一样的
                    Dim strNo = getItemFirstPart(r.Row.ItemArray(1).ToString)
                    If strNo = _setValue Then
                        pCombo.SelectedIndex = ii
                        Exit While
                    End If
                End While

            Case "CheckedListBox" ‘  如果控件类型是 CheckedListBox
                Dim strs As String() = _setValue.Split(",")    ‘   根据逗号对输入字符串进行分割
                For ii As Integer = 0 To strs.Length - 1       ‘      对于得到的 字符串数组
                    strs(ii) = strs(ii).Trim        ‘  去掉其两端的 空格
                Next
                Dim pClb As CheckedListBox = DirectCast(o, CheckedListBox)
                With pClb
                    For ii As Integer = 0 To .Items.Count - 1     ‘  对于 CheckedListBox 的每个选项
                        Dim strNo As String = getItemFirstPart(.GetItemText(.Items(ii)))     ‘    获取其前端的编号
                        If strs.Contains(strNo) Then         ‘  如果  字符串数组中有这个编号
                            ‘MessageBox.Show(strNo)
                            pClb.SetItemChecked(ii, True)       ‘   将这一项选中
                        Else
                            pClb.SetItemChecked(ii, False)      ‘   否则 不要选中
                        End If
                    Next
                End With
            Case Else   ‘  其他类型
                MessageBox.Show(" setBoxes 不能处理的控件类型:" & ty.ToString())
        End Select
        Return True
    End Function
 
‘‘‘ <summary>
    ‘‘‘   获取 字符串 前部的编号
    ‘‘‘ </summary>
    ‘‘‘ <param name="strItem">所输入的字符串,例如: 0101 公民身份证(中间有个空格) </param>
    ‘‘‘ <returns>空格前面的部分,编号</returns>
    ‘‘‘ <remarks></remarks>
    Private Function getItemFirstPart(strItem As String) As String
        Dim strs As String() = strItem.Split(" ")   ‘  按照空格进行拆分
        If strs.Length > 1 Then
            Return strs(0).Trim
        Else
            Return ""
        End If
    End Function

根据所需要设定的值,设置 ComboBox 或者 CheckedListBox的选中项,比如: setBoxes(cboDETR, "01")

原文:https://www.cnblogs.com/gaoleionline/p/12131772.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!