Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts
Thursday, 18 August 2011

Animation-Part II

0 comments
 

Animation using a DragDrop Procedure

Drag and drop is a common windows application where you can drag and drop an object such as a file into a folder or into a recycle bin. This capability can be easily programmed in visual basic. In the following example, I am creating a simulation of dragging the objects into a recycle bin, then drop a fire and burn them away.
In this program, I put 6 images on the form, one of them is a recycle bin, another is a burning recycle bin , one more is the fire, and three more images. In addition, set  the property dragmode of all the images( including the fire) that are to be dragged to  1(Automatic) so that dragging is enabled, and set the visible property of  burning recycle bin to false at start-up. Besides, label the tag of fire as fire in its properties windows. If you want to have better dragging effects, you need to load an appropriate icon under the dragIcon properties for those images to be dragged, preferably the icon should be the same as the image so that when you drag the image, it is like you are dragging the image along.
The essential event procedure  in this program is as follows:
Private Sub Image4_DragDrop(Source As Control, X As Single, Y As Single)
Source.Visible = False
If Source.Tag = "Fire" Then
Image4.Picture = Image5.Picture
End If
End Sub
Source refer to the image to be dragged. Using the code Source.Visible=False means it will disappear after being dragged into the recycle bin(Image4).If  the source is Fire, then the recycle bin will changed into a burning recycle bin , which is accomplished by using the code  Image4.Picture = Image5.Picture, where Image 5 is the burning recycle bin.
For details of this program, please refer to my game and fun programming page or click this link, Recycle Bin.
 

Animation for a complete motion

So far those examples of animation shown in lesson 23 only involve movement of static images. In this lesson, you will be able to create true animation where an action finish in a complete cycle, for example, a butterfly flapping its wings. In the following example, I used eight picture frames of a butterfly which display a butterfly flapping its wing at different stages.
You can actually copy the above images and use them in your program. You need to put all the above images overlapping one another,  make image1 visible while all other images invisible at start-up. Next, insert a command button and label it as Animate. Click on the command button and key in the statements that make the images appear and disappear successively by using the properties image.visible=true and image.visible=false. I use If..... Then and Elseif to control the program flow. When you run the program, you should be able to get the following animation.

The Interface

 

The Code

Private Sub Command1_Click()
If Image1.Visible = True Then
Image1.Visible = False
 Image2.Visible = True
ElseIf Image2.Visible = True Then
Image2.Visible = False
Image3.Visible = True
ElseIf Image3.Visible = True Then
Image3.Visible = False
Image4.Visible = True
ElseIf Image4.Visible = True Then
Image4.Visible = False
Image5.Visible = True
ElseIf Image5.Visible = True Then
Image5.Visible = False
Image6.Visible = True
ElseIf Image6.Visible = True Then
Image6.Visible = False
Image7.Visible = True
ElseIf Image7.Visible = True Then
Image7.Visible = False
Image8.Visible = True
ElseIf Image8.Visible = True Then
Image8.Visible = False
Image1.Visible = True
End If
End Sub
If you wish to create the effect of the butterfly flapping its wing and flying at the same time, then you could use the Left and Top properties of an object, such as the one used in the examples of lesson 23. Below is an example of a subroutine where the butterfly will flap its wing and move up at the same time. You can also write subroutines that move the butterfly to the left, to the right and to the bottom.
Sub move_up( )
If Image1.Visible = True Then
Image1.Visible = False
Image2.Visible = True
Image2.Top = Image2.Top - 100

ElseIf Image2.Visible = True Then
Image2.Visible = False
Image3.Visible = True
Image3.Top = Image3.Top - 100

ElseIf Image3.Visible = True Then
Image3.Visible = False
Image4.Visible = True
Image4.Top = Image4.Top - 100
ElseIf Image4.Visible = True Then
Image4.Visible = False
Image5.Visible = True
Image5.Top = Image5.Top - 100
ElseIf Image5.Visible = True Then
Image5.Visible = False
Image6.Visible = True
Image6.Top = Image6.Top - 100

ElseIf Image6.Visible = True Then
Image6.Visible = False
Image7.Visible = True
Image7.Top = Image7.Top - 100

ElseIf Image7.Visible = True Then
Image7.Visible = False
Image8.Visible = True
Image8.Top = Image8.Top - 100
ElseIf Image8.Visible = True Then
Image8.Visible = False
Image1.Visible = True
Image1.Top = Image1.Top - 100
End If
End Sub


Readmore...

Animation-Part III

0 comments
 

Animation using Timer

All preceding examples of animation that you have learn in lesson 23 and lesson 24 only involve manual animation, which means you need to keep on clicking a certain command button or pressing a key to make an object animate. In order to make it move automatically, you need to use a timer. The first step in creating automatic animation is to drag the timer from the toolbox into the form and set its interval to a certain value other than 0. A value of 1 is 1 milliseconds which means a value of 1000 represents 1 second. The value of the timer interval will determine the speed on an animation.
In the following example, I use a very simple technique to show animation by using the properties Visible=False and Visible=true to show and hide two images alternately. When you click on the program, you should see the following animation.

The Code

Private Sub Timer1_Timer()
If Image1.Visible = True Then
Image1.Visible = False
Image2.Visible = True
ElseIf Image2.Visible = True Then
Image2.Visible = False
Image1.Visible = True
End If

End Sub

