Vous êtes sur la page 1sur 52

Cont…..

 Sometimes its necessary to move or


resize the controls in a form as a program
is running
 You can do it at runtime easily
 All controls have these properties
available at design time or runtime to set
their location and dimensions:
 Top - The y coordinate of the top left of the
control.
 Left - The x coordinate of the top left of the
control.
 Width - The width of the control.
 Height - The height of the control.
 Note that all measurements are in twips (1/1440 of
an inch) by default, and that the origin (0, 0) in a
form is at upper left.
 For Knowledge only:
 Twips are screen-independent units to ensure that
the proportion of screen elements are the same on
all display systems. A twip is defined as being
1/1440 of an inch.
 A Pixel is a screen-dependent unit, standing for
'picture element'. A pixel is a dot that represents
the smallest graphical measurement on a screen.
 You can also use a controls Move() method to
move a control to a new location:
 object.Move left, [top, [width, [height]]]

Private Sub Command1_Click()


Const increment = 500
Command1.Width = 2 * Command1.Width
Command1.Height = 2 * Command1.Height
Command1.Move (Command1.Left +
increment)
End Sub
 Note:
 One way of creating simple animation is to use
an Image control to display an image and use
its Move() method to move it around a form.
 Showing and hiding controls is easy: just use
the controls Visible property.
 Setting this property to True displays the
control; setting it to False hides it.

Private Sub Command1_Click()


Command1.Visible = False
End Sub
 You can get the dimensions of a forms client
area with these properties:
 ScaleWidth--The width of the client area.
 ScaleHeight--The height of the client area.
 ScaleLeft—The horizontal coordinate of
upper left of client area.
 ScaleTop--The vertical coordinate of upper
left of client area.
 And you can get the overall dimensions of the
form using these properties:
 Width--The width of the form.
 Height--The height of the form.
 Left--The horizontal coordinate of upper left
of the form
 Top--The vertical coordinate of upper left of
the form
 You can also use the ScaleMode property to set a
forms coordinate system units you don’t have to
use twips. Here are the possible values for
ScaleMode:
 0 -- User-defined
 1--Twips (1/1440ths of an inch)
 2--Points (1/72nds of an inch)
 3--Pixels
 4--Characters (120 twips horizontally, 240 twips
vertically)
 5--Inches
 6--Millimeters
 7--Centimeters
 User-Defined Coordinates
 you can set up a user-defined coordinate system:
 Just set the ScaleWidth and ScaleHeight
properties yourself.
 It Can be done like:
 If you want to plot data on a 1000x1000 grid,
just set ScaleWidth and ScaleHeight to 1000.
 To draw a scatter plot of your data, then, you
could use PSet() to set individual pixels
directly.
 If one of the points to graph was (233, 599), you
could draw that dot this way: PSet(233, 599).
 Eg: Interesting Mouse Move
Private Sub Form_MouseMove(Button As
Integer,Shift As Integer, X As Single, Y As
Single)
Form1.BackColor = QBColor(7)
DrawWidth = 3
ForeColor = QBColor(4)
PSet (X, Y)
End Sub
 If you have two forms and you want to move
from one form to another.
 Eg : In our form about the users information,
we want that when user clicks on the submit
button then a new form should appear saying
data submitted. Or you want the user to move
to another form.
 To do this the following code can be written.
Private Sub Command1_Click()
Form2.Show
Form2.Text1.Text = “Data Submitted
Successfully"
End Sub
 With the load statement we can load the form
in the memory.
 If we want to work with the form before
displaying it then we can load the form using
the load event.
 Actually in visual basic we do not need to load
the form or write the load statement for the
form. Loading and unloading statements are
automatic.
 But when we refer to the form visual basic
loads it in the memory for us.
 We use the show method to show the form on
the screen
 When we use the show method then the form
is displayed on the screen and if it is not loaded
yet then it is loaded in the memory.
 If the Form is loaded but invisible, the Show
method is used to bring the Form on Top every
other window.
 If the Form is not loaded, the Show method
loads it and then displays it.
 Modeless Forms are the normal Forms.
 Modeless Forms interact with the user and the
