• Properties A few useful properties for the command button are: Property
  • Events There is only one command button event of interest, but it is a very important one: Event
  • BASIC - The First Lesson
  • Event Procedure Structure You know, by now, that event procedures are viewed in the Visual Basic code window. Each event procedure has the same general structure. First, there is a header
  • EventName
  • Assignment Statement The simplest, and most used, statement in BASIC is the assignment
  • Property Types Recall a property describes something about a control: size, color, appearance. Each property
  • ControlName.PropertyName = PropertyValue
  • frmExample.Visible = False This says the current Visible property of the form is replaced by the Boolean value False. We could make it come back with: frmExample.Visible = True
  • Beginning Visual Basic Project Design, Forms, Command Buttons Review and Preview




    Download 302 Kb.
    bet2/3
    Sana25.12.2019
    Hajmi302 Kb.
    #4849
    1   2   3

    Command Button Control
    The command button is one of the more widely used Visual Basic controls. Command buttons are used to start, pause, or end particular processes. The command button is selected from the toolbox. It appears as:
    In Toolbox: On Form (default properties):


    Properties
    A few useful properties for the command button are:
    Property Description

    Name Name used to identify command button. Three letter prefix for command button names is cmd.

    Caption Text that appears on the command button.

    Font Sets style, size, and type of caption text.

    Left Distance from left side of form to left side of command button.

    Top Distance from top side of form to top side of command button.

    Width Width of the command button in twips.

    Height Height of command button in twips.

    Enabled Determines whether command button can respond to user events (in run mode).

    Visible Determines whether the command button appears on the form (in run mode).

    Example
    Start Visual Basic and start a new project. Put a command button on the form. Move the button around and notice the changes in Top and Left properties. Resize the button and notice how Width and Height change. Set the Caption property.
    Many controls, in addition to the command button, have a Font property, so let’s take a little time to look at how to change it. Font establishes what the Caption looks like. When you click on Font in the properties window, a button with something called an ellipsis will appear on the right side of the window:

    Click this button and a Font Window will appear:



    With this window, you can choose three primary pieces of information: Font, Font Style, and Size. You can also have an underlined font. This window lists information about all fonts stored on your computer. To set the Font property, make your choices in this window and click OK. Try different fonts, font styles, and font size for the command button Caption property.
    Two other properties listed for the command button are Enabled and Visible. Each of these properties can either be True (On) or False (Off). Most other controls also have these properties. Why do you need these?
    If a control’s Enabled property is False, the user is unable to access that control. Say you had a stopwatch project with a Start and Stop button:

    You want the user to click Start, then Stop, to find the elapsed time. You wouldn’t want the user to be able to click the Stop button before clicking the Start button. So, initially, you would have the Start button’s Enabled property set to True and the Stop button’s Enabled property set to False. This way, the user can only click Start. Once the user clicked Start, you would swap property values. That is, make the Start button’s Enabled property False and the Stop button’s Enabled property True. That way, the user could now only click Stop.
    The effects of a False Enabled property are only evident when Visual Basic is in run mode. When a command button is not Enabled (Enabled is False), it will appear ‘hazy’ and the user won’t be able to click it. When Stop is not Enabled on the stopwatch, it looks like this:

    So, use the Enabled property when you want a control on the form to be temporarily disabled. This is a decision made in the project design process we discussed earlier.
    The Visible property is a bit more drastic. When a control’s Visible property is set to False (its default value is True), the control won’t even be on the form! Now, why would we want a control we just placed on the form, set properties for, and wrote event procedures for, to be invisible? The answer is similar to that for the Enabled property. Many times in a project, you will find times when you want a control to temporarily go away. Remember the Sample project in Class 1 where check boxes controlled whether toys were displayed or not. The display of the toys was controlled via the image control’s Visible property. Or, in the little stopwatch example, instead of setting a button’s Enabled property to False to make it ‘unclickable,’ we could just set the Visible property to False so it doesn’t appear on the form at all. Either way, you would obtain the desired result. This is another project design decision. One more thing - like the Enabled property, the effects of Visible being False are only evident in run mode. This makes sense. It would be hard to design a project with invisible controls!
    Now, play with the Enabled and Visible properties of the command button in the example you have been working with. Once you set either property, run the project to see the results. Note with Enabled set to False, you can’t click the button. Note with Visible set to False, the button isn’t there. When done, stop the example project.
    Events
    There is only one command button event of interest, but it is a very important one:
    Event Description

    Click Event executed when user clicks on the command button with the mouse.
    Every command button will have an event procedure corresponding to the Click event.

    BASIC - The First Lesson
    At long last, we are ready to get into the heart of a Visual Basic project - the BASIC language. You have seen that, in a Visual Basic project, event procedures are used to connect control events to actual actions taken by the computer. These event procedures are written using BASIC. So, you need to know BASIC to know Visual Basic. In each subsequent class in this course, you will learn something new about the BASIC language.
    Event Procedure Structure
    You know, by now, that event procedures are viewed in the Visual Basic code window. Each event procedure has the same general structure. First, there is a header line of the form:
    Private Sub ControlName_EventName()
    This tells us we are working with a Private (only accessible from our form), Subroutine (another name for a event procedure) that is executed when the event EventName occurs for the control ControlName. Makes sense, doesn’t it?
    The event procedure code begins following the header line. The event procedure code is simply a set of line-by-line instructions to the computer, telling it what to do. The computer will process the first line, then the second, then all subsequent lines. It will process lines until it reaches the event procedures footer line:
    End Sub

    The event procedure code is written in the BASIC language. BASIC is a set of keywords and symbols that are used to make the computer do things. There is a lot of content in BASIC and we’ll try to look at much of it in this course. Just one warning at this point. We’ve said it before, but it’s worth saying again. Computer programming requires exactness - it does not allow errors! You must especially be exact when typing in event procedures. Good typing skills are a necessity in the computer age. As you learn Visual Basic programming, you might like also to improve your typing skills using some of the software that’s available for that purpose. The better your typing skills, the fewer mistakes you will make in building your Visual Basic applications.


    Assignment Statement
    The simplest, and most used, statement in BASIC is the assignment statement. It has this form:
    LeftSide = RightSide
    The symbol = is called the assignment operator. You may recognize this symbol as the equal sign you use in arithmetic, but it’s not called an equal sign in computer programming. Why is that?
    In an assignment statement, we say whatever is on the left side of the assignment statement is replaced by whatever is on the right side. The left side of the assignment statement can only be a single term, like a control property. The right side can be just about any legal BASIC expression. It might have some math that needs to be done or something else that needs to be evaluated. If there are such evaluations, they are completed before the assignment. We are talking in very general terms right now and we have to. The idea of an assignment statement will become very obvious as you learn just a little more BASIC.

    Property Types
    Recall a property describes something about a control: size, color, appearance. Each property has a specific type depending on the kind of information it represents. When we use the properties window to set a value in design mode, Visual Basic automatically supplies the proper type. If we want to change a property in an event procedure using the BASIC assignment statement, we must know the property type so we can assign a properly typed value to it. Remember we use something called ‘dot notation’ to change properties in run mode:
    ControlName.PropertyName = PropertyValue
    ControlName is the Name property assigned to the control, PropertyName is the property name, and PropertyValue is the new value we are assigning to PropertyName. We will be concerned with four property types.
    The first property type is the integer type. These are properties that are represented by whole, non-decimal, numbers. Properties like the Top, Left, Height, and Width properties are integer type. So, if we assign a value to an integer type property, we will use integer numbers. As an example, to change the width property of a form named frmExample to 4,000 twips, we would write in BASIC:
    frmExample.Width = 4000
    This says we replace the current Width of the form with the new value of 4000. Notice you write 4,000 as 4000 in BASIC - we can’t use commas in large numbers.
    A second property type is the long integer type. And, a long integer is just like its name says. An integer type property can have values up to 32,767. Sometimes, we need bigger numbers than this, hence the long integer type. A long integer can have a value up to 2,147,483,647. That’s pretty big, but do you realize Bill Gates couldn’t even write down his net worth using a long integer? Maybe he needs someone at Microsoft to invent a very long integer type! The most common properties that uses long integers are colors, like the BackColor and ForeColor properties you will see for some controls. Remember, in a past class, we saw that the property value for gray is written as &H8000000F& - that’s a shorthand notation (called a hexadecimal number) for a long integer. When assigning color properties, we must use long integers.
    Fortunately, Visual Basic gives us lots of easy ways to refer to long integer numbers for colors, so it makes working with long integers easy. One way to use colors is with symbolic constants. Symbolic constants are used many places in Visual Basic - you’ll see lots of them as you work through the course. All symbolic constants start with the two letters vb (Visual Basic). Some symbolic constants for colors are:
    vbBlack - Black vbRed - Red

    vbGreen - Green vbYellow - Yellow

    vbBlue - Blue vbMagenta - Magenta (purple)

    vbCyan - Cyan (sky blue) vbWhite - White
    Each of these constants ‘stores’ the corresponding long integer value for the color it represents. To change our example form’s BackColor property to blue, you would use this assignment statement:
    frmExample.BackColor = vbBlue
    This says the BackColor of the form is replaced by long integer value represented by the symbolic constant named vbBlue.
    Another property type is the Boolean type. It takes its name from a famous mathematician (Boole). It can have two values: True or False. We saw that the Enabled and Visible properties for the command button have Boolean values. So, when working with Boolean type properties, we must insure we only assign a value of True or a value of False. To make our example form disappear (not a very good thing to do!), we would use the assignment statement:
    frmExample.Visible = False
    This says the current Visible property of the form is replaced by the Boolean value False. We could make it come back with:
    frmExample.Visible = True

    The last property type we need to look at is the string type. Properties of this type are simply what the definition says - strings of characters. A string can be a name, a string of numbers, a sentence, a paragraph, any characters at all. And, many times, a string will contain no characters at all (an empty string). The Caption property is a string type property. We will do lots of work with strings in Visual Basic, so it’s something you should become familiar with. When assigning string type properties, the only trick is to make sure the string is enclosed in quotes (). You may tend to forget this since string type property values are not enclosed in quotes in the properties window. To give our example form a caption, we would use:



    Download 302 Kb.
    1   2   3




    Download 302 Kb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Beginning Visual Basic Project Design, Forms, Command Buttons Review and Preview

    Download 302 Kb.