Tutorials‎ > ‎

VB Tutorials

Launch and Manage MDI Child Forms

posted Jan 14, 2010, 8:38 PM by Tim Key   [ updated Jan 14, 2010, 8:39 PM ]

Change the words YourChildFormName to the actual string name of your child form.  Change the words YourChildForm to your acutal ChildForm object and the words YourParentForm to your actual MDI form object.


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Determine if the form is already active
        Dim f As Form = Nothing
        Dim Child As YourChildForm
        Dim FoundIt As Boolean = False
        For Each f In YourParentForm.MdiChildren
            If f.Name = "YourChildFormName" Then
                'Found it
                found = True
                Exit For
            End If
        Next f

        'Launch if it wasn't found
        If FoundIt = False Then
            Child = New YourChildForm
            With Child
                .MdiParent = YourParentForm
            End With
        End If
    End Sub

Set Control Focus at Startup

posted Jan 14, 2010, 8:37 PM by Tim Key   [ updated Jan 14, 2010, 8:38 PM ]

Often times when developing application and using panels, getting a control to receive startup focus can be somewhat of a pain.  After much trial and error and a lot of online research into the problem, I finally found a solution that worked.

In my example I will show you how I overcame this problem using the form load event.


Private Sub Form1(ByVal sender As ObjectByVal e as System.EventArgs) Handles Me.Load


     With Me.Textbox1




     End With


End Sub


The key to this method is to call the focus event then the select event.  I added the SelectAll for my own personal preference.

Get User Screen Resolution

posted Jan 14, 2010, 8:36 PM by Tim Key   [ updated Jan 14, 2010, 8:37 PM ]

There are a couple of ways to do this but the basic principle is the same.  Here are a couple of ways to do this:


1) As a string function

    Public Function ScreenResolution() As String
        Dim X As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim Y As Integer = Screen.PrimaryScreen.Bounds.Height
        Return X & " x " & Y
    End Function


2) Broken down into two different functions

    Public Function ScreenWidth() As Integer
        Dim X As Integer = Screen.PrimaryScreen.Bounds.Width
        Return X
    End Function

    Public Function ScreenHeight() As Integer
        Dim Y As Integer = Screen.PrimaryScreen.Bounds.Height
        Return Y
    End Function


3) Copy and paste the below code into your own function or procedure and use it that way.

        Dim X As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim Y As Integer = Screen.PrimaryScreen.Bounds.Height

Trap The Enter Key In VB.Net

posted Jan 14, 2010, 8:33 PM by Tim Key   [ updated Jan 14, 2010, 8:36 PM ]

All you need to do is copy and past the segment of code below into the keypress event of the control.


        If Asc(e.KeyChar) = Keys.Enter Then
            'Do something here

            e.Handled = True
        End If



That's it, I hope you find it useful...

Pause or Delay Your Applicaiton

posted Jan 14, 2010, 8:32 PM by Tim Key   [ updated Jan 14, 2010, 8:33 PM ]

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Sub Pause(ByVal seconds As Single)
    Call Sleep(Int(seconds * 1000#))
End Sub

'Add the code snippet above and then call the Pause() procedure

Form Colors

posted Jan 14, 2010, 8:30 PM by Tim Key   [ updated Jan 14, 2010, 8:31 PM ]

Ok, so I like the XP look of blues and greys.  Here is what colors I usually use when designing forms.  These are in RGB format


Light Blue -  178, 203, 242

Med Blue -  118, 144, 197

Dark Blue -  51, 102, 204


Light Grey - 241, 242, 242


Hey, do what you want.  I just like it ok :-)

Load VB.Net Form by String Name

posted Jan 14, 2010, 8:27 PM by Tim Key   [ updated Jan 14, 2010, 8:30 PM ]

I wanted to store my form names in a table for a SQL Reporting Application I was working on.  Criteria forms weren't always required for the reports but when they were available I wanted to pull the form name from the menu table and call a simple procedure to load the form.  This would have been very easy to do in VB6 but I was at a total loss in VB.Net to accomplish this task.  Finally in a forum post soemeone told just how simple this is and I wanted to share it with you and also provide a link to the forum on which I picked it up.  The code works like this



'Create a class just copy and paste the code below into a blank class file


'*******Begin Code for class file***************************

'Source: http://forums.devarticles.com/archive/t-7884

Imports System
Imports System.Windows.Forms
Imports System.Reflection

Public Class ObjectFinder
    Public Shared Function CreateObjectInstance(ByVal objectName As String) As Object
        ' Creates and returns an instance of any object in the assembly by its type name.

        Dim obj As Object

            If objectName.LastIndexOf(".") = -1 Then
                'Appends the root namespace if not specified.
                objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
            End If

            obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)

        Catch ex As Exception
            obj = Nothing
        End Try
        Return obj

    End Function

    Public Shared Function CreateForm(ByVal formName As String) As Form
        ' Return the instance of the form by specifying its name.
        Return DirectCast(CreateObjectInstance(formName), Form)
    End Function