user allowed to switch to any other Form of the
application.
 If you do not specify the optional mode
argument, by default the mode is set to
modeless.
 The Modal Forms takes the total control of the
application where user cannot switch to any
other Forms in the application unless the Form
is closed.
 A modal Form, thus, must have a Close button
or some means to close the Form in order to
return to the Form where the Modal Form was
loaded.
 Eg check for Form1 and form2 of previous
example
 The Hide method is used to hide a Form. The
following is the syntax of the Hide Method.
FormName.Hide
 To hide a Form from within its own code, the
following code can be used.
Me.Hide
 You must understand that the Forms that are
hidden are not unloaded ; they remains in the
memory and can be displayed instantly with
the Show Method.
 When a Form is hidden, you can still access its
properties and code.
 If you hide all windows in a Visual Basic
program that has no Main() procedure in a
module, the program will end.
 Once you click on Hide button you can note
that the Form is invisible but the application is
still running.
 But when you click on Unload button you can
see that the application is terminated.
 To declare a new form based on a form you
already have, say Form1, you just use Dim :
 Then we have to set the variables value using
the New keyword
 Then we can show the newly created form
using the show() method.
 Eg:
Private Sub cmdNewForm_Click()
Dim Newform As Form1
Set Newform = New Form1
Newform.Show
End Sub
 If you have a standard Form and you want to
make that into an MDI child window inside an
MDI frame, follow these steps:
1. Add a new MDI form to the project using the
Project[vbar]Add MDI Form item.
2. Set the MDIChild property of the form you
want to use as the MDI child form (Form1
here) to True
3. Run the program; the form youve made into
the MDI child form appears in the MDI form,
 Note:
 In Visual Basic, you can use all kinds of forms
as MDI children in an MDI form, as long as
their MDIChild property is set to True.
 You can also use Show() and Hide() on those
windows to manage them as you like.
 You could arrange the MDI child forms with
their Left, Top, Width, and Height properties
 There is an easier way you can use the MDI
form method Arrange().
 You can use the Arrange() method to arrange
all the windows in the form in a cascade this
way:
 Eg:
Private Sub ArrangeAll_Click()
Me.Arrange vbCascade
End Sub

 The possible values to pass to Arrange() to


specify the way you want to arrange MDI
children are:
 vbCascade – 0 -- Cascades all nonminimized
MDI child windows
 vbTileHorizontal -- 1 -- Tiles all nonminimized
MDI child forms horizontally
 vbTileVertical – 2 -- Tiles all nonminimized
MDI child forms vertically
 vbArrangeIcons -- 3 -- Arranges icons for
minimized MDI child forms
 We have already created new forms in coding
and displayed or hide as per our requirements.
 The same can be done with the MDI forms also
 We can create a new MDI child form in the
code as shown in the example.
 We will also change the caption of the new
MDI form.
Private Sub NewWindow_Click ()
Dim NewForm As Form1
Set NewForm = New Form1
NewForm.Caption = "Document"
NewForm.Show
End Sub
 You can create arrays of many types of objects,
excluding such objects that there can only be
one of, like the application object, App
 You create an array of forms just as you would
create an array of any other kind of object
 Dim Forms(1 To 20) As Form1
 If we declare this array, Forms(), as a form-
level array in the MDI form, we can refer to
that array in all procedures in the MDI form.
 Suppose we have a procedure
NewWindow_Click()
 We create a new form and add it to the form
array
 We set up a static variable to hold the total
number of MDI child forms, NumberForms,
and increment that variable now that were
adding a new form
Private Sub NewWindow_Click()
Static NumberForms
NumberForms = NumberForms + 1
Set Forms(NumberForms) = New Form1
End Sub
 A view is just a window into a document, and
if a document has multiple views open, the
user can scroll around in different parts of the
same document at the same time.
 We have been able to open the same document
in several view windows now but what if the
user starts typing into one view? All the other
views should also be updated with the new
text as well.
 Example:
 In this example, the MDI child windows will be
based on a form, Form1, in which we have
placed a text box.
 The user can open as many MDI child
windows as they like with the New item in the
Window menu. When they type in one MDI
Childs text box, however, we should mirror
any such changes in the other MDI children’s
text boxes as well.
 Add a new module to the project so that we