Next example shows a complete cycle of a motion such as the butterfly flapping its wing. Previous examples show only manual animation while this example will display an automatic animation once you start the program or by clicking a command button. Similar to the example under lesson 24.2, you need to insert a group of eight images of a butterfly flapping its wings at different stages. Next, insert a timer into the form and set the interval to 10 or any value you like. Remember to make image1 visible while other images invisible at start-up. Finally, insert a command button, rename its caption  as Animate and key in the following statements by double clicking on this button. Bear in mind that you should enter the statements for hiding and showing the images under the timer1_timer subroutine otherwise the animation would work. Clicking on the animate button make timer start ticking and the event will run after every interval of 10 milliseconds or whatever interval you have set at design time. In future lesson, I will show you how to adjust the interval at runtime by using a slider bar or a scroll bar. When you run the program, you should see the following animation:
Private Sub Form_Load()
Image1.Visible = True
x = 0
End Sub

Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
If Image1.Visible = True Then
Image1.Visible = False
Image2.Visible = True

ElseIf Image2.Visible = True Then
Image2.Visible = False
Image3.Visible = True

 
ElseIf Image3.Visible = True Then
Image3.Visible = False
Image4.Visible = True
ElseIf Image4.Visible = True Then
Image4.Visible = False
Image5.Visible = True
ElseIf Image5.Visible = True Then
Image5.Visible = False
Image6.Visible = True
ElseIf Image6.Visible = True Then
Image6.Visible = False
Image7.Visible = True
ElseIf Image7.Visible = True Then
Image7.Visible = False
Image8.Visible = True
ElseIf Image8.Visible = True Then
Image8.Visible = False
Image1.Visible = True
End If
End Sub
Readmore...

Animation-Part I

0 comments
 
Animation is always an interesting and exciting part of programming. Although visual basic is not designed to handle advance animations, you can still create some interesting animated effects if you put  in some hard thinking. There are many ways to create animated effects in VB6, but for a start we will focus on some easy methods.
The simplest way to create animation is to set the VISIBLE property of a group of images or pictures or texts and labels to true or false by triggering a set of events such as clicking a button. Let's examine the following example:
This is a program that create the illusion of moving the jet plane in four directions, North, South ,East, West. In order to do this, insert five images of the same picture into the form. Set the visible property of the image in the center to be true while the rest set to false. On start-up, a user will only be able to see the image in the center. Next, insert four command buttons into the form and change the labels to Move North, Move East, Move West and Move South respectively. Double click on the move north button and key in the following procedure:
Sub Command1_click( )
Image1.Visible = False
Image3.Visible = True
Image2.Visible = False
Image4.Visible = False
Image5.Visible = False
End Sub
By clicking on the move north button, only image 3 is displayed. This will give an illusion that the jet plane has moved north. Key in similar procedures by double clicking other command buttons. You can also insert an addition command button and label it as Reset and key in the following codes:
Image1.Visible = True
Image3.Visible = False
Image2.Visible = False
Image4.Visible = False
Image5.Visible = False
Clicking on the reset button will make the image in the center visible again while other images become invisible, this will give the false impression that the jet plane has move back to the original position.
You can also issue the commands using a textbox, this idea actually came from my son Liew Xun (10 years old). His program is shown below:
Private Sub Command1_Click()

If Text1.Text = "n" Then
Image1.Visible = False
Image3.Visible = True
Image2.Visible = False
Image4.Visible = False
Image5.Visible = False

ElseIf Text1.Text = "e" Then
Image1.Visible = False
Image4.Visible = True
Image2.Visible = False
Image3.Visible = False
Image5.Visible = False

ElseIf Text1.Text = "w" Then
Image1.Visible = False
Image3.Visible = False
Image2.Visible = False
Image4.Visible = False
Image5.Visible = True

ElseIf Text1.Text = "s" Then
Image1.Visible = False
Image3.Visible = False
Image2.Visible = True
Image4.Visible = False
Image5.Visible = False
End If

End Sub
 
Another simple way to simulate animation in VB6 is by using the Left and Top properties of an object. Image.Left give the distance of the image in twips from the left border of the screen, and Image.Top give the distance of the image in twips from the top border of the screen, where 1 twip is equivalent to 1/1440 inch. Using a statement such as Image.Left-100 will move the image 100 twips to the left, Image.Left+100 will move the image 100 twip away from the left(or 100 twips to the right), Image.Top-100 will move the image 100 twips to the top and Image.Top+100 will move the image 100 twips away from the top border (or 100 twips down).Below is a program that can move an object up, down. left, and right every time you click on a relevant command button.
 
The Code

Private Sub Command1_Click()
Image1.Top = Image1.Top + 100
End Sub

Private Sub Command2_Click()
Image1.Top = Image1.Top - 100
End Sub

Private Sub Command3_Click()
Image1.Left = Image1.Left + 100
End Sub

Private Sub Command4_Click()
Image1.Left = Image1.Left - 100
End Sub
 
The fourth example let user magnify and diminish an object by changing the height and width properties of an object. It is quite similar to the previous example. The statements  Image1.Height = Image1.Height + 100  and Image1.Width = Image1.Width + 100 will increase the height and the width of an object by 100 twips each time a user click on the relevant command button. On the other hand, The statements  Image1.Height = Image1.Height - 100  and Image1.Width = Image1.Width -100 will decrease the height and the width of an object by 100 twips each time a user click on the relevant command button
The Code
Private Sub Command1_Click()
Image1.Height = Image1.Height + 100
Image1.Width = Image1.Width + 100
End Sub

Private Sub Command2_Click()

Image1.Height = Image1.Height - 100
Image1.Width = Image1.Width - 100

End Sub
You can try to combine both programs above and make an object move and increases or decreases in size each time a user click a command button.
 

Readmore...
Wednesday, 10 August 2011

Make Your First ActiveX Control 5

0 comments
 
Adding more events
Event KeyDown(KeyCode As Integer, Shift As Integer)
Event KeyUp(KeyCode As Integer, Shift As Integer)
Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
    RaiseEvent KeyDown(KeyCode, Shift)
End Sub
Private Sub Command1_KeyUp(KeyCode As Integer, Shift As Integer)
    RaiseEvent KeyUp(KeyCode, Shift)