End Class


'*******End Code for Class file**************************


'Then all you need to do is call the function like this...


'                    Dim frm As Form
'                   frm = ObjectFinder.CreateForm("MyForm")
'                   frm.ShowDialog()

Catch an error in VB.Net

posted Jan 14, 2010, 8:26 PM by Tim Key   [ updated Jan 14, 2010, 8:27 PM ]

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'To Trap an error use the Try - Catch
            'Put your code here that you want to try
            'This will stop your application from shutting down
            'if a problem comes up.

        Catch ex As Exception
        End Try


End Sub


'With a small amount of work, you can use more defined types of catch errors and apply different messages that will make more sense to you instead of the standard exception has occurred.  You can have as many catch lines as you want for each kind of error.

Check for Internet connection

posted Jan 14, 2010, 8:24 PM by Tim Key   [ updated Jan 14, 2010, 8:25 PM ]

In order to check for an internet connection you need to use the Windows API function InternetGetConnectedState which returns either true or false.  You can also get the connection type from the dwFlags parameter.  In our example I will popup a dialog box but you can do what ever you deem necessary with the results according to your specific needs.


    'Declare the API function

    Private Declare Function InternetGetConnectedState Lib "wininet" _
        (ByRef dwFlags As Long, ByVal dwReserved As Long) As Long

    'Define possible connection types
    Private Enum ConnectStates
        LAN = &H2
        Modem = &H1
        Proxy = &H4
        Offline = &H20
        Configured = &H40
        RasInstalled = &H10
    End Enum

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lng_dwFlags As Long
        Dim blnConnected As Boolean = (InternetGetConnectedState(lng_dwFlags, 0&) <> 0)
        Dim strMessage As String
        Dim ConnectionType As ConnectStates

        If blnConnected Then
            strMessage += "Computer is Connected" & ControlChars.NewLine

            'Display the connection flags
            strMessage += "Connection Flags: " & ControlChars.NewLine
            For Each connectiontype In _
                If (ConnectionType And lng_dwFlags) = ConnectionType Then
                    strMessage += "     " & ConnectionType.ToString() & ControlChars.NewLine
                End If
        End If

    End Sub


'This code does not verify that a connection is active just that the computer is currently configured for the internet.

Launch an exe file from vb.net

posted Jan 14, 2010, 8:23 PM by Tim Key   [ updated Jan 14, 2010, 8:24 PM ]

VB.Net makes this a short task with the System.Diagnostics class.  One line of code can get this working for you in a click event or you can setup a sub procedure and just send the path to the exe file.


'Sub Procedure

'Just copy and paste this procedure into your code and then just call it


Public Sub LoadFile(ByVal Path as String)

     'Launches the fhe file as long as the system has

     'the application to open it


End Sub


'Button Click

'Just copy and paste the one line below into your Click event and

'change the word Path to the full path to the file you are wanting to launch





I hope you find some use for this tip.

1-10 of 13