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.


  •