Public Class webcontrols
Public Class dropDownListUtilities
Public Sub setState(ByVal state As String, ByRef list As System.Web.UI.WebControls.DropDownList)
Dim lArray() As String
If state.ToLower() = "false" Then
state = "0"
ElseIf state.ToLower() = "true" Then
state = "1"
End If
Dim item As New System.Web.UI.WebControls.ListItem()
For Each item In list.Items
If state = item.Value Then
item.Selected = True
Else
item.Selected = False
End If
Next
If noneSelected(list) Then
list.SelectedIndex = 0
End If
End Sub
Public Function noneSelected(ByVal list As System.Web.UI.WebControls.DropDownList)
Dim item As New System.Web.UI.WebControls.ListItem()
Dim output As Boolean = True
For Each item In list.Items
If item.Selected Then
output = False
End If
Next
Return output
End Function
Public Function returnIndex(ByVal box As DropDownList, ByVal text As String) As Integer
Dim output As Integer = 0
Dim i As Integer = 0
Dim item As New ListItem()
For Each item In box.Items
If text = item.Text Then
output = i
Exit For
End If
i += 1
Next
Return output
End Function
Public Sub insertEntry(ByRef box As System.Web.UI.WebControls.DropDownList, ByVal txt As String, _
ByVal value As String, Optional ByVal selectIt As Boolean = False)
Dim item As New System.Web.UI.WebControls.ListItem()
item.Text = txt
item.Value = value
box.Items.Insert(0, item)
If selectIt Then
box.SelectedIndex = 0
End If
End Sub
Public Sub selectByValue(ByVal value As Object, ByVal list As DropDownList)
'FIRST, UNSELECT ANY THAT MIGHT BE SELECTED
list.SelectedIndex = -1
If Not IsDBNull(value) Then
If Not value = String.Empty Then
Try
list.Items.FindByValue(value).Selected = True
Catch
list.SelectedIndex = -1
End Try
Else
insertEntry(list, "Select One", "0", True)
End If
Else
insertEntry(list, "Select One", "0", True)
End If
End Sub
Public Sub selectByText(ByVal text As String, ByVal list As DropDownList)
'FIRST, UNSELECT ANY THAT MIGHT BE SELECTED
list.SelectedIndex = -1
If Not IsDBNull(text) Then
If Not text = String.Empty Then
Try
list.Items.FindByText(text).Selected = True
Catch
list.SelectedIndex = -1
End Try
Else
insertEntry(list, "Select One", "0", True)
End If
Else
insertEntry(list, "Select One", "0", True)
End If
End Sub
End Class
Public Class listBoxUtilities
Public Sub setState(ByVal state As String, ByRef box As System.Web.UI.WebControls.ListBox)
Dim lArray() As String
lArray = state.Split(",")
Dim item As New System.Web.UI.WebControls.ListItem()
For Each item In box.Items
If exists(item.Value, lArray) Then
item.Selected = True
Else
item.Selected = False
End If
Next
End Sub
Public Function noneSelected(ByVal box As System.Web.UI.WebControls.ListBox)
Dim item As New System.Web.UI.WebControls.ListItem()
Dim output As Boolean = True
For Each item In box.Items
If item.Selected Then
output = False
End If
Next
Return output
End Function
Public Function getCSV(ByVal box As System.Web.UI.WebControls.ListBox)
Dim item As New ListItem()
Dim output As String
For Each item In box.Items
If item.Selected Then
output = output & item.Value & ","
End If
Next
output = output.Substring(0, output.Length - 1)
Return output
End Function
Public Sub insertEntry(ByRef box As System.Web.UI.WebControls.ListBox, ByVal txt As String, _
ByVal value As String, Optional ByVal selectIt As Boolean = False)
Dim item As New System.Web.UI.WebControls.ListItem()
item.Text = txt
item.Value = value
box.Items.Insert(0, item)
If selectIt Then
box.SelectedIndex = 0
End If
End Sub
End Class
Shared Function exists(ByVal searchString As String, ByVal lArray() As String) As Boolean
Dim i As Integer
Dim output As Boolean = False
For i = 0 To lArray.Length - 1
If lArray(i) = searchString Then
output = True
End If
Next
Return output
End Function
End Class