End Sub
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    RaiseEvent MouseDown(Button, Shift, X, Y)
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    RaiseEvent MouseMove(Button, Shift, X, Y)
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    RaiseEvent MouseUp(Button, Shift, X, Y)
End Sub

Control's special events
Private Sub UserControl_Initialize()End Sub
The code that you will insert to this sub, will run when
the user first place the control on the form on design time,
and on runtime, when the form with the control on it is loaded.
Private Sub UserControl_Show()
End Sub
This event occur instantly after the Initialize event occur.
The initialize event occur After the control is loaded and before
the Control is visible (to the programmer or the user that run the program)
and the Show event occur right after the Control is visible to the programmer/user.
You can browse for other event: At the control's code window,
choose UserControl from the left ComboBox under the title bar,
and choose event with the right ComboBox.

Setting Control's propertiesAs you probably saw, when you inserted the control to your project,
the control had a default icon on the ToolBox.
To set your own Icon, Add your icon to the control ToolBoxBitmap property.

Some of the controls are invisible at runtime (Like Timer and ImageList).
To set your control to be invisible at runtime,
Set the control InvisibleAtRuntime property to True.

Make an About box
Make an About property, that when the user will
press on the About property cell on the control Properties window,
A message box will show up with your details.
Add the following code to your form:

Public Sub AboutBox()
    MsgBox "This is my message", , "This is my title"
End Sub

Now from the menu choose Tools->Procedure Attributes.



From the 'Name' combo box choose AboutBox,
Click on the Advanced button, and from the 'Procedure ID'
choose AboutBox, and press OK.
Now a New property has just been added to your control - the About Property.


Readmore...

Make Your First ActiveX Control 4

0 comments
 
Adding more properties to the control
Now we want that the control will have all the Command Button properties.
lets add the BackColor property. Enter the following code to your form:
Public Property Get BackColor() As OLE_COLOR
    BackColor = Command1.BackColor
End Property
Public Property Let BackColor(ByVal New_BackColor As OLE_COLOR)
    Command1.BackColor() = New_BackColor
    PropertyChanged "BackColor"
End Property
Enter the following line to the UserControl_ReadProperties function:
Command1.BackColor = PropBag.ReadProperty("BackColor", &H8000000F)
Enter the following line to the UserControl_WriteProperties function:
Call PropBag.WriteProperty("BackColor", Command1.BackColor, &H8000000F)

The OLE_COLOR is the type of the BackColor property variable,
the same as the Boolean is the type of the Enabled property variable,
and the Integer is the type of the Height property variable.


What we did now is almost the same as we did with the Text property.
The difference is that in the text property we used
a variable (TextVariable) to store the property information.
Here we not using a variable, we read and write the information
directly to the Command1.BackColor property.

The Command1.BackColor property is here our variable that store
the information. Why is that?
Because when the user set the Control BackColor property,
we actually want to set the Command1 BackColor property.
Suppose the user set the Control BackColor to Black.
In that case, We want to set the Command1 BackColor to Black.
So actually, the Control BackColor property is the
Command1 BackColor property.
So instead of reading and writing to variable,
we read and write directly to the Command1 BackColor property.
It's exactly the same thing with all of the other properties.

Adding the rest of the properties
Public Property Get Enabled() As Boolean
    Enabled = Command1.Enabled
End Property
Public Property Let Enabled(ByVal New_Enabled As Boolean)
    Command1.Enabled() = New_Enabled
    PropertyChanged "Enabled"
End Property
Public Property Get Font() As Font
    Set Font = Command1.Font
End Property

Public Property Set Font(ByVal New_Font As Font)
    Set Command1.Font = New_Font
    PropertyChanged "Font"
End Property

Public Property Get Picture() As Picture
     Set Picture = Command1.Picture
End Property

Public Property Set Picture(ByVal New_Picture As Picture)
Set Command1.Picture = New_Picture
    PropertyChanged "Picture"
End Property

Public Property Get DisabledPicture() As Picture
     Set DisabledPicture = Command1.DisabledPicture
End Property

Public Property Set DisabledPicture(ByVal New_DisabledPicture As Picture)
    Set Command1.DisabledPicture = New_DisabledPicture
    PropertyChanged "DisabledPicture"
End Property

Public Property Get MousePointer() As MousePointerConstants
    MousePointer = Command1.MousePointer
End Property

Public Property Let MousePointer(ByVal New_MousePointer As MousePointerConstants)
    Command1.MousePointer() = New_MousePointer
    PropertyChanged "MousePointer"
End Property

Public Property Get MouseIcon() As Picture
    Set MouseIcon = Command1.MouseIcon
End Property
Public Property Set MouseIcon(ByVal New_MouseIcon As Picture)
    Set Command1.MouseIcon() = New_MouseIcon
    PropertyChanged "MouseIcon"
End Property

Public Property Get Caption() As String
    Caption = Command1.Caption
End Property
Public Property Let Caption(ByVal New_Caption As String)
    Command1.Caption() = New_Caption
    PropertyChanged "Caption"
End Property

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    Command1.BackColor = PropBag.ReadProperty("BackColor", &H8000000F)
    Command1.Enabled = PropBag.ReadProperty("Enabled", True)
    Set Font = PropBag.ReadProperty("Font", Ambient.Font)
    Set Picture = PropBag.ReadProperty("Picture", "")
    Set DisabledPicture = PropBag.ReadProperty("DisabledPicture", "")
    Command1.MousePointer = PropBag.ReadProperty("MousePointer", 0)
    Set MouseIcon = PropBag.ReadProperty("MouseIcon", "")
    Command1.Caption = PropBag.ReadProperty("Caption", "Button")
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("BackColor", Command1.BackColor, &H8000000F)
    Call PropBag.WriteProperty("Enabled", Command1.Enabled, True)
    Call PropBag.WriteProperty("Font", Font, Ambient.Font)
    Call PropBag.WriteProperty("Picture", Picture, "")
    Call PropBag.WriteProperty("DisabledPicture", DisabledPicture, "")
    Call PropBag.WriteProperty("MousePointer", Command1.MousePointer, 0)
    Call PropBag.WriteProperty("MouseIcon", Command1.MouseIcon, "")
    Call PropBag.WriteProperty("Caption", Command1.Caption, "Button")
