|
VB 6 Helper Newsletter
|
Rod Stephens
|
Jan 31, 2009 06:45 PST
|
In non-programming-related news, I finished Terry Pratchett's "Nation."
As usual, a great book by Pratchett. Very different from most of his,
though. I got the feeling that he may have been facing his own
(everyone's) mortality as he wrote it.
http://www.amazon.com/exec/obidos/ASIN/0061433012/vbhelper/
See my other favorite books at http://www.BooksToKeep.com.
Rod
Have a great week and thanks for subscribing!
Rod
RodSte-@vb-helper.com
----------
*** Now Available ***
Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========
VB6 Contents:
1. Updated HowTo: Select an item from a ListBox or ComboBox with a given
ItemData value
2. New HowTo: Randomize the values in a series of TextBoxes, a ListBox,
or a single TextBox in Visual Basic 6
==========
++++++++++
<VB6>
++++++++++
==========
1. Updated HowTo: Select an item from a ListBox or ComboBox with a given
ItemData value
http://www.vb-helper.com/howto_select_item_from_item_data.html
http://www.vb-helper.com/HowTo/howto_select_item_from_item_data.zip
The example works with a ListBox or a ComboBox. Code shown is for a
ListBox.
Loop through the control's items examining their values. Select the
target item if it is found.
Private Sub optBook_Click(Index As Integer)
SelectItemData List1, Index
End Sub
' Search a control's ItemData property for the
' indicated ID. If found select the item.
Public Sub SelectItemData(ctl As Control, Id As Integer)
Dim nCnt As Long
If TypeName(ctl) = "ComboBox" Or TypeName(ctl) = "ListBox" Then
For nCnt = 0 To ctl.ListCount - 1
If ctl.ItemData(nCnt) = Id Then
ctl.ListIndex = nCnt
Exit For
End If
Next
End If
End Sub
Joanne James adapted this example to work in Visual Basic for
Applications with Access 2003.
Public Sub SelectItemData(ctl As Control, Id As Integer)
Dim nCnt As Long
If TypeName(ctl) = "ComboBox" Or TypeName(ctl) = _
"ListBox" Then
For nCnt = 0 To ctl.ListCount - 1
If ctl.ItemData(nCnt) = Id Then
ctl = ctl.ItemData(nCnt)
'ctl.ListIndex = nCnt 'can't set ListIndex
'ctl.Selected(nCnt + 1) = True 'this dont work although
MS help indicates it should in run time
'ctl.selectedItem = 2 'this property not supported
'ctl.selectedIndex = nCnt 'this property not supported
Exit For
End If
Next
End If
End Sub
(I tried to adapt this for Excel but the ListBox it uses by default is
very different. It doesn't even have an ItemData property.)
==========
2. New HowTo: Randomize the values in a series of TextBoxes, a ListBox,
or a single TextBox in Visual Basic 6
http://www.vb-helper.com/howto_randomize_textboxes_list.html
http://www.vb-helper.com/HowTo/howto_randomize_textboxes_list.zip
The RandomizeArray subroutine randomizes an array of integers. For each
position j in the array, it randomly selects an item that has not yet
been positioned and swaps it into position j.
' Randomize an array of numbers.
Public Sub RandomizeArray(items() As Integer)
Dim min_item As Integer
Dim max_item As Integer
Dim i As Integer
Dim j As Integer
Dim tmp_value As Integer
min_item = LBound(items)
max_item = UBound(items)
For i = min_item To max_item - 1
' Randomly assign item number i.
j = Int((max_item - i + 1) * Rnd + i)
tmp_value = items(i)
items(i) = items(j)
items(j) = tmp_value
Next i
End Sub
To randomize the values in a group of TextBoxes, the program loops
through the TextBoxes in a control array and copies their values into an
array.
It then calls RandomizeArray to randomize the values and copies the
randomizes values back into the TextBoxes.
' Randomize the numbers in the TextBoxes.
Private Sub cmdRandomizeTextBoxes_Click()
Dim values() As Integer
Dim i As Integer
' Load the valeus into an array.
ReDim values(txtNumber.LBound To txtNumber.UBound)
For i = txtNumber.LBound To txtNumber.UBound
values(i) = Val(txtNumber(i).Text)
Next i
' Randomize.
RandomizeArray values
' Display the results.
For i = txtNumber.LBound To txtNumber.UBound
txtNumber(i).Text = values(i)
Next i
End Sub
To randomize the values in a ListBox, the program loops through the
ListBox values and copies them into an array.
It then calls RandomizeArray to randomize the values and copies the
randomizes values back into the ListBox.
' Randomize the ListBox.
Private Sub cmdRandomizeListBox_Click()
Dim i As Integer
Dim values() As Integer
' Get the values.
ReDim values(0 To lstNumbers.ListCount - 1)
For i = 0 To lstNumbers.ListCount - 1
values(i) = lstNumbers.List(i)
Next i
' Randomize.
RandomizeArray values
' Display the results.
lstNumbers.Clear
For i = LBound(values) To UBound(values)
lstNumbers.AddItem values(i)
Next i
End Sub
To randomize the values in a single TextBox, the program uses Split to
split the values separated by commas apart. It loops through the values
and copies them into an array.
It then calls RandomizeArray to randomize the values and copies the
randomizes values back into the TextBox.
' Randomize the numbers in the single TextBox.
Private Sub cmdRandomizeTextBox_Click()
Dim numbers() As String
Dim i As Integer
Dim values() As Integer
Dim txt As String
' Get the values.
numbers = Split(txtNumbers.Text, ",")
ReDim values(LBound(numbers) To UBound(numbers))
For i = LBound(numbers) To UBound(numbers)
values(i) = Val(numbers(i))
Next i
' Randomize.
RandomizeArray values
' Display the results.
txt = ""
For i = LBound(values) To UBound(values)
txt = txt & ", " & values(i)
Next i
If Len(txt) > 0 Then txt = Mid$(txt, 2)
txtNumbers.Text = txt
End Sub
==========
Archives:
http://www.topica.com/lists/VBHelper
http://www.topica.com/lists/VB6Helper
http://www.topica.com/lists/VBNetHelper
Post questions at:
http://www.topica.com/lists/VBHelperQA
|
|
 |
|