Welcome Guest!
 VB.NET Helper
 Previous Message All Messages Next Message 
VB .NET Helper Newsletter  Rod Stephens
 Aug 16, 2011 14:13 PDT 

As I built this week's VB 6 examples, I was again reminded how much
better VB .NET is at some things than VB 6 is, in this case graphics.
Graphics in VB .NET is probably 20 times faster than it is in VB 6,
although this week's examples don't really need a lot of performance.

If you've been holding off on switching from VB 6 to VB .NET, take a
look at my book:

    Stephens' Visual Basic Programming 24-Hour Trainer
    http://www.vb-helper.com/24hourvb.htm
-----
Some links that you may find interesting:

    Old-school programming techniques you probably don't miss
    
http://www.computerworld.com/s/article/9132061/Old_school_programming_techniques_you_probably_don_t_miss


    100 Things Your Kids May Never Know About
    
http://shopping.yahoo.com/articles/yshoppingarticles/648/100-things-your-kids-may-never-know-about/


    Free ebook: Programming Windows Phone 7, by Charles Petzold
    http://tinyurl.com/3l566cg

    SAP Crystal Reports
    http://www.businessobjects.com/jump/xi/crvs2010/us2_default.asp

    SQL SERVER � Win a Book a Day (It's on day 17 of 35 now)
    http://wp.me/p2NUQ-3yX
-----
Updated book: Stephens' Visual Basic Programming 24-Hour Trainer
http://www.vb-helper.com/24hourvb.htm

Keith Zavodney discovered:

    The second Hint on page 198 concerning the DateTimePicker is
incorrectly associated with Exercise 16-5. It should be associated with
Exercise 16-4 on page 197.
-----
Things to do this week:

- View screencasts from "Visual Basic 24-Hour Trainer":

    Lesson 3, Part 1: http://www.youtube.com/watch?v=7jxlebRMuu0
    Lesson 3, Part 2: http://www.youtube.com/watch?v=GLGIFmvZx7E
    Lesson 5:         http://www.youtube.com/watch?v=xeJPTBTmNzA

- Post book reviews
-----
Have a great week and thanks for subscribing!

Rod
RodSte-@vb-helper.com

Twitter feeds:
    VBHelper
    CSharpHelper
==========
==========

    VB.NET Contents:
1. New HowTo: Write a subroutine in a code module that scales controls
without moving them in Visual Basic .NET
2. New HowTo: Draw a Pickover popcorn fractal in Visual Basic .NET
3. New HowTo: Draw a colored Pickover popcorn fractal in Visual Basic
.NET
4. New HowTo: Graph historical stock prices in Visual Basic .NET
5. New HowTo: Drag and drop images in Visual Basic .NET
6. New HowTo: Drag and drop images while displaying a preview in Visual
Basic .NET
==========
++++++++++
      <VB.NET>
++++++++++
==========
1. New HowTo: Write a subroutine in a code module that scales controls
without moving them in Visual Basic .NET
http://www.vb-helper.com/howto_net_use_code_module.html
http://www.vb-helper.com/HowTo/howto_net_use_code_module.zip

A code module is a file where you can place routines, variables, and
other pieces of code that you don't want associated with anym class. To
create a code module, open the Project menu and select Add Module. Give
the module a name and click OK.

In this example, the module contains the following subroutine, which
scales a control while keeping it centered in its original location.
(This is really an example of using a module. I'm not sure if you'll
ever want to scale a control like this.)

' Resize the control keeping it centered.
Public Sub StretchControl(ByVal ctl As Control, ByVal scale As Single)
    Dim new_width As Single = ctl.Width * scale
    Dim new_height As Single = ctl.Height * scale
    Dim cx As Double = ctl.Left + ctl.Width / 2
    Dim cy As Double = ctl.Top + ctl.Height / 2
    ctl.Bounds = New Rectangle( _
        CInt(cx - new_width / 2), _
        CInt(cy - new_height / 2), _
        CInt(new_width), _
        CInt(new_height))
End Sub

The code calculates the control's new size. It then figures out where it
should position the control to keep it centered in the same place. It
finsihes by setting the control's Bounds property to set the size and
position in a single step.
==========
2. New HowTo: Draw a Pickover popcorn fractal in Visual Basic .NET
http://www.vb-helper.com/howto_net_pickover_popcorn.html
http://www.vb-helper.com/HowTo/howto_net_pickover_popcorn.zip

(Picture at http://www.vb-helper.com/howto_net_pickover_popcorn.png.)

This program maps its drawing area into the region -4 <= x <= 4, -3 <= y
<= 3. If you click on a point, the program iterates these equations and
plots the points:

    Xn+1 = Xn - h * Sin(Yn + Tan(3 * Yn))
    Yn+1 = Yn - h * Sin(Xn + Tan(3 * Xn))

You can enter values for the constant h and the number of iterations to
perform in the text boxes.

If you click the Plot All button, the program plots the series for every
dx-th pixel, where dx is taken from the dx text box. For example, if dx
= 10, the program plots the series for every 10th pixel vertically and
horizontally.

Experiment with the values in the text boxes to see what difference they
make.

For details about how the code works, download the example program and
take a look.
==========
3. New HowTo: Draw a colored Pickover popcorn fractal in Visual Basic
.NET
http://www.vb-helper.com/howto_net_color_pickover_popcorn.html
http://www.vb-helper.com/HowTo/howto_net_color_pickover_popcorn.zip

(Pictures at
http://www.vb-helper.com/howto_net_color_pickover_popcorn.png and
http://www.vb-helper.com/howto_net_color_pickover_popcorn2.png.)

The example "Draw a Pickover popcorn fractal in Visual Basic .NET"
(http://www.vb-helper.com/howto_net_color_pickover_popcorn.html)
explains how to draw the basic Pickover popcorn fractal by iterating
these equations:

    Xn+1 = Xn - h * Sin(Yn + Tan(3 * Yn))
    Yn+1 = Yn - h * Sin(Xn + Tan(3 * Xn))

This program plots each point in color. When it starts a new series of
points, the program loops through the colors red, green, and blue.

To plot a point, the program looks at the pixel's color and moves its
red, green, or blue component halfway between its current value and the
"full on" value 255. For example, suppose the program is plotting a
green series of points and finds a pixel with red, green, and blue
components 128, 128, and 0. It would leave the red and blue components
unchanged and update the green compomnent to be halfway between its
current value 128 and 255 or (128 + 255) / 2 = 383 / 2 = 191. The result
is the color with RGB components 128, 191, 0.

Over time points that are hit many times by the same color become
bright. Points that are hit many times by different colors become close
to white.
==========
4. New HowTo: Graph historical stock prices in Visual Basic .NET
http://www.vb-helper.com/howto_net_graph_stock_history.html
http://www.vb-helper.com/HowTo/howto_net_graph_stock_history.zip

(Picture at http://www.vb-helper.com/howto_net_graph_stock_history.png.)

The key to this example is the GetStockPrices method shown in the
following code.

' Get the prices for this symbol.
Private Function GetStockPrices(ByVal symbol As String) As List(Of
Single)
    ' Compose the URL.
    Dim url As String =
"http://www.google.com/finance/historical?output=csv&q=" & symbol

    ' Get the result.
    ' Get the web response.
    Dim result As String = GetWebResponse(url)

    ' Get the historical prices.
    Dim lines() As String = result.Split( _
        New String() {vbCr, vbLf}, _
        StringSplitOptions.RemoveEmptyEntries)
    Dim prices As New List(Of Single)()

    ' Process the lines, skipping the header.
    For i As Integer = 1 To lines.Length - 1
        Dim line As String = lines(i)
        prices.Add(Single.Parse(line.Split(","c)(4)))
    Next i

    Return prices
End Function

This method composes a URL of the form
http://www.google.com/finance/historical?output=csv&q=DIS where DIS is a
stock ticker symbol. The Google website returns historical stock data
for that symbol in the form of a CSV (comma-separated value) file. The
program breaks it into lines and saves the prices in a List of float
that it returns to the calling code.

For additional information about how the program works, including how it
draws the graph and how the GetWebResponse method works, download the
example and look at the code.
==========
5. New HowTo: Drag and drop images in Visual Basic .NET
http://www.vb-helper.com/howto_net_drag_drop_image.html
http://www.vb-helper.com/HowTo/howto_net_drag_drop_image.zip

One of the first things you must do before implementing drag and drop is
set the AllowDrop property to true for the control that should accept a
drop. For some reason, Visual Studio does not display this property in
the properties window for PictureBoxes. IntelliSense doesn't even admit
the property exists!

When you right-click on a drag source PictureBox, the following code
starts the drag.

' Start the drag.
Private Sub picDragSource_MouseDown(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles
picDragSource.MouseDown, picDragSource2.MouseDown
    ' Start the drag if it's the right mouse button.
    If (e.Button = MouseButtons.Right) Then
        Dim source As PictureBox = DirectCast(sender, PictureBox)
        picDragSource.DoDragDrop(source.Image, DragDropEffects.Copy)
    End If
End Sub

When the drag moves over the drop target, the following code executes.

' Allow a copy of an image.
Private Sub picDropTarget_DragEnter(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.DragEventArgs) Handles picDropTarget.DragEnter
    ' See if this is a copy and the data includes an image.
    If (e.Data.GetDataPresent(DataFormats.Bitmap) AndAlso _
       (e.AllowedEffect And DragDropEffects.Copy) <> 0) _
    Then
        ' Allow this.
        e.Effect = DragDropEffects.Copy
    Else
        ' Don't allow any other drop.
        e.Effect = DragDropEffects.None
    End If
End Sub

If Bitmap data is present and this is a copy operation, the code allows
the copy.

Finally if the user drops on the target, the following code executes.

' Accept the drop.
Private Sub picDropTarget_DragDrop(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.DragEventArgs) Handles picDropTarget.DragDrop
    Dim bm As Bitmap = DirectCast( _
        e.Data.GetData(DataFormats.Bitmap, True), Bitmap)
    picDropTarget.Image = bm
End Sub

This code sets the drop target's Image property equal to the dropped
bitmap. The drop target PictureBox has ScaleMode set to AutoSize so it
resizes itself to fit the dropped image.
==========
6. New HowTo: Drag and drop images while displaying a preview in Visual
Basic .NET
http://www.vb-helper.com/howto_net_drag_drop_image_preview.html
http://www.vb-helper.com/HowTo/howto_net_drag_drop_image_preview.zip

The example "Drag and drop images in Visual Basic .NET"
(http://www.vb-helper.com/howto_net_drag_drop_image.html) explains how
to drag and drop images. See that example for the basic ideas. (Pay
special attention to the first paragraph, which discusses the AllowDrop
property.)

This example adds a preview to the drop. When you drag an image over the
drop target, it displays a grayed out version of the image.

When you right-click on a drag source PictureBox, the following code
starts the drag.

' Start the drag.
Private Sub picDragSource_MouseDown(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles
picDragSource.MouseDown, picDragSource2.MouseDown
    ' Start the drag if it's the right mouse button.
    If (e.Button = MouseButtons.Right) Then
        Dim source As PictureBox = DirectCast(sender, PictureBox)
        picDragSource.DoDragDrop(source.Image, DragDropEffects.Copy)
    End If
End Sub

This code calls DoDragDrop to start the drag, passing it the image to
drag.

When the drag moves over the drop target, the following code executes.

' The current image during a drag.
Private OldImage As Image = Nothing

' Allow a copy of an image.
Private Sub picDropTarget_DragEnter(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.DragEventArgs) Handles picDropTarget.DragEnter
    ' See if this is a copy and the data includes an image.
    If (e.Data.GetDataPresent(DataFormats.Bitmap) AndAlso _
       (e.AllowedEffect And DragDropEffects.Copy) <> 0) _
    Then
        ' Allow this.
        e.Effect = DragDropEffects.Copy

        ' Save the current image.
        OldImage = picDropTarget.Image

        ' Display the preview image.
        Dim bm As Bitmap = DirectCast(e.Data.GetData(DataFormats.Bitmap,
True), Bitmap)
        Dim copy_bm As Bitmap = DirectCast(bm.Clone(), Bitmap)
        Using gr As Graphics = Graphics.FromImage(copy_bm)
            ' Cover with translucent white.
            Using br As New SolidBrush(Color.FromArgb(128, 255, 255,
255))
                gr.FillRectangle(br, 0, 0, bm.Width, bm.Height)
            End Using
        End Using
        picDropTarget.Image = copy_bm
    Else
        ' Don't allow any other drop.
        e.Effect = DragDropEffects.None
    End If
End Sub

If Bitmap data is present and this is a copy operation, the code allows
the copy. It saves the drop target's current image. It then makes a copy
of the dragged image and draws a translucent white rectangle over it to
make it lighter. It then displays the preview image in the drop target.

If the drag leaves the drop target, the following code executes.

' Remove the drag enter image.
Private Sub picDropTarget_DragLeave(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles picDropTarget.DragLeave
    ' Restore the saved image.
    picDropTarget.Image = OldImage
End Sub

This code restores the drop target's original image.

Finally if the user drops on the target, the following code executes.

' Accept the drop.
Private Sub picDropTarget_DragDrop(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.DragEventArgs) Handles picDropTarget.DragDrop
    Dim bm As Bitmap = DirectCast(e.Data.GetData(DataFormats.Bitmap,
True), Bitmap)
    picDropTarget.ClientSize = bm.Size
    picDropTarget.Image = bm
End Sub

This code displays the dropped image and makes the PictureBox fit the
image.

Initially I set the drop target's AutoSize property to AutoSize so it
would fit the image. This caused some weird flickering problems when you
dragged images of different sizes over the control. Give it a try and
see. This version is much better.

With a little extra work, you could drag the image to a specific
position on the drop target. In that case, you could display a preview
of the image in its new position.
==========
Archives:
    http://www.topica.com/lists/VBHelper
    http://www.topica.com/lists/VB6Helper
    http://www.topica.com/lists/VBNetHelper

Twitter feeds:
    VBHelper
    CSharpHelper

Post questions at:
    http://www.topica.com/lists/VBHelperQA
	
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Developers
 Food & Drink
 Health & Fitness
 Internet
 Music
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Software
 Sports
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.