Saturday, October 15, 2011

Saturday 10.15.11

Imports System

Namespace Indexers
'a simplified ListBox control
Public Class ListBoxTest
Private strings(255) As String
Private ctr As Integer = 0

'initialize the list box with strings
Public Sub New(ByVal ParamArray initialStrings() As String)
Dim s As String
'copy the strings passed in to the constructor
For Each s In initialStrings
strings(ctr) = s
ctr += 1
Next
End Sub

'add a single string to the end of the list box
Public Sub Add(ByVal theString As String)
If ctr >= strings.Length Then
'handle bad index
Else
strings(ctr) = theString
ctr += 1
End If
End Sub

'allow array-like access
Default Public Property Item(ByVal index As Integer) As String
Get
If index < 0 Or index >= strings.Length Then
'handle bad index
Else
Return strings(index)
End If
End Get
Set(ByVal value As String)
If index >= ctr Then
'handle error
Else
strings(index) = value
End If
End Set
End Property

'index on string
Default Public Property Item(ByVal index As String) As String
Get
If index.Length = 0 Then
'handle bad index
Else
Return strings(findString(index))
End If
End Get
Set(ByVal value As String)
strings(findString(index)) = value
End Set
End Property

'helper method given a string find first matchign record that starts with the target
Private Function findString(ByVal searchString As String) As Integer
Dim i As Integer
For i = 0 To strings.Length - 1
If strings(i).StartsWith(searchString) Then
Return i
End If
Next
Return -1
End Function

'publish how many strings you hold
Public Function Count() As Integer
Return ctr
End Function
End Class

Public Class tester


Public Sub Run()
'craete a new list box and initialize
Dim lbt As New ListBoxTest("Hello", "World")
Dim i As Integer

Console.WriteLine("After creation...")
For i = 0 To lbt.Count - 1
Console.WriteLine("lbt({0}): {1}", i, lbt(i))
Next
'add a few strings
lbt.Add("Who")
lbt.Add("Is")
lbt.Add("John")
lbt.Add("Galt")

Console.WriteLine(vbCrLf & "After adding strings...")
For i = 0 To lbt.Count - 1
Console.WriteLine("lbt({0}): {1}", i, lbt(i))
Next

'test the access
Dim subst As String = "Universe"
lbt(1) = subst
lbt("Hel") = "GoodBye"

'access all the strings
Console.WriteLine(vbCrLf & "After editing strings...")
For i = 0 To lbt.Count - 1
Console.WriteLine("lbt({0}): {1}", i, lbt(i))
Next
End Sub

Public Shared Sub Main()
Dim t As New tester
t.Run()
End Sub
End Class

End Namespace


Partial Class SelectableList
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If (Not Page.IsPostBack) Then
For i As Integer = 3 To 5
ListBox1.Items.Add("Option " & i.ToString())
DropdownList1.Items.Add("Option " & i.ToString())
CheckboxList1.Items.Add("Option " & i.ToString())
RadiobuttonList1.Items.Add("Option " & i.ToString())
Next i
End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("<b>Selected items for Listbox1:</b><br />")
For Each li As ListItem In ListBox1.Items
If li.Selected Then
Response.Write("- " & li.Text & "<br />")
End If
Next
Response.Write("<b>Selected item for DropdownList1:</b><br/>")
Response.Write("- " & DropdownList1.SelectedItem.Text & "<br />")

Response.Write("<b>selected items for CheckboxList1:</b><br />")
For Each li As ListItem In CheckboxList1.Items
If li.Selected Then
Response.Write("- " & li.Text & "<br />")
End If
Next
End Sub

End Class

No comments: