Showing posts with label VB6 Programming. Show all posts
Showing posts with label VB6 Programming. Show all posts

Creating a Function to Convert Numbers into Roman Numerals

Saturday, March 27, 2010 · 3 comments



To create a number of letters we often use roman numerals to indicate the month of making the letter.For example, the letter number is '001/Int/III/2010' where the symbol 'III' indicates that the letter was made in March 2010.

For this we need a function that can convert an integer number from 1 to 12 into roman numerals. In the Visual Basic programming, we can easily create such a function. Well, here we'll show an example program to create such functions, as well as examples of how to use it.

  • First open your VB program and select standard exe form
  • Click the View menu
  • Click the Code menu


  • Write the Visual Basic (VB) program code below

    Function IToR(i As Integer) As String Dim s As String s = "I II III IV V VI VII VIIIIX X XI XII " IToR = Mid(s, (i - 1) * 4 + 1, 4) End Function


  • Click the View menu
  • Click the Object menu
  • Put two label controls, two textboxes and a CommandButton to the form
  • Change the Caption of first label into 'Month'
  • Change the Caption of second label into 'Roman Numeral'
  • Change the name of the first textbox into 'txtMonth'
  • Change the name of the second textbox into 'txtRomNum'
  • Remove texts on the first and second textboxes
  • Change the name of the CommandButton into 'cmdConvert'
  • Change the CommandButton caption into 'Convert'
    So your Screen design will look like the image below:


  • Double click CommandButton 'Convert'
  • Write below Visual Basic (VB) program coding

    Private Sub cmdConvert_Click() txtRomNum.Text = IToR(Val(txtMonth.Text)) End Sub


    Okay, you have completed the program
    Now it's time to test
  • Run your program
  • Type a number such as 8 in the first textbox
  • Click CommandButton 'Convert'
    Consider the results


  • What's an Event in Visual Basic?

    Thursday, September 10, 2009 · 3 comments

    An event is an action that recognized by a control or a form. For examples moving mouse over a control, clicking a mouse, pressing a key, etc. You can write code in an event that runs when the event occurs.

    Every object in Visual Basic has a set of event that it recognizes. Each object has specific events. Here are list of some events than owned by each object:

    Object name: CommnandButton
    Events owned: Click, DragDrop, DragOver, GotFocus, KeyDown, KeyPress, KeyUp, LostFocus, MouseDown, MouseMove, MouseUp, OLECompleteDrag, OLEDragDrop, OLEDragOver, OLEGiveFeedback, OLESetData and OLEStartDrag.

    Object name: TextBox
    Events owned: Change, Click, DblClick, DragDrop, DragOver, GotFocus,


    When You Should Set the Name Property in VB6 Programming?

    Monday, September 7, 2009 · 0 comments

    One of the important thing is setting the Name of object property when you developing VB6 applications. And in the previous posting I've written that you should set the name with adopting the Standard Naming Convensions in order easier for other developer in understanding your program. And also you should give the descriptive name that according to the purpose of the object.

    Now, the question is "when you should set the name property?". The recommended time to set the Name property is as soon as after you create the object. Why this time? Because if you set the name (change the name) property after you write coding for the object, it makes the code in your applications will loss its association with the control.

    When you change a name property of an object, the sub procedure that associated with the object won't be changed automatically, so code seems lost. For example: You have a TextBox named Text1, and you have written some codes in Sub Text1_Change() and Sub Text1_LostFocus(). When you change the name to other name (for example txtFirstName), the name of Sub Text1_Change() and Sub Text1_LostFocus() won't be changed to new name automatically. So the association between the object and the code is lost.

    Now, what you should do if you have to change the object name after you write your code that associated with the object?


    How to Return Property Value in VB6 Programming?

    Saturday, September 5, 2009 · 0 comments

    It is often needed to return property value of an object to perform a calculation or some other task in your Visual Basic Application. And in the most previous sample program I've written use this way to perform some calculations, and also some other tasks. For example in the sample program of How to Convert Degree from Celcius to Another in VB 6 Programming? also returning property value to be calculated, and then the result of calculation to set the property value of other object.

    To return the value of an object property, you can use below syntax:
    Variable = Object.Property
    Or
    Object2.Property = Object1.Property

    Note:
    You can use the first syntax if you want to return the value of an object property into a variable, so the value will be kept in the variable.

    You should use the second syntax if you want to return the value to set another object property value.

    In the most sample program I've written in the previous postings use the second syntax to return value of an object property and perform the calculation, then the result kept as value of another object property.

    Notice below sample code:
    Text2.Text = Val(Text1.Text) * 1.8 + 32

    The value of Text1.Text is returned and used as an operand in the calcuation phrase and the result of calculation then used as value of Text2.Text. In this example you don't need a variable to keep the calculation result.

    Another example code:
    strName = txtName.Text
    lblName.Caption = txtName.Text

    Any comment? Please leave it in the below comment box.


    Setting Properties of Object at Run Time - Sample Code

    Friday, September 4, 2009 · 1 comments

    Setting Object Properties at Run Time








    In this posting I'll write a sample program of VB6 that demonstrates how to set object properties at run time. However some of property must be set at design time. Okay, follow below instructions:

  • Open your Visual Basic 6 application
  • Select a standard exe project type (let with default name Form1 and Project1)
  • Add your Form1 with 7 Labels, 6 TextBoxes, a CommandButton and a ComboBox
    Your design maybe look like the image on the top of this posting.
  • Change the Names of the TextBoxes into txtFName, txtAddr1, txtAddr2, txtProvince, txtCity and txtCountry
  • Change the Name of the ComboBox into cboGender
  • Change the Name of the CommandButton into cmdRegister
  • Double-click anywhere on your Form1 area to create a Sub Form_Load()
  • Write below program coding in the Sub Form_Load() area
    'setting Form1 caption
    Form1.Caption = "New Member's Registration Form"
    
    'setting the properties of Title (Label1)
    With Label1
    .Caption = "New Member's Registration Form"
    .FontSize = 18
    .FontUnderline = True
    .Left = 360
    .Top = 120
    .Width = 5775
    .Height = 495
    End With
    
    'setting the properties of Full Name label (Label2)
    With Label2
    .Caption = "Full Name"
    .FontSize = 10
    .Top = 960
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Gender label (Label3)
    With Label3
    .Caption = "Gender"
    .FontSize = 10
    .Top = 1320
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Address1 label (Label4)
    With Label4
    .Caption = "Adress1"
    .FontSize = 10
    .Top = 1680
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Address2 label (Label5)
    With Label5
    .Caption = "Adress2"
    .FontSize = 10
    .Top = 2040
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of City label (Label6)
    With Label6
    .Caption = "City"
    .FontSize = 10
    .Top = 2400
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Province label (Label7)
    With Label7
    .Caption = "Province"
    .FontSize = 10
    .Top = 2760
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Country label (Label8)
    With Label8
    .Caption = "Country"
    .FontSize = 10
    .Top = 3120
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of txtFName TextBox
    With txtFName
    .Text = ""
    .FontSize = 10
    .Top = 960
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of cboGender ComboBox
    With cboGender
    .Text = ""
    .FontSize = 10
    .Top = 1320
    .Left = 1680
    '      .Height = 330
    .Width = 1575
    .AddItem "Female"
    .AddItem "Male"
    End With
    
    'setting the properties of txtAddr1 TextBox
    With txtAddr1
    .Text = ""
    .FontSize = 10
    .Top = 1680
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of txtAddr2 TextBox
    With txtAddr2
    .Text = ""
    .FontSize = 10
    .Top = 2040
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of txtCity TextBox
    With txtCity
    .Text = ""
    .FontSize = 10
    .Top = 2400
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of txtProvince TextBox
    With txtProvince
    .Text = ""
    .FontSize = 10
    .Top = 2760
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of txtCountry TextBox
    With txtCountry
    .Text = ""
    .FontSize = 10
    .Top = 3120
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of cmdRegister CommandButton
    With cmdRegister
    .Caption = "Register"
    .FontSize = 14
    .Top = 3720
    .Left = 2880
    .Height = 495
    .Width = 1695
    End With


  • Test your program by running it.
    Your program should like below screenshot.
    Setting Object Properties at Run Time


  • How to Convert Degree from Celcius to Another in VB 6 Programming?

    Thursday, September 3, 2009 · 1 comments

    In this posting I will tell you a simple VB programming tips "How to convert degree / temperature from Celcius to another one", so the base temperature is Celcius. The temperature will be converted into Fahrenheit, Rheamur and Kelvin. As we know that we have three conversion formulas are (in Celcius base):
    Fahrenheit = ( Celcius X 1.8 ) + 32
    Rheamur = Celcius X 0.8
    Kelvin = Celcius + 273.15
    How to apply these three formulas in simple VB6 programming? It's just an easy thing to do! You can do it just with a simple VB program. Please follow below instructions for your tips!

  • Create a VB project for example Project1
  • Create a VB standard form for example Form1
  • Create 5 labels for example Label1, Label2, Label3, Label4, Label5
  • Go to Label1 properties by clicking Label1 object, and then change Label1 Caption into "Degree Conversion", and change Font Size into 14.

  • Go to Label2 properties, and find Label2 Caption and change the value into "Celcius"
  • Go to Label3 properties, and then change Label3 Caption into "Fahrenheit"
  • Go to Label4 properties, and then change Label4 Caption into "Rheamur"
  • Go to Label5 properties, and then change Label5 Caption into "Kelvin"
  • Create 4 text boxes for example Text1, Text2, Text3, Text4
  • Create a command button for example Command1
  • Go to Command1 properties, and change Command1 caption into "Convert"

    So your screen design will look like below image:
    Design Simple VB Program Degree Conversion from Celcius
    Now, let's write the program coding.
    Double click any where in Form1 area to create a Sub Form_Load()
    Write down below program coding in the Sub Form_Load() area

    'to clear text boxes text Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = ""


    Double click Command1 command button to create a Sub Command1_Click()
    Write down below program coding in the Sub Command1_Click() area

    Text2.Text = Val(Text1.Text) * 1.8 + 32 Text3.Text = Val(Text1.Text) * 0.8 Text4.Text = Val(Text1.Text) + 273.15


    So your program will look like the following list:

    Private Sub Command1_Click() Text2.Text = Val(Text1.Text) * 1.8 + 32 Text3.Text = Val(Text1.Text) * 0.8 Text4.Text = Val(Text1.Text) + 273.15 End Sub

    Private Sub Form_Load() 'to clear text boxes text Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" End Sub


    Now, you can test your program by clicking Start button on your VB toolbar to check whether your program is correct or not.
  • Type a number in Text1 for example 15 (in the text box with label "Celcius")
  • Click Command1 command button (with label "Convert")
    If the results are 59 in Text2 (Fahrenheit), 12 in Text3 (Rheamur), and 288.15 in Text4 (Kelvin), it means that your program is correct and running properly.
    Look at below image for your illustration.
    Simple VB Program Degree Conversion from Celcius

    How do you think, it's an easy simple VB programming tips isn't it?





  • How to Convert Degree from Rheamur to Another in VB 6 Programming?

    · 0 comments

    In the previous posting you have learnt a simple VB programming tips "How to convert degree from Fahrenheit into another one in VB6 programming". Now we will learn another simple VB programming tips "How to convert degree / temperature from Rheamur into another one", so the base degree is Rheamur, and will be converted into Celcius, Fahrenheit and Kelvin. We have three previous conversion formulas (Celcius base) as follow:
    Fahrenheit = ( Celcius X 1.8 ) + 32
    Rheamur = Celcius X 0.8
    Kelvin = Celcius + 273.15


    From these formulas, now we can generate new three other formulas (Rheamur base) as the following:
    Celcius = Rheamur / 0.8
    Fahrenheit = ( ( Rheamur / 0.8 ) X 1.8 ) + 32
    Kelvin = ( Rheamur / 0.8 ) + 273.15


    Now, let's implement above formulas into a simple VB6 programming. Let's go on to the following simple VB programming tips!

  • Create a VB project for example Project1
  • Create a VB standard form for example Form1
  • Create 5 labels for example Label1, Label2, Label3, Label4, Label5
  • Go to Label1 properties by clicking Label1 object, and then change Label1 Caption into "Degree Conversion", and change Font Size into 14.
  • Go to Label2 properties, and then change Label2 Caption into "Rheamur"
  • Go to Label3 properties, and then change Label3 Caption into "Celcius"
  • Go to Label4 properties, and then change Label4 Caption into "Fahrenheit"
  • Go to Label5 properties, and then change Label5 Caption into "Kelvin"
  • Create 4 text boxes for example Text1, Text2, Text3, Text4
  • Create a command button for example Command1
  • Go to Command1 properties, and change Command1 caption into "Convert"

    So your screen design should look like below image:
    Screen Design Simple VB6 Program - Degree Conversion from Rheamur

  • Double click anywhere in the Form1 area to create a Sub Form_Load()
  • Write down below program coding in the Sub Form_Load() area
    'to clear the content of text boxes
    Text1.Text = ""
    Text2.Text = ""
    Text3.Text = ""
    Text4.Text = ""

  • Double click the command button with caption "Convert" to create a Sub Command1_Click()
  • Write down below program coding in the Sub Command1_Click() area
    'conversion process
    'Text1 represents Rheamur
    'Text2 represents Celcius
    'Text3 represents Fahrenheit
    'Text4 represents Kelvin
    Text2.Text = Val(Text1.Text) / 0.8
    Text3.Text = ((Val(Text1.Text) / 0.8) * 1.8) + 32
    Text4.Text = (Val(Text1.Text) / 0.8) + 273.15

    So your program will look like below listing:
    Private Sub Command1_Click()
    'conversion process
    'Text1 represents Rheamur
    'Text2 represents Celcius
    'Text3 represents Fahrenheit
    'Text4 represents Kelvin
    Text2.Text = Val(Text1.Text) / 0.8
    Text3.Text = ((Val(Text1.Text) / 0.8) * 1.8) + 32
    Text4.Text = (Val(Text1.Text) / 0.8) + 273.15
    End Sub

    Private Sub Form_Load()
    'to clear the content of text boxes
    Text1.Text = ""
    Text2.Text = ""
    Text3.Text = ""
    Text4.Text = ""
    End Sub

    Ok, your program's finish. Test your program by clicking Start button on your VB toolbar to know whether your program working properly or not.
  • Type a number in Text1 for example 80 (in the text box with label "Rheamur")
  • Click command button with label "Convert"
    If the results are 100 in Text2 (Celcius), 212 in Text3 (Fahrenheit), and 373.15 in Text4 (Kelvin), it means that your program is correct and working properly.
    Look at below image for your illustration.
    Simple Vb Program Degree Conversion From Rheamur

    It is very easy and very simple isn't it? Would you like to try it? Ok, in the next posting I'll write another simple tips with VB6 programming. See you in the next posting!




  • How to Set Object Properties at Run Time in VB6?

    Wednesday, September 2, 2009 · 0 comments

    In the previous posting you have learnt how to set object properties at design time, by clicking the object you want, then change/set the value of property in the properties window. In this posting I'll write how to set/change property value at run time (when program running). And also I'll give you a sample program (code).

    Below is the syntax how to set an object property at run time:
    Object.Property = Expression

    For example:
    Below statement will set Caption of lblName with "John Smith" at run time.
    lblName.Caption = "John Smith"

    However not all object properties can be set their values at run time, because some object properties can be set just at design time. In the next posting maybe I'll write lists of which object properties that can be set just at design time, which ones that can be set just at run time, and which ones that can be set both at design time and run time.


    How to Set Properties of Object at Design Time in VB6?

    Tuesday, September 1, 2009 · 0 comments

    Setting Properties of Object in VB6 programming














    In the previous posting I've written that you can set the properties of an object at design time. The properties settings that you establish at design time are used as the initial settings each time your application runs. You can set the object properties by using Properties window.

    Although it is not a must, because you set the properties of course according to your need. But some properties usually you set them at design time, such as:
    BackStyle
    BorderStyle
    Caption
    Enabled
    FillColor
    FillStyle
    Name
    Visible

    How to set the value of property at design time, you can follow below steps:

  • At the Form Designer window, select the form or control which you want to set the property by clicking it.
    Visual Basic will activate the form or control, and displays the property for the object.
  • In the Propertis window, select the property you want to set.
  • Type or select the property setting value you want.
    Some property values can be selected from the list, but others must be typed.


  • How to Name Objects in VB6 Programming ?

    Monday, August 31, 2009 · 2 comments

    When an object is created, a default name based on its object typed will be given to the object. For example Form1 will be given to object/control with type Form, Text2, Text3,... will be given to Textbox object, Command1, Command2,.... will be given to command button object. You should immediately change the name property of the object to a name that describes the purpose of the control. Changing the Name property of your object with a name that describe the purpose with the object will make the code in your application easier to read and debug.

    Visual Basic has several Standard Naming Convensions. Although you can assign any name you want to an object, but it is a better to adopt a naming convension and use it ocnsistently throughout your programms. Because it will make easier for others to understand your code.

    Here it is a list of Standard Naming Convensions used in Visual Basic that assign to each type object:

  • Object type: Check box
    The prefix you should use: chk
    Example name: chkDrink

  • Object type: Combo box
    The prefix you should use: cbo
    Example name: cboStore

  • Object type: Command button
    The prefix you should use: cmd
    Example name: cmdSelect

  • Object type: Data
    The prefix you should use: dat
    Example name: datSales

  • Object type: Directory list box
    The prefix you should use: dir
    Example name: dirImage

  • Object type: Drive list box
    The prefix you should use: drv
    Example name: drvSource

  • Object type: File list box
    The prefix you should use: fil
    Example name: filSource

  • Object type: Form
    The prefix you should use: frm
    Example name: frmEntry

  • Object type: Frame
    The prefix you should use: fra
    Example name: fraOption

  • Object type: Grid
    The prefix you should use: grd
    Example name: grdItem

  • Object type: Horizontal croll bar
    The prefix you should use: hsb
    Example name: hsbTemperature

  • Object type: Image
    The prefix you should use: img
    Example name: imgEmployee

  • Object type: Label
    The prefix you should use: lbl
    Example name: lblName

  • Object type: Line
    The prefix you should use: lin
    Example name: linHorizontal

  • Object type: List box
    The prefix you should use: lst
    Example name: lstCustomer

  • Object type: Menu
    The prefix you should use: mnu
    Example name: mnuReport

  • Object type: OLE
    The prefix you should use: ole
    Example name: oleObject2

  • Object type: Option button
    The prefix you should use: opt
    Example name: optCountry

  • Object type: Picture box
    The prefix you should use: pic
    Example name: picFlower

  • Object type: Shape
    The prefix you should use: shp
    Example name: shpSquare

  • Object type: Text box
    The prefix you should use: txt
    Example name: txtName

  • Object type: Timer
    The prefix you should use: tmr
    Example name: tmrStep

  • Object type: Vertical scroll bar
    The prefix you should use: vsb
    Example name: vsbRate


  • Project Types in Visual Basic

    Saturday, August 29, 2009 · 0 comments





















    A Visual Basic project is made up of the forms, code modules, ActiveX controls, and environmental settings required by an application (you can identify a Visual Basic project from it's extention name ".vbp"). The project lists all the files you need in your application.

    By default Visual Basic offers you several project templates designed to support the development of different kinds of applications and components. The project templates contain the basic project objects and environment settings you need to create the type of application or component you want to build. When you begin to develop an application, first you have to decide what project template you will use.

    By default you can select the following templates that available in Visual Basic:

  • Standard EXE
    By default Standard EXE projects contain a form, and you can use this project template to develop a stand-alone application.

  • Data Project
    You can use this project template to develop an application that reads or manipulates data from data sources.

  • ActiveX EXE/ActiveX DLL
    Use ActiveX EXE and ActiveX DLL project templates to develop COM components that expose functionality to other applications. You can use an ActiveX EXE project template if your component will both expose functionality programmatically and run as a stand-alone application. You can use an ActiveX DLL if your component will only be used programatically by another application.

  • ActiveX Control
    You can use ActiveX Control project template if you want to create a component that designed to be a user interface element in a form or a dialog box.

  • ActiveX Document EXE/ActiveX Document DLL
    You can use ActiveX Document EXE and ActiveX Document DLL if you want to create a component that designed for use in a document object container, such as Internet Explorer.

  • DHTML Application
    You can use DHTML Application project template if you want to create a component that can be used on the client side of a Web application.

  • IIS Application
    You can use IIS Aplication project template if you want to create a component that can be used on the server side of a Web application.

    You can create a new Visual Basic project with below steps:
  • Start your Visual Basic application
    The New Project dialog box appears.
  • Select the project template you want, then click Open button.

    Or you can follow below steps if your Visual Basic application already open:
  • Click File menu.
  • Click New Project menu.
    The New Project dialog box appears.


  • Select the project template you want, and then click OK button.



  • Common Terminology in Visual Basic

    Friday, August 28, 2009 · 0 comments

    Like another programming language, Visual Basic also has some common terminology that required to be understood by the developers (programmers). Here are some common terms in Visual Basic:

  • Design time
    Design time is any time when an application being developed in Visual Basic Development Environment

  • Run time
    Run time is any time when an application is running.

  • Forms
    Forms are windows that can be customized by the developer (programmer)

  • Controls
    Controls are graphic that representation of objects

  • Objects
    Object is a general term that used to describe forms and controls that make up a program

  • Properties
    Properties are the characteristics of objects such as size, width, height, etc

  • Methods
    Methods are the actions that object can perform

  • Events
    Events are the actions that recognized by forms or controls

  • Even-driven programming
    .......


  • 8 Basic Steps to Create a Program in Visual Basic

    Thursday, August 27, 2009 · 2 comments

    8 Basic Steps to create a program in Visual Basic



    Visual Basic is one of even-driven programming. Creating an even-driven programs like Visual Basic requires different approach from that used in procedural languages. The approach may seem unusual, if you are experienced with procedural programming.

    Here are 8 basic steps in creating an application with Visual Basic:

    1. Create a Design Plan

    2. Create the user interface

    3. Set the properties of interface objects

    4. Write code for events

    5. Save the project

    6. Test and debug the application

    7. Make an executable file (by compiling it)

    8. Create a setup application



    How to Display Images in a Folder with VB6 Program ?

    Wednesday, August 26, 2009 · 1 comments

    VB6 application to display images in a directory













    In this posting I will write a Sample of Simple VB6 program to display images in a directory of your computer. To display image in VB6 programming you can use LoadPicture statement. The format of statement is:
    LoadPicture (Path\FileName)
    For example:
    LoadPicture ("Image01.jpg") -> display image named Image01.jpg in the current folder (the folder of image and the program is the same)
    LoadPicture ("d:\MyPicture\Image02.bmp") -> display image named Image02.bmp that stored in folder MyPicture in drive d:

    To make a VB6 program application like the above illustration you can follow below VB6 Programming tips.
    In this case assumed that you have some images file (with extention .jpg or .bmp) that stored in directory D:\MyPicture.
    Now you have to create a screen design like below image:
    Screen Design VB6 Program Display Image













    To create the screen design, please follow below steps:

  • Open your Visual Basic software
  • Select a standard exe project, let with default name (Project1 and Form1)
  • Add a label (Label1) control to your form, change its caption into "My Photo Collections", and change font size into 14
  • Add a DriveListBox (Drive1) control to your form
  • Add a Dir DirListBox (Dir1) control to your form
  • Add a FileListBox (File1) control to your form
  • Add an Image (Image1) control to your form
  • Arrange and resize the controls like the above screen design.

    Ok, program interface already done. Now the time to write code statements to functionate the controls, so the program can work.
  • Double-click Drive1 to create a Sub Drive1_Change()
  • Write below Code Statements in the Sub Drive1_Change()
      Dir1.Path = Drive1.Drive
    File1.Path = Dir1.Path

  • Double-click Dir1 to create a Sub Dir1_Change()
  • Write below Code Statement in Sub Dir1_Change()
        File1.Path = Dir1.Path

  • Double-click File1 to create a Sub File1_Click()
  • Write the code statements below in the Sub File1_Click()
        On Error GoTo ErrHandle
    Image1.Picture = LoadPicture(File1.Path & "/" & File1.FileName)
    Exit Sub

    ErrHandle:
    MsgBox "Invalid Image display"
    Resume Next

    So your Program coding will look like below listing:
    Private Sub Dir1_Change()
    File1.Path = Dir1.Path
    End Sub

    Private Sub Drive1_Change()
    Dir1.Path = Drive1.Drive
    File1.Path = Dir1.Path
    End Sub

    Private Sub File1_Click()
    On Error GoTo ErrHandle
    Image1.Picture = LoadPicture(File1.Path & "/" & File1.FileName)
    Exit Sub

    ErrHandle:
    MsgBox "Invalid Image display"
    Resume Next
    End Sub

    Now the time to test your program, follow below tips:
  • Click Start button on your VB standard toolbar
  • Select Drive D: in the DriveListBox
  • Select folder D:\MyPicture in the DirListBox
  • Select file name you want in FileListBox

    Your program should like the illustration on the top of this posting, however the image you display depend on the image you have and select.


  • Visual Basic Standard Toolbar

    Tuesday, August 25, 2009 · 1 comments

    Visual Basic Standard Toolbar

    The Standard Toolbar is one of Visual Basic Development Environment components. The Standard Toolbar contains buttons for many of the most commands used in Visual Basic.
    The button such Open Project, Save Project, Start, Break, and End contain in this toolbar.

    It also contains another button, such buttons to display Project Explorer, the Properties Window, the Toolbar, and other elements of Visual asic Development environment.

    By default most Visual Basic Standard Toolbar are docked (attached) on the top of your screen. However you can undock it whenever you want. To undock it, click near the double bar along the left side of a toolbar, and drag away from its docked position. To dock a toolbar, drag it to another edge of the main window.

    Many buttons exist in Visual Basic Standard Toolbar that make easier to edit your VB application:
    Many buttons exist in VB Standard Toolbar





    Another buttons in Visual Basic Standard Toolbar:
    Visual Basic Standard Toolbar


    With..End With Statement to Set Properties of Object in VB6

    Saturday, August 22, 2009 · 0 comments

    How to Set Object Properties using With..End With
    In the earlier posting I have written that you can set object properties in two session, at design-time and at run-time, although some object properties can be set in both run-time and design-time, other can be set just in design-time, and onother can be set just in run time.

    When you set object properties maybe you need to several properties at once, for example you want to set the properties of TextBox as following:

  • set font to Bold mode
  • set Font Size to 24
  • set the text with a value "Set Object Properties"

    So you can do with the following coding:
    Text1.Font.Bold = True
    Text1.Font.Size = 24
    Text1.Text = "Set Object Properties"

    However to make it easier to read, and easier to understand, you can use With..End With statement to set several Object Properies.
    You can use below code statement:
    With Text1
    .Font.Bold = True
    .Font.Size = 24
    .Text = "Set Object Properties"
    End With

    Example:
    Create a screen design like below image:
    How to Set Object Properties using With..End With
    Note: Set property "Multiline" of Text1 and Text2 to "True".

    Double-click command1 button.
    Write below coding:
       Text1.Text = "One by one set object properties"
    Text1.Font.Bold = True
    Text1.Font.Size = 24
    Text1.FontUnderline = True

    Double-click command2 button.
    Write below code statements:
    With Text2
    .Text = "Using With..End With to set object properties"
    .Font.Bold = True
    .Font.Size = 24
    .FontUnderline = True
    End With

    So your coding should like below listing:
    Private Sub Command1_Click()
    Text1.Text = "One by one set object properties"
    Text1.Font.Bold = True
    Text1.Font.Size = 24
    Text1.FontUnderline = True
    End Sub
    Private Sub Command2_Click()
    With Text2
    .Text = "Using With..End With to set object properties"
    .Font.Bold = True
    .Font.Size = 24
    .FontUnderline = True
    End With
    End Sub

    Now run our program, click command1 button, and then also click command2 button.
    Your program sholud like screenshot in top of this posting.


  • Code Editor Window in Visual Basic

    · 0 comments

    Code Editor Window in Visual Basic
    In the earlier posting I've told you that a VB Project maybe contains one or more files like form, module, and so on. One form usually contains one or more controls. However a form with it's control contained on it can't do anything without code statements (programming code) that associate with them. And a module also contains one or more statements.

    A place in VB Development Environment where you can write your VB code statements (programming code) is called Visual Basic Code Editor Window. It's also known as the Code Window. A sparate Code Editor Window is displayed for each form or module in your project, making it easy to organize, view, and navigate.

    There are two drop-down lists at the top of the Code Editor Window called Object List and Procedure List.The Object list contains a list of all the Contols that exist on the form. When you selectiong a control name from the list, the Procedure list shows all the events (actions) for the control.

    Using the Object and Procedure list together, can make you easy to located and edit the code you want in your VB application. The image above (on the top of posting)illustrates the Code Editor Window.

    To open the Visual Basic Code Editor Window you can follow the following steps:

  • In the Project Explorer window, click the form which code you want to open
  • Then Click View Code button
    How to open VB Code Editor Window - Click View Code button

    Tips:
    For example: You have a control called txtAmount on your form. You want to write code statements on Sub txtAmount_LostFocus(). How can you do that?
    Ok, you can follow the tips below:
  • Click the Form in the Project Explorer window
  • Click View Code button

  • In the Code Editor window, click txtAmount from the Object list
    VB Code Editor Window

  • Then click LostFocus from the Procedure list
    VB Code Editor Window

    So, Sub txtAmount_LostFocus() appears on your screen (in the Code Editor window) and you can write your code statements you want.
    VB Code Editor Window


  • Select Case...End Select - VB6 Sample Program Part 2

    · 0 comments

    This posting is continuation of previous posting "Select Case...End Select - VB6 Sample Program Part 1". In the previous posting you have designed a program interface like below image, and you have saved it.
    Select Case End Select VB6 Sample Program

    Now, let's writing the program coding for the above program design.

  • Double click cmdCalBonus button to create sub cmdCalBonus_Click()
  • Write down below program coding in the cmdCalBonus_Click() area

    Select Case Val(txtSalQty.Text)
    Case 1 To 2
    txtBonus.Text = "2%"
    Case 3, 4
    txtBonus.Text = "3%"
    Case 5 To 6
    txtBonus.Text = "4.5%"
    Case 7 To 8
    txtBonus.Text = "6.5%"
    Case 9
    txtBonus.Text = "10%"
    Case Else
    txtBonus.Text = "15%"
    End Select
    txtBonusAmt.Text = Val(txtSalQty.Text) * Val(txtPriceUnit.Text) * _
    Val(Left(txtBonus.Text, Len(txtBonus.Text) _
    - 1)) / 100

    At the end, your program will look like below listing:

    Private Sub cmdCalBonus_Click()
    Select Case Val(txtSalQty.Text)
    Case 1 To 2
    txtBonus.Text = "2%"
    Case 3, 4
    txtBonus.Text = "3%"
    Case 5 To 6
    txtBonus.Text = "4.5%"
    Case 7 To 8
    txtBonus.Text = "6.5%"
    Case 9
    txtBonus.Text = "10%"
    Case Else
    txtBonus.Text = "15%"
    End Select
    txtBonusAmt.Text = Val(txtSalQty.Text) * Val(txtPriceUnit.Text) * _
    Val(Left(txtBonus.Text, Len(txtBonus.Text) _
    - 1)) / 100
    End Sub

    Let's test your program to check whether it's correct or not.
  • Click Start button on your VB toolbar
  • Type a number (for example 5) on your textbox with label Your Sales Quantity
  • Type a number (for example 100) on your textbox with label Price Unit (USD)
  • And finally click Calculate Bonus button
  • Notice what happen on your program, what's the result?

    According to the Bonus Table if Sales Quantity = 5, so the Bonus Percentage should equal to 4.5%. So the Bonus Amount = 5 X 100 X 4.5 / 100. And the result is 22.5. So your program should like below screenshot.
    Select Case End Select VB6 Sample Program

    Now I'll explain how the program works.
  • When you type 5 in the Your Purchase Quantity, then type 100 in the Price Unit (USD), and click Calculate Bonus button, so the cmdCalBonus_Click() executed.
  • Then this statement "Val(txtSalQty.Text)" will convert text "5" into number 5
  • Then in Select Case.. defined that txtBonus.Text is "4.5%" according to the Sales Quantity => 5
  • Then program executes below statement:
        txtBonusAmt.Text = Val(txtSalQty.Text) * Val(txtPriceUnit.Text) * _
    Val(Left(txtBonus.Text, Len(txtBonus.Text) _
    - 1)) / 100
    Mybe you think that this is a complex statement. But in fact it's just a simple statement.
    Val(txtSalQty.Text) convert text "5" into number 5
    Val(txtPriceUnit.Text) convert text "100" into number 100


    Because txtBonus.Text = "4.5%" (this is a text) so:
    Len(txtBonus.Text) will return 4, and Len(txtBonus.Text) - 1 will return 3
    Left(txtBonus.Text, Len(txtBonus.Text) - 1) equals to Left("4.5%",4-1) will return "4.5"
    Then Val(Left(txtBonus.Text, Len(txtBonus.Text) - 1)) equal to Val("4.5") will return number 4.5


    So the above formula will equal to:
    txtBonusAmt.Text = Val("5")*Val("100")*Val(Left("4.5%",Len("4.5%")-1))/100
    <=> txtBonusAmt.Text = 5 * 100 * 4.5 / 100
    <=> txtBonusAmt.Text = 22.5



  • Select Case End Select - VB6 Sample Program Part 1

    · 0 comments

    VB Select Case Sample Program
    In this posting I'll write a sample of simple VB6 program that using Select Case..End Select control structure. However this program also using several VB6 functions like Val() (to convert text into number so can be calculated), Len() (to get the length of a text) and Left() (to pick up a part from the left side of a text).

    The scenario of the program is as the following:

  • This program is Bonus Calculation (Sales System).
  • Here it is the bonus percentage table:
      Sales Quantity = 1 - 2 -> Bonus = 2%
    Sales Quantity = 3 - 4 -> Bonus = 3%
    Sales Quantity = 5 - 6 -> Bonus = 4.5%
    Sales Quantity = 7 - 8 -> Bonus = 6.5%
    Sales Quantity = 9 -> Bonus = 10%
    Sales Quantity = 10 or more -> Bonus = 15%
  • Bonus Amount = Sales Quantity X Price Unit X Bonus

    The screen design of the program is like below screenshot.
    Secect Case End Select VB6 Sample Program

    Now, let's design the program first (create screen interface).
  • Open your Visual Basic 6 software
  • Select a standard type project
  • Create 5 Labels
  • Create 4 Textboxes
  • Create a Command button
  • Arrange the controls on your Form like the picture above
  • Click first Label (Label1)
  • Rename the caption of Label1 into Bonus calculation
  • Change the font size into 24
  • Change the caption of Label2, Label3, Label4 and Label5 into Your Sales Quantity, Price Unit (USD), Percentage of Bonus, and Amount of Bonus (USD)

  • Click first Textbox (Text1), clear it's Text, and rename into txtSalQty
  • Click second Textbox (Text2), clear it's Text, and rename into txtPriceUnit
  • Click third Textbox (Text3), clear it's Text, and rename into txtBonus
  • Click fourth Textbox (Text4), clear it's Text, and rename into txtBonusAmt
  • Click Command1 button, change the caption into Calculate Bonus, and rename into cmdCalBonus

  • Click anywhere in Form1 area, and rename into frmCalBonus (look at properties window)
  • Click Project1 file (in Project explorer window)
  • Click cell next to (Name) in Properties Window, and change the Name into prjCalBonus

    Look at image above, your screen design should like that.
    Ok, now the time to save your work (your program) in order you can edit it next time (this program will be used in the next posting). Here it is tips to save your VB6 program:
  • Click File menu on your menu bar
  • Click Save Project As... menu
  • On Save File As window, type frmCalBonus on the cell with label File name:
  • Select the folder you want to save in, then click Save button
  • Then on Save Project As window, Type prjBonus on the cell with label File name:
  • Then click Save button

    Note: The extention .vbp and .frm will automatically given by the system.
    Ok, now your program has been saved. You have 2 files in your project: prjBonus.vbp and frmCalBonus.frm and you can close your VB program. I'll continue to write the program coding in the next posting.


  • How to Add Controls to Form in VB6 Programming

    Friday, August 21, 2009 · 0 comments

    Visual Basic Toolbox and Form
    In the previous posting I've told you that one of Visual Basic Development Environment's element is Toolbox, that contains controls. And another element is Form Design window (Form). You can place controls into your Form to build your VB application.

    Now, how to place controls from Toolbox into your Form? Ok, it's just a simple way to do that. You just click, drag and drop the control you want. Or also you can double-click the icon Toolbox of the control you want.

    There are two manners to place (add) controls to a form. The first manner is double-click the Toolbox icon of the control you want to add. In instance it will place the control with default size in the centre of active form.
    Control placed in the center active form











    When you adding more than one controls with this way, they will be placed on the top of each other in the center of the form. Then you can reposition/rearrange them into new place you want in the form.

    The second manner, you can follow below steps:

  • First, click on the Toolbox icon of the control you want (for example TextBox control)
    How to add control to form - Click icon you want

  • Place your mouse pointer on the Form (the pointer change to crosshair / + sign)
  • Position the pointer in the place where you want the upper left corner of the control
  • Click your mouse, hold, and drag (so your pointer draws a rectangle that indicate the size of your control)

    How to add control to form - Drag your mouse

  • When your control is correctly sized, release your mouse, so the control now appears on your Form
    How to add control to form - Release your mouse

    After you add controls to your form, you can reposition them to new location in the form, and also you can resize them according to your need. That are the way how to add / place control to your Visual Basic form.

    Ok, I think you get it. And I'll write another Visual Basic tips in the next posting.


  • Archives

    Labels