VB Tips and Code Samples

02/22/2012
It took me a long time when WPF was new to be able to show an image from the resources in an imagebox. Because it was asked in the Express forum I've placed here a simple version of that.
'set a reference to system drawing
Class MainWindow
    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Using memst As New IO.MemoryStream
            Dim bmpImage As New BitmapImage
            My.Resources.Edj345.Save(memst, System.Drawing.Imaging.ImageFormat.Jpeg)
            bmpImage.BeginInit()
            bmpImage.StreamSource = New IO.MemoryStream(memst.ToArray)
            bmpImage.EndInit()
            Image1.Source = bmpImage
        End Using
    End Sub
End Class
02/22/2012

This is Tip is meant to show the use of the keyword “Using”, the Try and Catch, the ExecuteScalar and how to concatenate first name and last name to a full name.

To test it you need only a new project with one button and a textbox on a windows form.
And to set your Server name in the connectionstring.

Imports System.Data.SqlClient
'Code for versions starting with VB10SP1 for earlier versions add the Byval's in the method
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgsHandles Button1.Click
        Try
            Using Con As New SqlConnection("Data Source=YourServer;Initial Catalog=Northwind;Integrated Security=True")
                Con.Open()
                Using com As New SqlCommand("Select FirstName + ' ' + Lastname Picture from Employees where EmployeeID =1", Con)
                    Dim textObj = com.ExecuteScalar
                    If Not textObj Is Nothing AndAlso Not textObj Is DBNull.Value Then
                        TextBox1.Text = CStr(textObj)
                    End If
                End Using
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
>
02/22/2012
Sometimes is asked in the forums: How can we play two different sounds at the same time using .Net framework classes. We can do it using the Windows MediaPlayer we got with WPF. To try this Tip you need only two buttons on a windows form and run.
'Set a reference to Windows BAse
'Set a reference to Presentation Core
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        Dim sp As New System.Windows.Media.MediaPlayer
        sp.Open(New System.Uri("C:\wavs\wav1.wav"))
        sp.Play()
    End Sub
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgsHandles Button2.Click
        Dim sp As New System.Windows.Media.MediaPlayer
        sp.Open(New System.Uri("C:\wavs\wav2.wav"))
        sp.Play()
    End Sub
End Class
02/22/2012
Often is asked in the forums, how can we change the foreground controls in the background. That can be done by the invoke. But I find it easier to use a timer for that. I've the idea that with this I've more control over it. To try this Tip you only need a new windows form project with 2 labels draged on the surface.
Public Class Form1
    Private Lblvalue1 As Integer
    Private Lblvalue2 As Integer
    Private WithEvents bgw As New System.ComponentModel.BackgroundWorker
    Private WithEvents tm As New Timer With {.Enabled = True, .Interval = 100}
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load
        bgw.RunWorkerAsync()
    End Sub
    Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgsHandles bgw.DoWork
        For i = 0 To 1000
            Lblvalue1 += 1
            Lblvalue2 += 100
            Threading.Thread.Sleep(500)
        Next
    End Sub
    Private Sub tm_Tick(sender As Object, e As System.EventArgsHandles tm.Tick
        Label1.Text = CStr(Lblvalue1)
        Label2.Text = CStr(Lblvalue2)
    End Sub
End Class
02/22/2012
This are two often asked questions in one sample. Be aware the code is for VB10SP1 and newer but can be used with VB2008 and Framework 3.5 too, if the Byval is added.
'Be aware later than VB10SP1 code is used for older versions add byval in the methods
Imports System.ComponentModel
Imports System.Windows.Media
'Set a reference to WindowsBase
'Set a reference to PresentationCore
Public Class Form1
    Private WithEvents tm As New Timer With {.Enabled = False}
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load
        Dim Songs As New BindingList(Of SongFrom {New Song With {.Title = "C:\Test\Wav1.wav"},
                                                    New Song With {.Title = "C:\Test\Wav2.wav"},
                                                    New Song With {.Title = "C:\Test\MP1.mp3"}}
        DataGridView1.DataSource = Songs
    End Sub
    Public Class Song
        Public Property Title As String
    End Class
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        tm.Interval = 100
        tm.Start()
    End Sub
    Private Sub tm_Tick(sender As Object, e As System.EventArgsHandles tm.Tick
        tm.Stop()
        Static position As Integer = 0
        Dim time As Integer
        If position < DataGridView1.SelectedRows.Count Then
            'Because the DataGridView returns selectedrows reverse
            Dim toPlay = DataGridView1.SelectedRows.Count - 1 - position
            time = PlayMedia(CStr(DataGridView1.SelectedRows(toPlay).Cells(0).Value))
            position += 1
            tm.Interval = time
            tm.Start()
        End If
    End Sub
    Private Function PlayMedia(Title As StringAs Integer
        Dim sp As New System.Windows.Media.MediaPlayer
        sp.Open(New System.Uri(Title))
        System.Threading.Thread.Sleep(3000) 'to give the media time to load
        Dim duration = sp.NaturalDuration.TimeSpan
        Dim time As Integer
        time = CInt(duration.TotalMilliseconds)
        sp.Play()
        Return time
        sp.Close()
    End Function
End Class
01/05/2012
This example shows how to make SubArray extension methods that let you easily copy parts of one- and two-dimensional arrays in Visual Basic .NET.
01/05/2012
This example shows how to add delegates to combine lists of methods in Visual Basic .NET.
01/05/2012
This example shows how to find solutions to the equilateral triangle puzzle in Visual Basic .NET.
01/05/2012
This example shows how to add a DrawRectangle method to the Graphics class that takes a RectangleF as a parameter in Visual Basic .NET.
01/05/2012
This example shows how to display solutions to the equilateral triangle puzzle in Visual Basic .NET.
01/05/2012
This example describes a puzzle to find the equilateral triangles in Visual Basic .NET.
01/05/2012
This example shows how to display solutions to the equilateral triangle puzzle in Visual Basic 6.
01/05/2012
This example shows how to copy a two-dimensional array in Visual Basic .NET.
01/05/2012
This example describes a puzzle to find the equilateral triangles in Visual Basic 6.
01/05/2012
This example shows how to find solutions to the equilateral triangle puzzle in Visual Basic 6.