|
VB 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
VB.NET Contents:
3. Updated HowTo: Make a drawing application in VB .NET
4. New HowTo: Let the user click to draw markers on a map in Visual
Basic 2005
==========
++++++++++
<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
==========
++++++++++
<VB.NET>
++++++++++
==========
3. Updated HowTo: Make a drawing application in VB .NET
http://www.vb-helper.com/howto_net_drawing_framework.html
http://www.vb-helper.com/HowTo/howto_net_drawing_framework.zip
This is a very complex application so its Web page only describes key
points. This newsletter entry only describes the new feature I just
added. Go to http://www.vb-helper.com/howto_net_drawing_framework.html
for more information and to download the example.
To let the user move objects, the program looks for MouseMove events. If
the left button is down, the main form calls the picture's
MoveSelectedDrawableToMouse method to move the currently selected
Drawable. It then invalidates the picture to redraw it.
' If we have an object selected, move it.
Private Sub picCanvas_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
picCanvas.MouseMove
' Only move if the left button is down.
If e.Button = Windows.Forms.MouseButtons.Left Then
' Move it.
m_Picture.MoveSelectedDrawableToMouse(e.X, e.Y)
' Redraw to show the new position.
picCanvas.Invalidate()
End If
End Sub
The DrawablePicture's MoveSelectedDrawableToMouse method exits if no
Drawable is selected.
If an object is selected, the program calculates the distance the mouse
has been moved and calls the selected Drawable's MoveRelative method to
move it by a corresponding amount. It then saves the new mouse position
for future moves.
(Note: The mouse's position is saved in (m_SelectedMouseX,
m_SelectedMouseY) when a Drawable is selected so it is ready to move.)
' Move the selected drawable. The mouse has moved from
' (m_SelectedMouseX, m_SelectedMouseY) to (x, y).
Public Sub MoveSelectedDrawableToMouse(ByVal x As Integer, _
ByVal y As Integer)
' Do nothing if nothing is selected.
If SelectedDrawable Is Nothing Then Exit Sub
' See how far we want it moved.
Dim new_dx As Integer = x - m_SelectedMouseX
Dim new_dy As Integer = y - m_SelectedMouseY
' Move it.
SelectedDrawable.MoveRelative(new_dx, new_dy)
' Save the new mouse position.
m_SelectedMouseX = x
m_SelectedMouseY = y
End Sub
==========
4. New HowTo: Let the user click to draw markers on a map in Visual
Basic 2005
http://www.vb-helper.com/howto_2005_marker_on_map.html
http://www.vb-helper.com/HowTo/howto_2005_marker_on_map.zip
The program declares a generic List(Of Rectangle) to hold information
about the markers.
When the user clicks on the map, the program adds a new Rectangle to the
list, placing it at the location the user clicked.
The map's Paint event handler loops through the markers drawing them.
Finally the Clear Markers button empties the list of markers.
Private m_Markers As New List(Of Rectangle)
' Add a marker.
Private Sub picMap_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles picMap.MouseClick
Const WID As Integer = 7
m_Markers.Add(New Rectangle(e.X - WID \ 2, e.Y - WID \ 2, WID, WID))
' Redraw.
picMap.Invalidate()
End Sub
' Draw the markers.
Private Sub picMap_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles picMap.Paint
For Each rect As Rectangle In m_Markers
e.Graphics.FillEllipse(Brushes.Red, rect)
e.Graphics.DrawEllipse(Pens.Black, rect)
Next rect
End Sub
' Clear the markers.
Private Sub btnClearMarkers_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnClearMarkers.Click
m_Markers.Clear()
' Redraw.
picMap.Invalidate()
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
|
|
 |
|