can set up a global array of forms, Forms, and
an array index variable, NumberForms, in that
module
Public Forms(1 To 20) As Form1
Public NumberForms As Integer
 We add the following code to our form1
Private Sub NewWindow_Click()
NumberForms = NumberForms + 1
Set Forms(NumberForms) = New Form1
Forms(NumberForms).Caption = "Document"
& Str(NumberForms)
Forms(NumberForms).Show
End Sub
Now the Forms array holds the MDI children in
our program.
 When the user types text into the text box
displayed in an MDI child, we want to update
all the other MDI children as well, making
them display the same text.
 When you type into a text box, a Change event
occurs, and well add code to that events
handler function to update all the other MDI
children.
Private Sub Text1_Change()
Dim Text As String
Text = Text1.Text
For index = 1 To NumberForms
Forms(index).Text1.Text = Text
Next index
End Sub
 In order to take the user’s input we can use the
basic input boxes
 The limitations of the input boxes is that they can
accept only one line of input.
 So instead of using these input boxes we can use
Dialog Boxes.
 To add a dialog box to a project, select the
Project[vbar]Add Form item.
 Visual Basic already has a predefined dialog box
form, named Dialog, so select that in the Add
Form box and click Open.
 This dialog box comes with an OK and Cancel
button, and its BorderStyle property is already
set to 3, which creates a fixed dialog-style
border with only one control button: a close
button.
 We add a text box, Text1, to the dialog box
 Next, we declare a Public string, Feedback, in
the dialog boxs (General) section; this string
will hold the text that the user gives us as
feedback
 We can write the code for cancel button as
Hide
 For Ok button we can take the feedback of the
user:
Private Sub OKButton_Click()
Feedback = Text1.Text
Hide
End Sub
 In our main form we can display the dialogue
box using the command button.
 Note:
 One good rule for constructing dialog boxes:
always add a Cancel button so that if users
open the dialog box by mistake, they can close
it without problems
 Next, we can display the feedback that the user
has given us, if any, by examining the dialogs
Feedback string this way:
Dialog.Show 1
Text1.Text = Dialog.Feedback
 The MsgBox() Function:
 Heres the syntax:
 MsgBox(prompt[, buttons] [, title] [, helpfile,
context])
 The prompt argument holds the string
displayed as the message in the dialog
box.(The maximum length of prompt is
approximately 1,024 characters.)
 The buttons argument specifies what to put
into the message box, as specified in Table 4.2.
The default value for buttons is 0.
 The title parameter holds the string displayed
in the title bar of the dialog box.
 The helpfile argument is a string that identifies
the Help file to use to provide context-sensitive
Help for the dialog box.
 The context argument is the Help context
number assigned to the appropriate Help topic
 You can use the InputBox() function to get a
string of text from the user. Heres the syntax
for this function:
 InputBox(prompt[, title] [, default] [, xpos] [, ypos]
[, helpfile, context])
 The default argument is a string displayed in
the text box as the default response if no other
input is provided
 The xpos argument is a number that specifies
(in twips) the horizontal distance of the left
edge of the dialog box from the left edge of the
screen.
 The ypos argument is a number that specifies
(in twips) the vertical distance of the upper
edge of the dialog box from the top of the
screen
 The InputBox() function returns the string the
user entered.
 You can pass forms to procedures just as you
would any object
 Here, weve set up a subroutine,
ColorWindowWhite(), to turn the background
color of a form to white
Sub ColorWindowWhite(FormToColor As Form)
FormToColor.BackColor = RGB(255, 255, 255)
End Sub
 In the button click event we can write
Private Sub Command1_Click()
ColorWindowWhite Me
End Sub
 You can set the WindowState property to
maximize or minimize the forms.
 0Normal

 1Minimized

 2Maximized

 Eg:

Private Sub Command1_Click()


WindowState = 1
End Sub
 You can also set the Enabled property to
enable or disable a window (when its
disabled, it will only beep if the user tries to
give it the focus). You set the Enabled property
to True to enable a window and to False to
disable it.
Questions......?

Vous aimerez peut-être aussi