Sunday, 28 February 2010

Moving Code Examples

Our code snippets are now going to be made as hub pages - it's just far easier to create them on that site than on Blogger.

Our first two are basic and easy examples that are ideal for the beginner.

If you want to start learning Visual Basic then try out Text Box Input and VB Box.

Cheers.

Monday, 5 October 2009

Progress Bar VB

Progress Bar VB
Progress bars are a very handy control within the Visual Basic environment.

They can give you a visual indicator of how 'far' through a recordset or dataset your application has reached. When searching through a large number of records it is a good way to let the user know that things are 'still happening' and that the system has not hung.

Here we will give a simple example in VB6

  • Open VB and create a new standard project
  • In the Project menu option, choose 'Components' and scroll down the list. You should come to Microsoft Windows Common Controls 6.0 (Depending on which service pack you have installed)
  • Check it and a tick will appear
  • Click OK and more controls will have appeared in the toolbox
  • VB6 Components Menu - Progress Bar VB Now you can drag a progress bar control onto the form. Place it and a command button on the form. We always rename our controls to something meaningul (EG: cmdSave) but for the purposes of this example we won't bother renaming any controls.


    VB Progress BarNow we will play with a few properties:

    Set the Min property to 1
    Set the Max property to 10
    Set the Scrolling property to ccScrollingSmooth

    Now in the Command1 Code enter the following:

    Dim iIndex As Integer

    'Clear the progress bar - it cannot be less than the min property
    ProgressBar1.Value = 1

    For iIndex = 1 To 9

    'Increment the progress bar
    ProgressBar1.Value = ProgressBar1.Value + 1
    Call Wait(0.1)
    Next

    Now paste in the follwing private subroutine:

    Private Sub Wait(ByVal iHowManySecs As Integer)
    Dim dStart As Date

    On Error GoTo WaitErr

    dStart = Now

    Do Until DateDiff("s", dStart, Now) > iHowManySecs
    'Waiting until number of seconds has passed
    Loop

    WaitExit:
    Exit Sub

    WaitErr:
    Resume WaitExit

    End Sub

    Run the application and click the command button. A nice simple smooth scrolling progress bar.

    Of course progress bars are most often used when looping through a recordset which we will cover in the future.

    Progress Bar VB

    Monday, 28 September 2009

    VB MDI

    VB MDI

    MDI stands for Multiple Document Interface. An MDI application is an application in which you can view and manipulate several documents at the same time. A good example of an MDI application used the world over is Microsoft Excel.

    Visual Basic.Net allows the developer to create MDI applications as well as SDI applications, here is a quick overview of VB MDI.

    To create an MDI application open a new Windows Application in Visual Basic.NET. As usual the new application will open with a default form name Form1.

    *To keep things simple we will not bother renaming forms and controls.*


    Add another form, (unsurprisingly Form2) to this application by right-clicking on the project name in the Solution Explorer window and selecting Add->Add Windows Form.

    If you want you can place some controls on Form2.

    For this example we will set Form1 as the MDI parent window and Form2 as MDI child window.

    *MDI child forms are important for MDI apps as users interact mostly through the child forms.*

    Select Form1 and set (in the Windows Style section) the property IsMdiContainer to True.

    By setting it to true it designates this form as an MDI 'Container' for the child windows. Once that property has been set to True you will notice that the form has changed colour.

    Drag any Main Menu component from the toolbox onto Form1. Child windows will show when a menu item is clicked. Name the top-level menu item 'File' and submenu items as 'New Child Window', 'Arrange Child Forms' and 'Close'.

    Now you need to add some code:

    *Note that we still use hungarian notation when declaring variables.*

    m prefix denotes it is a 'member' of the current module
    i prefix denotes an integer variable

    Go into code view of the form. Right at the top of the form you will see:

    Public Class Form1 Inherits System.Windows.Forms.Form

    Add in:

    '---------- Member Variables ----------
    Dim miChildForm As Integer = 0
    Dim mChildForms(5) As Form2 'Array to store child windows

    '---------- Member Procedures ---------
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _
    As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object,_
    ByVal e As System.EventArgs) Handles MenuItem2.Click
    miChildForm += 1 'Same as miChildForm = miChildForm + 1
    mChildForms(miChildForm) = New Form2()
    mChildForms(miChildForm).Text = "Child Form: " & Str(miChildForm)

    'Set title for child windows and increment the number with an array
    mChildForms(miChildForm).MdiParent = Me
    mChildForms(miChildForm).Show()
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object,_
    ByVal e As System.EventArgs) Handles MenuItem3.Click
    Me.LayoutMdi(MdiLayout.Cascade)

    End Sub

    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal_
    e As System.EventArgs) Handles MenuItem4.Click
    Me.Close()
    End Sub

    Hit F5 to run the app and have a play!
    Nice and easy code to get you going with VB MDI

    Thursday, 24 September 2009

    VB Message Box

    VB Message Box

    The VB Message Box must be one of the most frequently used modules in the Visual Basic library. The message box can be a very powerful tool and can help application users make the correct decision before proceeding with a choice.

    To display a simple VB Message Box enter the following code:

    Visual Basic 6
    MsgBox ("This is a VB Message Box")

    VB Message Box
    Visual Basic.Net
    MsgBox("This is a VB Message Box")

    VB Message Box
    There are various options available to you to enhance the appearance of a message box such as adding options of Yes, No and Cancel. Icons can be added to make the message box appear as a warning or a question. A title of the message box can also be specified. Here was chosen the title as 'Proceed?' and set the message box style as Question.

    Visual Basic 6
    MsgBox "Do you wish to proceed?", vbQuestion + vbYesNo, "Proceed?"

    VB Message Box
    Visual Basic.Net
    MsgBox("Do you wish to proceed?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Proceed?")

    VB Message Box
    Here we have chosen an 'alert' style message box. It is informing the user that details cannot be displayed with an exclamation icon:

    Visual Basic 6:
    MsgBox "Cannot display details at this time", vbExclamation + vbOKOnly, "Details Unavailable"

    VB Message Box
    Visual Basic.Net
    MsgBox("Cannot display details at this time", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Details Unavailable")

    VB Message Box

    VB Message Box