End Sub

The difference between SET and GETAs you see, in some of the properties, we use SET instead of GET.
When you want to change the Command Button Picture property,
you press on the Button with the 3 dots on him that
found in the "Picture" cell and then browse for your picture.
When we want to set property that uses the browse button, we use SET instead of GET.

Readmore...

Make Your First ActiveX Control 3

0 comments
 
Implementing events
We know what the user wants to pop up when he
clicks the button - the Text in TextVariable.
Now we need to pop up the message box when the user click the button.
We want 2 events: KeyPress and Click.
First we need to declare them.
enter the following code to your form:

Event Click()
Event KeyPress(KeyAscii As Integer)
How do we know that the Click event not getting parameters,
and the KeyPress event get the KeyAscii parameter?
Double Click on the command button.
2 new lines have been inserted to your code:
Private Sub
Command1_Click()
End Sub
as you see, the Click event gets no parameters.
Now go to the button KeyPress event,
via the right ComboBox under the title bar,
which now showing the current event - Click
After you choose KeyPress from the combobox,
2 new lines were inserted to your code:

Private Sub Command1_KeyPress(KeyAscii As Integer)
End Sub


As you can see, the button KeyPress event get the KeyAscii parameter.

Implementing The KeyPress EventWe don't want to change the KeyPress Event. We want that the code the user will insert to the
KeyPress Event  will be launched as usual,  without any changes.
So we will enter the following lines to our form:

Private Sub Command1_KeyPress(KeyAscii As Integer)
    RaiseEvent KeyPress(KeyAscii)
End Sub
Code Explanation: when the user press on the Command1 Button,
simply launch the Control (MyControl) KeyPress event.
The 'RaiseEvent' function launch an event.
It launch the event with the KeyAscii parameter that has been
received from the Command1 KeyPress event.

Implementing The Click EventWe want to pop up the message box when the
Click event occur, and then run the code that
the user entered in the MyControl1 - Click event.

Enter the following code to your form:
Private Sub Command1_Click()
    MsgBox (TextVariable)
    RaiseEvent Click
End Sub
When the user Click on Command1, pop up a message box
with the TextVariable string.
Then run the code that the user inserted to the control Click event.
What will happen if you omit the 'RaiseEvent Click' line?
When the user will click the button, the message box will pop up,
and the code that the user entered to the MyControl1 Click
event will not be apply.
So actually the user will not be able to program the click event.

By now, your code should look like this:
Dim TextVariable As String

Event
Click()
Event KeyPress(KeyAscii As Integer)
Private Sub Command1_Click()
    MsgBox (TextVariable)
    RaiseEvent Click
End Sub
Private Sub Command1_KeyPress(KeyAscii As Integer)
    RaiseEvent KeyPress(KeyAscii)
End Sub
Private Sub UserControl_Resize()
    Command1.Width = UserControl.Width
    Command1.Height = UserControl.Height
End Sub
Public Property Get Text() As String
   
Text = TextVariable
End Property
Public Property Let Text(ByVal New_Text As String)
    TextVariable = New_Text
    PropertyChanged "Text"
End Property

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    TextVariable = PropBag.ReadProperty("Text", "There is no message")
End Sub

