|
VB Helper Newsletter
|
Rod Stephens
|
Oct 16, 2009 14:17 PDT
|
Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/
----------
DevX posted another of my articles:
WPF Wonders: Building Control Templates
http://www.devx.com/dotnet/Article/42947
October 6, 2009
WPF's properties and styles let you change a control's appearance.
Templates let you modify a control at a much more fundamental level,
changing the components that make up the control and the way it acts.
This article shows how you can use templates to change the fundamental
structure of WPF controls. Examples show how to build a Label that wraps
text and ends long text with an ellipsis, an interesting button, and a
distinctive scroll bar.
They're pretty cool and easy to use.
----------
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
----------
Last week I whined about the fact that VB .NET doesn't have reference
counting. John Mueller (all around great guy and the most prolific
non-romance writer alive) sent this reply:
Really cool example this week! I agree, leaving reference counting
out of VB.NET was a mistake. However, I don't think Microsoft had a
choice in the matter (please correct me if I'm wrong). I read an
article recently about the whole threading issue at:
http://visualstudiomagazine.com/Articles/2009/10/01/Threading-and-the-UI.aspx
If you read that article completely and then start reviewing the
free threading model, you'll begin to understand why moving from
apartment-model threading in VB6 to free threading in VB.NET is such a
big deal. Reference counting would seem impossible in VB.NET simply
because of the way the threading model works. Of course, you get a
whole bunch of positive things with free threading as well, so I think
the move was a good one, but not completely good--we had to give
something up. As the author of that article states, "There is no free
lunch" <g>.
I'm not completely convinced but I can see how reference counting would
mean a lot more cross-thread synchronization and it makes more sense
than the official line Microsoft has always given when asked about
garbage collection. They say it's better because it handles reference
loops (where object A points to object B, which points back at object
A). Yes, they are theoretically a problem but I've never actually seen
one cause trouble in the wild.
Check out John's Web site at:
http://www.mwt.net/~jmueller/
Have a great week and thanks for subscribing!
----------
Rod
RodSte-@vb-helper.com
Books To Keep: http://www.BooksToKeep.com
----------
==========
VB.NET Contents:
1. New HowTo: Drag and drop strings from one ListBox to another in
Visual Basic .NET
2. New HowTo: Drag and drop objects from one ListBox to another in
Visual Basic .NET
3. New HowTo: Drag and drop one or more multi-column values from one
ListView control to another in Visual Basic .NET
==========
++++++++++
<VB.NET>
++++++++++
==========
1. New HowTo: Drag and drop strings from one ListBox to another in
Visual Basic .NET
http://www.vb-helper.com/howto_net_drag_drop_listboxes.html
http://www.vb-helper.com/HowTo/howto_net_drag_drop_listboxes.zip
When you press the right mouse button over a ListBox, the MouseDown
event handler starts the drag by calling the control's DoDragDrop
method. It passes in the selected string to drag and allows the copy and
move actions.
The call to DoDragDrop returns when the drag is finished. The method
returns a value indicating whether the drag resulted in a move, copy, or
nothing (in this case). If the drag ended with a move, then the code
removes the item from the ListBox that started the drag.
' Start a drag.
Private Sub lst_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lstFruits.MouseDown,
lstAnimals.MouseDown
Dim lst As ListBox = DirectCast(sender, ListBox)
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim index As Integer = lst.IndexFromPoint(e.X, e.Y)
If index <> ListBox.NoMatches Then
Dim item As String = lst.Items(index)
Dim drop_effect As DragDropEffects = _
lst.DoDragDrop( _
lst.Items(index), _
DragDropEffects.Move Or DragDropEffects.Copy)
' If it was moved, remove the item from this list.
If drop_effect = DragDropEffects.Move Then
' See if the user dropped the item in this ListBox
' at a higher position.
If lst.Items(index) = item Then
' The item has not moved.
lst.Items.RemoveAt(index)
Else
' The item has moved.
lst.Items.RemoveAt(index + 1)
End If
End If
End If
End If
End Sub
If the drag moves over a ListBox, the DragOver event gives the control a
chance to allow or prohibit the drag. The following code checks whether
the drag can provide a string as data. If so, then it checks whether the
Ctrl key is down and allows a move or copy effect.
' Display the appropriate cursor.
Private Sub lstFruits_DragOver(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lstFruits.DragOver,
lstAnimals.DragOver
Const KEY_CTRL As Integer = 8
If Not (e.Data.GetDataPresent(GetType(System.String))) Then
e.Effect = DragDropEffects.None
ElseIf (e.KeyState And KEY_CTRL) And _
(e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy
Then
' Copy.
e.Effect = DragDropEffects.Copy
ElseIf (e.AllowedEffect And DragDropEffects.Move) =
DragDropEffects.Move Then
' Move.
e.Effect = DragDropEffects.Move
End If
End Sub
Finally, when the user drops, the DragDrop event handler deals with the
dropped data. If the drop can provide a string and this is a copy or
move, the code gets the string and adds it to the receiving ListBox.
The event handler's e parameter gives the location where the drop
occurred in screen coordinates. The code uses the control's
PointToClient method to convert the coordinates into the control's
coordinate system. It then uses the IndexFromPoint method to learn the
index of the item where the drop occurred. Finally the code inserts the
dropped string before this item.
' Drop the entry in the list.
Private Sub lstFruits_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lstFruits.DragDrop,
lstAnimals.DragDrop
If e.Data.GetDataPresent(GetType(System.String)) Then
If (e.Effect = DragDropEffects.Copy) Or _
(e.Effect = DragDropEffects.Move) Then
Dim lst As ListBox = DirectCast(sender, ListBox)
Dim item As Object =
CType(e.Data.GetData(GetType(System.String)), System.Object)
Dim pt As Point = lst.PointToClient(New Point(e.X, e.Y))
Dim index As Integer = lst.IndexFromPoint(pt.X, pt.Y)
If index = ListBox.NoMatches Then
lst.Items.Add(item)
Else
lst.Items.Insert(index, item)
End If
End If
End If
End Sub
==========
2. New HowTo: Drag and drop objects from one ListBox to another in
Visual Basic .NET
http://www.vb-helper.com/howto_net_drag_drop_listbox_objects.html
http://www.vb-helper.com/HowTo/howto_net_drag_drop_listbox_objects.zip
In this example, two ListBoxes contain Person objects. The Person
class's ToString method is overridden to display the person's first and
last names.
When you press the right mouse button over a ListBox, the MouseDown
event handler starts the drag by calling the control's DoDragDrop
method. It passes in the selected string to drag and allows the copy and
move actions.
The call to DoDragDrop returns when the drag is finished. The method
returns a value indicating whether the drag resulted in a move, copy, or
nothing (in this case). If the drag ended with a move, then the code
removes the item from the ListBox that started the drag.
' Start a drag.
Private Sub ListBox_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lstNaughty.MouseDown,
lstNice.MouseDown
Dim lst As ListBox = DirectCast(sender, ListBox)
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim index As Integer = lst.IndexFromPoint(e.X, e.Y)
If index <> ListBox.NoMatches Then
Dim per As Person = lst.Items(index)
Dim drop_effect As DragDropEffects = _
lst.DoDragDrop( _
per, _
DragDropEffects.Move Or DragDropEffects.Copy)
' If it was moved, remove the item from this list.
If drop_effect = DragDropEffects.Move Then
lst.Items.Remove(per)
End If
End If
End If
End Sub
If the drag moves over a ListBox, the DragOver event gives the control a
chance to allow or prohibit the drag. The following code checks whether
the drag can provide an object of type
howto_net_drag_drop_listbox_objects.Person as data. (The name is the
program's namespace followed by the class name.) If the drag can provide
this data, then the code checks whether the Ctrl key is down and allows
a move or copy effect.
' Display the appropriate cursor.
Private Sub ListBox_DragOver(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lstNaughty.DragOver,
lstNice.DragOver
Const KEY_CTRL As Integer = 8
If Not
(e.Data.GetDataPresent("howto_net_drag_drop_listbox_objects.Person"))
Then
e.Effect = DragDropEffects.None
ElseIf (e.KeyState And KEY_CTRL) And _
(e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy
Then
' Copy.
e.Effect = DragDropEffects.Copy
ElseIf (e.AllowedEffect And DragDropEffects.Move) =
DragDropEffects.Move Then
' Move.
e.Effect = DragDropEffects.Move
End If
End Sub
Finally, when the user drops, the DragDrop event handler deals with the
dropped data. If the drop can provide a Person object and this is a copy
or move, the code gets the data, converts it into a Person, and adds it
to the receiving ListBox.
The event handler's e parameter gives the location where the drop
occurred in screen coordinates. The code uses the control's
PointToClient method to convert the coordinates into the control's
coordinate system. It then uses the IndexFromPoint method to learn the
index of the item where the drop occurred. Finally the code inserts the
dropped string before this item.
' Drop the entry in the list.
Private Sub ListBox_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lstNaughty.DragDrop,
lstNice.DragDrop
If
e.Data.GetDataPresent("howto_net_drag_drop_listbox_objects.Person") Then
If (e.Effect = DragDropEffects.Copy) Or _
(e.Effect = DragDropEffects.Move) Then
Dim lst As ListBox = DirectCast(sender, ListBox)
Dim item As Person = DirectCast( _
e.Data.GetData("howto_net_drag_drop_listbox_objects.Person"), _
Person)
item = New Person(item.FirstName, item.LastName)
Dim pt As Point = lst.PointToClient(New Point(e.X, e.Y))
Dim index As Integer = lst.IndexFromPoint(pt.X, pt.Y)
If index = ListBox.NoMatches Then
lst.Items.Add(item)
Else
lst.Items.Insert(index, item)
End If
End If
End If
End Sub
==========
3. New HowTo: Drag and drop one or more multi-column values from one
ListView control to another in Visual Basic .NET
http://www.vb-helper.com/howto_net_drag_drop_listviews.html
http://www.vb-helper.com/HowTo/howto_net_drag_drop_listviews.zip
When you press the right mouse button over a ListView, the MouseDown
event handler starts the drag. It creates a List(Of ListViewItem) and
copies references to the currently selected items into it.
The code then calls the control's DoDragDrop method to start the drag,
allowing the copy and move actions.
The call to DoDragDrop returns when the drag is finished. The method
returns a value indicating whether the drag resulted in a move, copy, or
nothing (in this case). If the drag ended with a move, then the code
removes the selected items from the ListView that started the drag.
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemDragEventArgs) Handles lvwAssigned.ItemDrag,
lvwUnassigned.ItemDrag
Dim lvw As ListView = DirectCast(sender, ListView)
Dim dragged_items As New List(Of ListViewItem)
For Each selected_item As ListViewItem In lvw.SelectedItems
dragged_items.Add(selected_item)
Next selected_item
Dim result As DragDropEffects = _
lvw.DoDragDrop(New
DataObject("System.Collections.Generic.List(Of
System.Windows.Forms.ListViewItem)", dragged_items),
DragDropEffects.Move)
' Remove the dragged items if necessary.
If result = DragDropEffects.Move Then
For Each selected_item As ListViewItem In dragged_items
lvw.Items.Remove(selected_item)
Next selected_item
End If
End Sub
If the drag moves over a ListView, the DragOver event gives the control
a chance to allow or prohibit the drag. The following code checks
whether the drag can provide a List(Of ListViewItem) as data. If so,
then it checks whether the Ctrl key is down and allows a move or copy
effect.
Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lvwAssigned.DragEnter,
lvwUnassigned.DragEnter
' Check for the custom DataFormat ListViewItem array.
If e.Data.GetDataPresent("System.Collections.Generic.List(Of
System.Windows.Forms.ListViewItem)") Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Finally, when the user drops, the DragDrop event handler deals with the
dropped data. If the drop can provide a List(Of ListViewItem) and this
is a copy or move, the code gets the data.
The event handler's e parameter gives the location where the drop
occurred in screen coordinates. The code uses the control's
PointToClient method to convert the coordinates into the control's
coordinate system. It then uses the GetItemAt method to learn the index
of the item where the drop occurred.
The code then loops through the list of items, adding them to the target
ListView. One catch is that a ListViewItem cannot be in more than one
ListView at the same time. To avoid problems adding the ListViewItem to
the drop target, the code clones each ListViewItem and adds them to the
ListView.
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lvwAssigned.DragDrop,
lvwUnassigned.DragDrop
Dim lvw As ListView = DirectCast(sender, ListView)
Dim dragged_items As List(Of ListViewItem) = _
e.Data.GetData("System.Collections.Generic.List(Of
System.Windows.Forms.ListViewItem)")
Dim pt As Point = lvw.PointToClient(New Point(e.X, e.Y))
Dim lvi As ListViewItem = lvw.GetItemAt(pt.X, pt.Y)
Dim index As Integer
If lvi IsNot Nothing Then
index = lvi.Index
End If
For Each list_view_item As ListViewItem In dragged_items
' Add the item to the target list.
Dim new_item As ListViewItem =
DirectCast(list_view_item.Clone(), ListViewItem)
If lvi Is Nothing Then
lvw.Items.Add(new_item)
Else
lvw.Items.Insert(index, new_item)
index += 1
End If
Next list_view_item
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
|
|
 |
|