Private Sub
UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("Text", TextVariable, "There is no message) 
End Sub

Compiling and running the control
Now lets see how the control is working by now.
Save the project (File->Save Project).

lets compile the project to OCX Control.
From the menu, choose   File->Make MyFirstOCX.ocx   and press OK.

Now your OCX Control has been created, AND registered with your system.

Start a new project and enter your control to the project
(From the menu choose Project->Components, mark the MyFirstOCX checkBox and press OK)
 Now you see the new control at the ToolBox.




Insert it to your form, resize it to your preferred size, and insert "hello" to the control Text property.Run the program and click the button. A "hello" message box is popping.



Readmore...

Make Your First ActiveX Control 2

0 comments
 



Starting the programming
Add 1 Command Button to your form (named Command1).
This is how your control will look like.
Your control is the form and everything on it.
We don't want that the control will be a form with a button on it,
we want that the control will be a button only,
without the form around him.
So resize the form to be at the size of the button exactly,
so you won't see the form.
So now the Control look like a button.
But it's not a button, it's a form with button on it.
But what if the user will resize the control at design time
(like you do to Command Button, after you enter it to your form)?

Suppose you have a regular form with a button on it,
and the form is resized to the button size (like in your current control).
When the user will resize the form, he will see the form
that was before 'under' the button.
The same thing will happen in our case.

To solve this problem, when the user resize the form (i.e the control),
we need to resize the button to fit the form and the form will be
again at the size of the button.
When the user resize the control, he actually try to resize the button.
Enter the following code to let the user resize the button
instead of resizing the form:

Private Sub UserControl_Resize()
   Command1.Width = UserControl.Width
   Command1.Height = UserControl.Height
End Sub
UserControl is the name of the Form/Control.
thus, UserControl.Width is the Form/Control width,
UserControl.Height is the Form/Control Height and UserControl_Resize()
is the event that occur when the user resize the control.

Implementing Control's PropertiesEvery control has few properties as default: Name, Left, Index, Tag, and more.
Our control will inherit those properties as default.
But we want that our control will have some properties that
he doesn't get as default, like Text - the text of the message box
that will pop when the user press the button.
Implementing Text PropertyWe have 2 occasions: when reading the Text property and
when changing the Text property.
The reading occasion occur when we want to
read the porperty that the user set.
For example if the user set the control Text property to "hello",
the reading result will return "hello".
Lets implement first the reading occasion.

Enter the following code to your form:
Dim TextVariable As String
The TextVariable will be the variable that holds for us the value of the Text property, therefore the String that will be inserted into the message box.

Enter the following code to your form:
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    TextVariable = PropBag.ReadProperty("Text", "There is no message")
End Sub


The function above says: read the control's "Text" property.
If the reading yield nothing, set as default the Text property
to be "There is no message".
We called to read the Text property,
now we have to implement the reading method:
Public Property Get Text() As String
   
Text = TextVariable
End Property
The TextVariable will hold the Text property value,
so we simply need to return the value of TextVariable.
TextVariable is a string, and the calling for reading the
Text property value will return string, therefore the 'As String' above.

Implementing The Writing MethodThe write occasion occur when the user change the Text property.
In that case, we need to update the variable that
holds for us the propery value (TextVariable).

Enter the Following code to your form:
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("Text", TextVariable, "There is no message")
End Sub

The function above says:
Write the new property value to TextVariable,
and update the Text Property.
If the writing to the TextVariable return nothing,
Set the Text property value to be "There is no message".

Now we have to implement the writing method,
where the new property value will be entered into the TextVariable.
Enter the following code to your form:
Public Property Let Text(ByVal New_Text As String)
    TextVariable = New_Text
    PropertyChanged "Text"
End Property

The new Text property value is passed with the New_Text parameter.
Of course this parameter have to be String, because the Text property holds String.
We set the TextVariable variable to hold the new Text property value.
Then we announce that the "Text" property has been changed.

Readmore...

Make Your First ActiveX Control 1

0 comments
 
First of all, what is ActiveX Control?ActiveX control is control like all visual basic common controls: Command Button, Label, etc.
You can make your own ActiveX control, for example  hover button control, and use it in every VB program you make without addition of code. Instead of writing the same code every time you want to use the hover button, make once hover button  ActiveX control, and drag it to your form every time you want to use it, like it was the usual Command Button.

How can you make your own ActiveX control?In this tutorial we will make a button control, that will pop a message box when the user will click on it.
I know that it's not very useful, and for this purpose you don't have to make an ActiveX control, but this example  will teach you how to make an ActiveX control.

Getting startedChoose from the menu (in VB 6.0) File->New Project
Select ActiveX Control and press OK.
A new form without a title bar has been opened
Choose from the menu Project->Project1 Properties.
 in the 'Project Name' Text Box is written by default 'Project1'
 Change the 'Project Name' to myFirstOCX.

This is what you will see, after you complete the control,
In the Project->Components menu - where the user choose
which OCX controls to add to his project

Right click on the form, and choose properties from the menu.
The form's name property by default is UserControl1. Change it to MyControl.
 This will be default name of the control when the user will
insert it to his form: MyControl1, MyControl2, and so on.

Readmore...

Using "Select Case"

0 comments
 
The "Select Case" conditional statement
is very useful when you have in your conditional
statement many conditions.

For example, in the last page we had the following code:


Dim Name As String
Name = InputBox("Please enter your name")
If (Name = "elvis") Then
   MsgBox "your name is elvis"
ElseIf (Name = "tim") Then
   MsgBox "your name is tim"
ElseIf (Name = "john") Then
   MsgBox "your name is john"
ElseIf (Name = "steve") Then
   MsgBox "your name is steve"
Else
   MsgBox "I don't know you"
End If


When you have many conditions, the "If Statement"
become too bulky.
In this case, you can use instead of it the "Select Case Statement".
The following "Select Case Statement" is do EXACTLY the same
thing as the code above:

Dim Name As String
Name = InputBox("Please enter your name")
Select Case Name
    Case "elvis": MsgBox "your name is elvis"
    Case "tim": MsgBox "your name is tim"
    Case "john": MsgBox "your name is john"
    Case "steve": MsgBox "your name is steve"
    Case Else: MsgBox "I don't know you"
End Select



The "Select Case" syntax is very simple:

It Begins with:
Select Case VariableName
Then comes the conditions.
Every condition has "Case" before it, and ":" after it.
The "Select Case" conditions are little different from the "If" conditions:


The Select Case Condition   The equivalent "If" Condition
Case "elvis" VariableName = "elvis"
Case Is <> "elvis" VariableName <> "elvis"
Case 5 VariableName = 5
Case Is <> 5 VariableName <> 5
Case Is > 5 VariableName > 5
Case Is >= 5 VariableName >= 5
Case Is < 5 VariableName < 5
Case Is <= 5 VariableName <= 5


After every condition, comes the line that will be executed
if the condition will be "True".

For example, the line:

Case "elvis": MsgBox "your name is elvis"

Is the same as:

If (VariableName = "elvis") Then  
    MsgBox "your name is elvis"
End If



After all conditions, comes the Else condition (It's
optional condition, and you can omit it if you want):
Case Else:

This condition will be executed ONLY if none
of the conditions above was executed.

After all of the conditions, the statement closed with   End Select


Note that you can have more than 1 line to executed
in each condition. For example:

Dim Name As String
Name = InputBox("Please enter your name")
Select Case Name
    Case "elvis": MsgBox "your name is elvis"
                  MsgBox "you are the king!"
                  MsgBox "I didn't know you are alive!"
    Case "tim": MsgBox "your name is tim"
    Case "john": MsgBox "your name is john"
    Case Else: MsgBox "I don't know you"
End Select
Readmore...

Nested Conditional Statements

0 comments
 
Untill now, we could program simple conditional statements:
If the password is xxx - do something,
and if not - do other thing.

But what if we want to write more complicated conditional statements:
If the password is xxx - do something,
If the password is yyy - do something else,
If the password is zzz - do something else,
and if the password is none of the above - do something else.


To do that we'll use "ElseIf":

Dim Number As Integer
Number = InputBox("Please enter your age")
If (Number > 50) Then
   MsgBox "you are older than 50"
ElseIf (Number < 20) Then
   MsgBox "you are younger than 20"
Else
   MsgBox "your age is between 20 and 50"
End If



How does that code above work:

First, the first boolean expression is being evaluated:
(Number > 50)
If it's equal to True then the line that following it
(MsgBox "you are older than 50") will be executed, and
the "If statement" will be ended here, so the next
boolean expression (Number < 20) will not be checked.
If it's equal to False, the next boolean expression will be checked:
(Number < 20)
If it's equal to True, the line that following it
(MsgBox "you are younger than 20") will be executed, and
the "If statement" will be ended here.
If it's equal To False, the "Else Statement" will be executed:
MsgBox "your age is between 20 and 50"

Only 1 of the lines will be executed!


You can use more than one "ElseIf" in your "If Statement",
For example:

Dim Name As String
Name = InputBox("Please enter your name")
If (Name = "elvis") Then
   MsgBox "your name is elvis"
ElseIf (Name = "tim") Then
   MsgBox "your name is tim"
ElseIf (Name = "john") Then
   MsgBox "your name is john"
ElseIf (Name = "steve") Then
   MsgBox "your name is steve"
Else
   MsgBox "I don't know you"
End If
Readmore...

More Advanced Boolean Expressions

0 comments
 
ou can combine the operators "Not", "And" and "Or" in
the same boolean expression, using the parentheses "(" and ")"

For example:

(True And False) Or (Not False) = True

The priority of the "Not" operator is higher than
the priority of "And" and "Or" operators.

For example:

Not True And False = False, because
The first operater to be executed is the "Not" (Figure 6):
Not True = False.
Then we get the following boolean expression:
False And False = False.
If we will executed the "And" operator before
the "Not" operator, we would get WRONG answer

A Teaser:
What will be printed on the form after
executing the following code?

Dim Elvis As Boolean
Elvis = (Not False And True) And Not ((True And Not False) Or False)
Print Elvis


Answer:
 







Readmore...

Using The "Not" Operator

0 comments
 
The "Not" operator has very simple function:

Not True = False
Not False = True


Before we continue,
I'll mention that you don't have to include
"Else" in the "If" statement.

For example:

Dim Password As String
Password = InputBox("Please enter the password")
If (Password = "let me in") Then
   MsgBox "The Password is correct!"
End If



If the password is correct, The code above
will display a message box with the text
"The Password is correct!".
Otherwise, the code will do nothing,
because there is no "Else".


Lets see an example of using the operator "Not".
Copy the following code to your Form_Load event:

Dim Password As String
Password = InputBox("Please enter the password")
If Not (Password = "let me in") Then
   MsgBox "Incorrect Password!"
   End
End If



The boolean expression: Not (Password = "let me in")
is True only if the Password is not "let me in". Why?
If the Password is not "let me in",
(Password = "let me in") is False (Figure 3) ,
and we get the boolean expression: Not (False).
Not False = True, so eventually, the
expression value is True
In conclusion,
The code above will display a message box
with the text "Incorrect Password!" ONLY if
the password is different from "let me in"
Readmore...

Using "And" and "Or" In Boolean Expressions

0 comments
 
We can use "And" and "Or" operators to make more   complicated boolean expression.

In the last example we used the boolean expression Password = "let me in".

This boolean expression is "True" if the Password is "let me in", and "False" If the Password is not.

How can we make a boolean expression
that will be "True" if the Password is
"let me in" or "sam sent me", and if the
Password is other than the two above, the
boolean expression will be "False"?

To create this boolean expression we will use the "Or" operator:

(Password = "let me in") Or (Password = "sam sent me")

Put the following code in your Form_Load event:

Dim Password As String
Password = InputBox("Please enter the password")
If (Password = "let me in") Or (Password = "sam sent me") Then
   MsgBox "The Password is correct!"
Else
   MsgBox "Incorrect Password!"
   End
End If



Run the program.
If you'll type "let me in" or if you'll type "sam sent me"
the password will be correct.
If you'll type any other text the program will shut down.


The "Or" is operator, the same as "+" is operator.
How the "+" operator works we already know:
5 + 6 = 11
But how the "Or" Operator works?

The "+" operator is being executed on numbers:
Number + Other Number = The sum of both numbers.

The "Or" operator is being executed on "True" or "False":
False Or True = True
True Or False = True
True Or True = True
False Or False = False

The 4 examples above are the only options
of using the "Or" operator.

Lets see what effect has the "Or" operator on
the boolean expression we used:

(Password = "let me in") Or (Password = "sam sent me")

First, the left and right boolean expressions
are being evaluated (Figure 2).

For example, if the Password is "let me in" then
(Password = "let me in") is True
and (Password = "sam sent me") is False.

Then the boolean expression is look like this:
(True) Or (False)

As we saw in the 4 examples above, True Or False = True
So the final result of the expression is True

This boolean expression will be True if
the password is "let me in" because True Or False = True,
the expression will be True if the password is "sam sent me"
because False Or True = True,
the expression will be False if the password is NOT "let me in"
and NOT "sam sent me" because False Or False = False.

The "And" operator is similar to the "Or"
operator, except it has different Effects on boolean expressions:

True And True = True
True And False = False
False And True = False
False And False = False

When using the "And" operator, the
expression will be True only if both
boolean expressions are True.

For example, copy the following code
to your Form_Load event:


Dim UserName As String
Dim Password As String

UserName = InputBox("Please enter the user name")
Password = InputBox("Please enter the password")
If (Password = "let me in") And (UserName = "elvis") Then
   MsgBox "The login is correct!"
Else
   MsgBox "Incorrect Login!"
   End
End If



This code will pop up two InputBoxes.
The first will ask you to enter the user name,
and the second will ask you to enter the password.
The login will be correct only if both user name
and password are correct.

The boolean expression (Password = "let me in") And (UserName = "elvis")
is True only if (Password = "let me in") is True, and
(UserName = "elvis") is True, because
True And True = True, and any other
combination is equal to False.
Readmore...

The syntax of the conditional statement:

0 comments
 
If (boolean expression) Then
    The code to execute if the boolean expression is equal to True
Else
    The code to execute if the boolean expression is equal to False
End If


Lets make a password protected program.
We want to prompt the user to enter the password
at the very start of the program, so put the following
code in the Form_Load event:


Dim Password As String
Password = InputBox("Please enter the password")
If (Password = "let me in") Then
   MsgBox "The Password is correct!"
Else
   MsgBox "Incorrect Password!"
End If



Run the program.
An InputBox is appearing.
Type "let me in" (without the quotes) in the InputBox.
Note that the text is case sensitive, and if you will
type "LET ME IN" you will get a message box with the
text "Incorrect Password!".


Lets understand how this code works:

The boolean expression (Password = "let me in") value is True
ONLY if the Password variable value is "let me in".

If this boolean expression's value is True, the code
that will be executed is the code that located between
the    Then    and the   Else   (MsgBox "The Password is correct!")

If this boolean expression's value is False, and this
happens when the Password variable value is NOT "let me in",
the code that will be executed is the code that located between
the    Else    and the   End If   (MsgBox "Incorrect Password!")


If you enter a wrong password, a message box is appearing,
but the program is continue running.
To end the program, use the command   End  .
This command is simply shut down the program.

Put the End command in the "Else Section":


Dim Password As String
Password = InputBox("Please enter the password")
If (Password = "let me in") Then
   MsgBox "The Password is correct!"
Else
   MsgBox "Incorrect Password!"
   End
End If
Readmore...

InputBox

0 comments
 
In the next examples we want to receive a value
from the user when the program is running.
To do so we will use the InputBox command.

The InputBox command syntax is:

VariableName = InputBox ("Text to Display")

After executing this command, the variable
will get the value that the user has entered.

Example:
Put 1 Command button on your form and
enter the following code to its click event:

Dim Elvis As String
Elvis = InputBox("Please Enter your name")
Print Elvis



Run the program and click the button.

A Message Box is appearing with the text "Please Enter your name"
 Type your name in the text box, and press OK. Your name will be printed on the form.
Readmore...

Conditional Statements

0 comments
 
What Are Conditional Statements?
Suppose you want to protect your program with password.
You ask from the user to enter the password.
If the password is correct - you want to let the user in, if not - you want to end the program.

To do this, you have to use conditional statement because
the code you will execute (let the user in or end the program) is
depend on what is the password that the user has entered.

One of the basics of Conditional statement are Boolean variables.
Boolean variables are commonly used in programming,
and you have to understand them before continuing with conditional statements.

Boolean VariablesAs we learnt, String variables store text, and
Integer Variables Store numbers.
Boolean variable stores one of the following constant values:
"True", or "False".

For example:

Dim Kuku As Boolean
Kuku = True
Dim YoYo As Boolean
YoYo = False


What are the True and False stand for?
They are the result of every "Boolean expression".

Boolean Expressions 
Boolean expression is like a question that
the answer to it is "True" or "False"

For example:
Is 4 Plus 6 is 10? True
Is 2 bigger than 4? False

But the question
How much is 4 Plus 6?
Is not a boolean expression, because its answer is 10 (and not True or False)

Examples Of Boolean ExpressionsThe following code:

Dim ABC As Boolean
ABC = (3 > 4)
Print ABC


Will print "False" on the form.
The value of the boolean expression 3 > 4
is "False", because 3 is not greater than 4.


The following code:

Dim ABC As Boolean
ABC = ("abf" = "abf")
Print ABC


Will print "True" on the form.
The value of the boolean expression "abf" = "abf"
is "True", because "abf" is equal to "abf"


The following code:

Dim ABC As Boolean
Dim A As Integer
Dim B As Integer
A = 1
B = 2
ABC = (A + 1 = B)
Print ABC


Will print "True" on the form,
because the value of the boolean expression (A + 1 = B) is "True".


In Boolean expressions you can use the following signs:


= Equal to
<> Not Equal to
> Greater than
< Smaller than
>= Greater than or Equal to
<= Smaller than or Equal to
Readmore...
Monday, 8 August 2011

Setting the BackColor Property

0 comments
 

You can set the BackColor property at run-time in 2 ways.

Way 1: Using the Color's numeric value
Every color has numeric value.
You can simply assign this value to the BackColor property.

For example, The numeric value of the red color is &H000000FF&
If you want to set the Form BackColor property to red,
use the following code:

Command1.BackColor = &H000000FF&

You can find what is the numeric value of every
color by making the following simple steps:

Click on the BackColor property arrow button
in the properties window (Figure 10).


Figure 10

Then click on the Palette Tab (Figure 11).

Figure 11

Select your desirable color (Figure 12).



The color value will be displayed in the BackColor Property cell (Figure 13).


And once again, if you setting a Command Button's color,
don't forget to set its Style property to 1 - Graphical.

Way 2: Using the Colors ConstantsFirst of all, what is Constant?
Constant is a variable that its value can not be changed.
Constant holds a common used value.
For example, the Constant vbRed holds the value
of the red color - &H000000FF&

Instead of writing in your code:

Command1.BackColor = &H000000FF&

You can write:

Command1.BackColor = vbRed
The 2 statements above are identical, because
vbRed = &H000000FF&


Where are these constants came from?
Visual Basic automatic declare them
when the program starts.

Imagine it like as the following code is
automatic being entered to your program:

Dim vbRed As Long
vbRed = &H000000FF&

Dim vbBlue As Long
vbBlue = &H00FF0000&


And so on...

But with one exception: You can't
change the constants value.

For example, the following code is NOT allowed:

vbBlue = 5


There are more Color constants, a partial list:

vbRed, vbBlue, vbBlack, vbGreen, vbWhite, vbYellow


You can declare your own constants.
The constant declaration syntax:

Const MyVariableName = MyVariableValue


For example, the following code will declare a const
variable with the name Piano and the (const) value "abcdef"


Const Piano = "abcdef"


After this declaration, the   Print Piano  
code line will print abcde on the form.

In addition, the following code line will not be allowed:

Piano = "gggg"

Because it's been declared as a Const,
and Const value can not be changed.
Readmore...

Setting Properties At Run-Time

0 comments
 
By now you know how to set a component property before running your program (=at "Design Time"),
Now we will learn how to do that during program's Run-Time.


To change a property, use the following syntax:

TheControlName.ThePropertyName = TheNewPropertyValue

For example, suppose we have Command Button
With the name "Command1", and we want to
set its Caption property to be "Hello".
To do that, we will use the following code:

Command1.Caption = "Hello"

To test it, simply copy the line above to your
Command1 Click event, run the program
and click the button at run-time.

Note that the Hello is inside quotes because
it's a String.
You can assign a variable value to a property:

Dim MyVar As String
MyVar = "Hello"
Command1.Caption = MyVar


This code will do exactly the same as the code line above it.
Note that MyVar is without quotes, because it's variable.

And how do you set a Form caption property at run-time?
exactly the same:

Form1.Caption = "Hello"

Not all the properties get text values.
For example, Visible property can be "True" or "False"

False and True are not text strings, but
Visual Basic commonly used values that called "Boolean"

Therefore, when you assign these values to property,
You don't have to use the quotes, For example:

Command1.Visible = False
Form1.Enabled = True



Some of the properties values has the following syntax:
Number - String

For example, the Command Button's Style property can
get 2 values:

0 - Standard
1 - Graphical


To set these properties value, omit the string.
For example:

Command1.Style = 1
Form1.WindowState = 2



Some properties represent graphics, for example
The Picture property.

To set these properties at run-time, use
the LoadPicture Command.
The example below will load the ICO file "d:\games\toto.ico"
to the Picture property of Command Button with the name "MyButton"

MyButton.Picture = LoadPicture("d:\games\toto.ico")
 

Readmore...

Additional Form's Properties

0 comments
 
The Form has many of the Command Button's properties
(MousePointer, BackColor, Visible and more).
He also has additional properties:

Caption - The text that appear in the Form's title bar,
and in the taskbar.

Icon - The Icon that appear in the form's title bar (Figure 1) and in the Task bar (Figure 2).

Figure 1


Figure2

ControlBox - Set this property to "False" to remove
the title bar's Close, Minimize and Maximize buttons (Figure 3).

Figure3


MaxButton - Set this property to "False" to disable
The title bar Maximize Button.

MinButton - Set this property to "False" to disable
The title bar Minimize Button.

ShowInTaskbar - Setting this property to "False" will
cause the form not showing in the task bar.

WindowState - The initial appearance style of the form:
Minimized, Maximized, or normal.


The Form's KeyPreview PropertyTo understand this property,
lets look on the following example:

Start new project, and add 1 Command Button
(named Command1) to your form.
verify that the Form's KeyPreview property is set to "False".
Add the following code to your program:


Private Sub Command1_KeyPress(KeyAscii As Integer)
    Print "Button Pressed"
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    Print "Form Pressed"
End Sub


The code above will print "Button Pressed" on
the form when the Command Button's KeyPress event
will be executed, and print "Form Pressed" when the
Form's KeyPress event will be executed.


Run the program and press any key on the keyboard.
"Button Pressed" is appearing on the form, but
"Form Pressed" isn't appearing.

The Form's KeyPress event hasn't been executed.

When the KeyPreview property is "False",
if any control is found on the form (the command button in this case),
It will get all the Key events (KeyPress, KeyDown and KeyUp)
instead of the form.

To allow the Form's Key events be executed,
set the KeyPreview property to "True".

Lets try it. Set the KeyPreview
property to "True", run the program again
and press any key.
The Form's KeyPress event has been executed,
in addition to the Button's KeyPress event that
been executed too.

Notice That the form's KeyPress event executed
before the Button's event.
Readmore...

Additional Command Button's Properties

0 comments
 
Add 1 Command Button to your form, and change the following properties to see what they do.

Caption - the text that appear on the button

Font - The Caption's font.



ToolTipText - Insert into this property the text that will appear when the mouse is stand still on the button
Figure 3

Enabled - Setting this property to "False" will make the button be gray, and the user will no be able to press on it



BackColor - This is the background color of the button This property will take affect only after you will set theButton's Style property to 1 - Graphical

Picture - The Picture that appear on the button This property and all the following Picture related properties will take affect only after you will set the Button's Style property to 1 - Graphical


DisabledPicture - The Picture that will appear on the button when it disabled (When the Enabled property is "False", like in Figure 4).

DownPicture - The Picture that will appear on the button when the button is being pressed.
Visible - If you will set this property to "False", the Button will not appear on your form at run-time.

MousePointer - Choose here the mouse pointer (Arrow, Hourglass, and more) when it will be over the button.
If you will set the property to be 99-Custom, The mouse pointer will be the icon that you will select in the MouseIcon property.

Left - The Button X coordinate, relative to the form left border.

Top - The Button Y coordinate, relative to the form Top border.

Width - The Button width.

Height - The Button height.
Readmore...