+ All Categories
Home > Documents > Chapter 2 ( UID)

Chapter 2 ( UID)

Date post: 06-Apr-2018
Category:
Upload: dheereeye
View: 249 times
Download: 1 times
Share this document with a friend

of 89

Transcript
  • 8/3/2019 Chapter 2 ( UID)

    1/89

    CHAPTER 2: USER INTERFACE DESIGN

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    2/89

    The in-class project for this chapter usesnumerous controls. You will build a projectform that is similar to that shown below. Itwill include the following new controls:

    TextBox

    MaskedTextBox

    GroupBox

    RadioButton CheckBox

    PictureBox

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    3/89

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    4/89

    A GroupBox control is used to group or containother controls. It is used to organize a form intodifferent sections and this can make the formeasier for an application user to use.

    Name property most of the time you will notbother to name a GroupBox control because theyare rarely referred to when you writeprogramming code if you were to name the

    control, a name suchas StudentInformationGroupBox would beappropriate.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    5/89

    Text property displays the words in theupper left corner of the GroupBox control. Ifyou leave the Text property blank, theGroupBox looks like a rectangle.

    The form above has three GroupBox controlswith Text property settingsofStudentInformation, BackGround,and TextColor.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    6/89

    You learned about TextBox and Labelcontrols in an earlier note set. This sectionreviews and extends your understanding ofTextBox and Label controls.

    TextBox controls can be used for:

    Data input this use requires no specialproperty settings.

    Data output display.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    7/89

    Set the ReadOnly property to True and theTextBox looks like a Label.

    You can also use a label to display output,

    but a TextBox a control is easier to use. Set TabStop = False to keep from tabbing to

    a read only TextBox.

    The form in the figure shown above has

    three TextBox controls two of them areaccompanied by two Label controls insideof the Name InformationGroupBox control.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    8/89

    The label controls display the words"Name:" and "Major:" these are prompts totell the system user what type ofinformation is to be typed into the TextBox

    controls. Don't bother naming Label controls you

    will not refer to them later in programmingcode.

    Set the Text property to the Label control tothe prompt to be displayed "Name:" and"Major:".

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    9/89

    The first two TextBox controls are next to thelabels.

    Data entered into a TextBox control is saved tothe Text property of the control.

    Assign the Name property a meaningful nameto each TextBox NameTextBox and MajorTextBox.

    The third TextBox is used to display output and

    has an accompanying label withthe Text property = Output: andthe Name property = OutputLabel.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    10/89

    Name property for= OutputTextBox. ReadOnly property = True. MultiLine property = True the size of the

    TextBox is then stretched to allow for multipleoutput lines.

    Other TextBox control properties include: ForeColor color of the text in the control. BackColor color of the background white is

    the default. TextAlign your options are to display thetext left, right, or center justified. Left is the

    default.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    11/89

    Multiline set to True if the TextBox willdisplay more than one line of text thedefault is False.

    WordWrap this property determines if thecontents of a TextBox should wrap to asecond line (or subsequent lines) if theoutput will not fit on a single line thedefault is True.

    ReadOnly set to True if the TextBox willonly display output the default is False soyou can type text into the TextBox.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    12/89

    The MaskedTextBox control is a specialform of TextBox control with specialproperties to format the data entered byan application user.

    Name property use a name suchas PhoneMaskedTextBox.

    Mask property set to different inputmask values. Click the ellipse button onthe property to open up the InputMask window shown in the figure below.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    13/89Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    14/89

    In the figure above, the Phonenumber mask description is selected thismask, like the Zip code, only allows entry ofnumeric digits. Note the display of theexample mask.

    In the figure showing the form design, theMaskedTextBox control is accompanied by

    a label with a Text value of "Phone:" thatprovides a prompt.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    15/89

    The RichTextBox control is a special form ofTextBox control with special properties toenable applying different character andparagraph formatting, as with wordprocessor software package.

    Name property use a name suchas OutputRichTextBox.

    WordWrap and Multiline properties applyto the RichTextBox just as they do to aregular TextBox.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    16/89

    This figure illustrates use of a RichTextBox todisplay text loaded from a RTF (Rich TextFile) file by use of this coding segment (the

    file is stored in the projects \bin\debugfolder):

    'Display information from Rich TextBoxContents.rtf file to the RichTextBox. RichTextBox1.LoadFile("RichTextBoxContents.rtf")

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    17/89

  • 8/3/2019 Chapter 2 ( UID)

    18/89

    Text property as shown in the figure above. You can name the GroupBox control, but it is

    not necessary as you will not refer to its namewhen writing code.

    Add three Label controls and two TextBoxcontrols to the GroupBox as shown in the figure.

    Name property of the Label controls leave asthe default value.

    Name property of the TextBox controls NameTextBox and MajorTextBox.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    19/89

    Add a MaskedTextBox control to the GroupBoxas shown in the figure.

    Name property PhoneMaskedTextBox. Mask property Phone number Add the output TextBox control and

    accompanying Label control below theGroupBox control as shown in the figure.

    Label property settings: Name property OutputLabel. Text propertyOutput:.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    20/89

    TextBox property settings: Name property OutputTextBox. ReadOnly property True. TabStop property False. MultiLine property True. Save the project and run it. Attempt to

    enter data into the TextBox andMaskedTextBox controls to ensure thecontrols work properly.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    21/89

    RadioButton controls are used to restrict theselection of values from a defined set ofalternatives.

    Name property use a name suchas GrayRadioButton or RedRadioButton.

    Checked property only one RadioButtoncontrol in a group can be the default

    specify this by setting its Checked propertyto True the default is False.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    22/89

    Only one RadioButton in a group can beselected at a time selecting a newRadioButton causes the other RadioButtonsto be unselected.

    Text property displays next to theRadioButton, e.g., Black, Red, Blue, etc. RadioButtons that are to function as a

    group of RadioButtons should be placedinside a GroupBox control. RadioButton

    controls not in a group that are just on aform function as a group, but this keepsyou from creating more than one group.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    23/89

    Format menu use this to align/size thecontrols and set vertical/horizontalspacing. Select the controls by holdingdown CTRL or SHIFT keys and using themouse to click each one in order. The firstcontrol selected is the base control whichall of the other controls will mirror.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    24/89

    Add two GroupBox controls and set thefollowing properties:

    Text property as shown in the figure. Add RadioButton controls inside each

    GroupBox and set their properties: TextGray, Red, Blue, and Green in one

    and Black and White in the other. Checked set to True for the Gray and the

    Black RadioButtons. Name name each RadioButton an

    appropriate name,e.g., GrayRadioButton, RedRadioButton, etc.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    25/89

    Save the project and run it. Select amongthe RadioButton controls. You should findthat when you select one control, thepreviously checked control within the groupis unchecked.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    26/89

    CheckBox controls are similar to RadioButtoncontrols except that they are used whenever thesystem user can select zero, one, or more than oneof the options presented by CheckBoxes.

    Our program only has a single CheckBox control,

    although you can always use as many as arenecessary. Name property use a name such

    as MessageCheckBox. Checked property works like the RadioButton

    stores True or False, depending on whether theCheckBox is checked.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    27/89

    Add a CheckBox control and set theproperties as indicated:

    Name property set to MessageCheckBox. Text property set to Message Visible as

    shown in the figure. Save the project and run it. You should be

    able to check and uncheck the CheckBox,but this doesn't have any effect on the

    program interface yet later you'll writecode to make the CheckBox do something.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    28/89

    The PictureBox control is used to displayimages on a form.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    29/89

    Put the control on the form stretch it tothe size desired notice that the control isempty.

    Name property use a name suchas SnowPictureBox.

    Image property access this property todisplay the Select Resource dialog box asshown in the figure below to add an image

    to the PictureBox any kind of image willgenerally do ico (icon), bitmap, jpg, gif,etc.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    30/89

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    31/89

    SizeMode property set this tothe StretchImage value in order to cause anicon file to fill the entire PictureBox controlspace.

    Visible property set to True to make an imagevisible; False to make an image invisible.

    Imported images are saved to theform's .resx file and becomes a permanent partof the project.

    This code segment demonstrates how to makea PictureBox control appear or disappear bysetting the Visible property with program codeat run time.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    32/89

    'Turn on the sun and turn off the snow SunPictureBox.Visible = True SnowPictureBox.Visible = False

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    33/89

    Add two PictureBox controls to your projectform. Place these side by side and usethe Format menu to size them to identicalsizes. Set the properties as follows:

    Name property SunPictureBox and SnowPictureBox.

    Image property import two images of the sun andsnow to the resource listing.

    SizeMode property set both toa StretchImage value to stretch the image to fill aPictureBox control.

    Visible property setthe SunPictureBox to True (the default), andthe SnowPictureBox to False.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    34/89

    Run your project to confirm that the sundisplays and the snow doesn't later you willwrite program code to make the imagesappear and disappear.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    35/89

    BorderStyle property Labels, TextBox and PictureBoxcontrols all have a BorderStyle property this propertymakes controls appear as either flat or three-dimensional.

    BorderStyle property -- set to an appropriate value toenhance the appearance of a form and add a professionaltouch to a project.

    BorderStyle property values: None flat appearance with no border. FixedSingle a flat appearance with black border. Fixed3D for a TextBox, this looks about like

    FixedSingle. For a Label control, the appearance is athree-dimensional, recessed appearance.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    36/89

    The TextBox control default valuefor BorderStyle is Fixed3D.

    The Label and PictureBox controls defaultvalue for BorderStyle is None.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    37/89

    You may need to modify several controls of the same type, forexample, several Buttons or CheckBoxes or Labels to select andset properties that they have in common such asthe BorderStyle. This will also enable you to align controls on aform more rapidly.

    Methods to select more than one control at a time: Lasso method if the controls are located on a form near to

    each other, simply place the mouse pointer at one corner ofan imaginary box around the controls, hold down the leftmouse button and drag the mouse toward the oppositecorner. You'll notice that a "dashed line" selection box formsaround the controls, and when you release the mouse button,each selected control will be highlighted with selection

    handles like those shown around the buttons in this figure.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    38/89

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    39/89

    Shift and Ctrl (Control) key method holddown either the Shift or Ctrl key and single-click the other controls that you wish toselect. This approach works best when

    selecting controls that are not next to oneanother or controls located inside aGroupBox control.

    Edit menu, Select All option this selects allcontrols on a form.

    To "unselect" a group of controls, click awayfrom the controls someplace on the form.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    40/89

    move all controls selected as a group by dragging themwith the mouse button. set properties of multiple controls that are common

    properties. For example, you can select the threecommand buttons on your in-class exercise form andset the BackColor property to red for all three buttons atthe same time. This is faster than setting them one at atime.

    align and resize controls by using the buttons onthe Layout Toolbar shown in the figure below. If thistoolbar is not visible, selectthe Viewmenu, Toolbars option and selectthe LayoutToolbar. The toolbar buttons are disabled(grayed out) until you select more than onecontrol. Practice with this tool bar now.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    41/89

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    42/89

    The form you design should be easy touse. The more intuitive the form is in termsof system user's understanding how to useit, the less training that will be required to

    teach system users how to be productivewith the computer applications that youprogram. Follow these guidelines.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    43/89

    Use predominantly gray colors to avoidproblems for people who are color blind.

    Use white backgrounds for TextBoxes andgray backgrounds for Labels. The

    background for read-only TextBoxes is alsogray. Using different colors will keepapplication users from trying to type datainto Labels and read-only TextBoxes where

    data cannot be entered, and will make theareas on a form can be used for data entrymore obvious.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    44/89

    Use GroupBoxes to group controls to aidthe system user in organizing howinformation on a form is presented and is tobe used.

    If Labels display messages such as promptsto enter data into an accompanyingTextBox, leave the Labels with a

    flat BorderStyle property.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    45/89

    Use a MS Sans Serif font for mostinformation on a form as this font is easiestfor most people to read.

    Do not use large fonts except for a limitednumber of items.

    Do not use bold or italic fonts except tohighlight select information.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    46/89

    Set these properties of the Form control tomap keyboard keys to Button controls on aform.

    AcceptButton property maps thekeyboard Enter key to the specified buttonon the form makes the Enter key act likeyou've clicked the button with the mouse.

    CancelButton property maps thekeyboard ESC key to a specified button onthe form.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    47/89

    Select the View menu, Tab Order option todisplay the tab order for a form as shown inthis figure.

    Change the order numbering by mouse-clicking on the numbers.

    Tabbing should be left to right, top tobottom.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    48/89

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    49/89

    TabIndex property changing the taborder sets the TabIndex property value (anumeric value) for a control it is the orderof the numbers that actually controls thetabbing during run time.

    TabIndex values start at zero (0) andincrease one unit at a time most controlsare assigned a TabIndex value (PictureBoxcontrols are not).

    A control with TabIndex = 0 hasthe focus on startup of an application. Select the View menu, Tab Order option

    again to turn off the display of the taborder.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    50/89

    TabStop property set this to False if youdo not want a control to be in the tab order.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    51/89

    Define keyboard access keys (hot keys) for buttonsand other controls in case the computer mousequits working also this provides shortcuts favoredby some individuals.

    Access keys are letters in text that are underlined. Type an ampersand (&) just before the intended hot

    key character in the Text property of thebutton. For example, to display Exit, type E&xitthis will make the Alt-X key combination the hotkeys for the Exit button.

    Do not use the same hot key more than once on aform.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    52/89

    StartPosition property setto CenterScreen to display a form centered onthe display at run time.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    53/89

    Tool tips are small labels that displaywhenever an application user pauses themouse pointer over a toolbar button or aform control.

    Each form needs only one ToolTip control. Access the toolbox and add

    a ToolTip control (component) by double-clicking it as shown in this figure.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    54/89

    The ToolTip control displays inthe component tray at the bottom of the IDEbelow the form. The default Name propertyvalue is ToolTip1.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    55/89

    ToolTip on ToolTip1 property: A single ToolTip control on a form causes

    each control on the form acquires aproperty with this name when you add a

    tool tip control to the project. Type the text of the tip to display into

    the ToolTip on ToolTip1 property of acontrol such as a button as shown in this

    figure. At run time, pausing the mouse over a

    control will cause the tool tip to display.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    56/89

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    57/89

    Modify the form to make the appearanceand use of the form more professional.

    Use the Layout Toolbar to align all controlson the form that might not be alignedproperly.

    Set theForm's AcceptButton and CancelButton properties to map the DisplayButton button tothe Enter key and the ResetButton button tothe ESC key.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    58/89

    Select all three buttons at the same time set the BackColor property of all threebuttons to light blue.

    Define hot keys for the three buttons asshown in the above figure.

    Define hot keys for the Name:, Major:, andPhone: Label controls.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    59/89

    Check the tab order of the controls on yourproject. Ensure that they tab from top tobottom, left to right.

    Add a ToolTip control to the project. Addappropriate tip messages for the inputTextBox, MaskedTextBox, and Buttoncontrols.

    Run the project to check that themodifications made display properly duringproject execution.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    60/89

    From your study of Chapter 1 you shouldrecall how to use the Close method for theform (use the Me keyword) to code the ExitButton's click event.

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesExitButton.Click 'Exit the form Me.Close() End Sub

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    61/89

    Clicking the Display Message Button controlshould cause the Output TextBox control todisplay three separate lines of output asshown in this figure. This display is actually

    one long string of text this is termed stringor text concatenation that is the addition ofdifferent strings of text together.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    62/89

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    63/89

    The concatenationoperator is theampersand (&) you can also use the plussymbol (+) the result is to simply add twostrings of text or characters together to

    form a longer, single string. Example of string concatenation:

    "42" & "16" is NOT equal to 58 the correctanswer is the string of characters 4216

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    64/89

    "42" & " " & "16" results in a string of4216 -- Note the blank space that has beenconcatenated (added) to the middle of thestring of characters.

    "42" & ControlChars.NewLine & "16" resultsin output on two lines like this:

    42 16

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    65/89

    The ControlChars.NewLine is a value ofthe ControlChars (control characters)VB enumeration of values used to controlthe output display of.

    Sometimes you will need to display texttyped into a TextBox control to anotherTextBox or label control.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    66/89

    . This is easily accomplished with anassignment statement like the one shownhere. This code would be placed insidea Click event for a button control.

    .OutputTextBox.Text = "MyName:" & NameTextBox.Text

    You may need to display information from

    two or more TextBox controls to a singleTextBox or label control.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    67/89

    Use the concatenation operator (theampersand & symbol) to concatenateinformation (means to add two or morestrings together) from the Text properties

    of the controls and store the singleresulting string to the Text property of thereceiving control.

    This code produces the output shown in theabove figure.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    68/89

    Private Sub DisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesDisplayButton.Click 'Store output message to the output

    TextBox OutputTextBox.Text = "My name: " &NameTextBox.Text & ControlChars.NewLine& "Major: " & MajorTextBox.Text &

    ControlChars.NewLine & "Local phone: " &PhoneMaskedTextBox.Text End Sub

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    69/89

    You may find the above code difficult toread because the code wraps aroundmultiple display lines. You can break thecode up into readable segments by using

    the underscore ( _ ) character as acontinuation character for a line of VBcode. You'll also notice that we indentedthe continued line 4 spaces. This is anormal coding procedure to make the codeeasier to read.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    70/89

    'Store output message to the outputTextBox OutputTextBox.Text = "My name: " _ & NameTextBox.Text _ & ControlChars.NewLine _ & "Major: " _ & MajorTextBox.Text _ & ControlChars.NewLine _ & "Local phone: " _ & PhoneMaskedTextBox.Text

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    71/89

    You can break a line of code any placeexcept in the middle of a string of text thatis within double-quote marks.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    72/89

    The primary event used for RadioButtoncontrols is NOT the Click event it isthe CheckedChanged event.

    CheckedChanged fires whenever aRadioButton's Checked property value changes.

    Each RadioButton control can have itsown CheckedChanged event procedure.

    You will use the CheckedChanged event tomodify the color properties of different

    controls on the form.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    73/89

    Most controls haveboth ForeColor and BackColor properties.

    ForeColor property sets the color of thetext display.

    BackColor property sets the backgroundcolor of a control.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    74/89

    Colors are defined in VB as an enumeration ofvalues an enumeration is a list of predefinedvalues. The enumeration name for colors issimplyColor and is accessed by typing theword Color followed by the dot. Intellisensewill list enumerated color values by theirname.

    Colors are set by use of an assignment

    statement along with the Intellisense popupwindows list of enumerated values as shown inthis figure and the examples below the figure.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    75/89

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    76/89

    CheckBox controls also exhibita CheckedChanged event whentheir Checked property changes value. Thiscode causes the OutputTextBoxcontrol toappear and disappear depending on whether

    the MessageCheckBox control is checked ornot. 'Make the OutputTextBox and OutputLabelinvisible OutputTextBox.Visible =

    MessageCheckBox.Checked OutputLabel.Visible =MessageCheckBox.Checked

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    77/89

    Since boththe Visible and Checked properties ofcontrols are Boolean (bothstore True or False), you can store the valueof a Checked property tothe Visible property of another control andcause it to appear/disappear.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    78/89

    A PictureBox control's Click event is accessedby double-clicking on the control.

    The effect you want is to turn the sun into thesnow, and back into the sun again when one of

    the PictureBox control images is clicked. Thisis coded with the Visible property by settingthe property to True (displays the image)or False (makes the image invisible).

    Code for the Click event sub procedure forthe SunPictureBox control is:

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    79/89

    'Turn the sun into the snow SnowPictureBox.Visible = True SunPictureBox.Visible = False Code for the Click event sub procedure for

    the SnowPictureBox control is: 'Turn the snow into the sun SnowPictureBox.Visible = False SunPictureBox.Visible = True

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    80/89

    The Reset button control is used to resetthe form to its original condition so that itis prepared for data entry for anotherstudent.

    This requires a number of different codingstatements.

    A good way to organize the logic for this

    event is to first type in remarks to outlinethe task requirements as shown in this subprocedure outline.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    81/89

    'Clear the input and output TextBox andMaskedTextBox controls -

    'This code shows use of the Clear method,String.Empty value, and 'empty double quote marks 'Reset the form's BackColor to gray 'and ForeColor to black 'Reset the message display CheckBox

    control to checked 'Turn on the sun and turn off the snow 'Set focus to first TextBox

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    82/89

    Use an assignment statement to clear thecontents of a TextBox, MaskedTextBox, orlabel control.

    Assign a value of the empty string tothe Text property of these controls.

    The empty string is denoted by typing a setof two double-quote marks together withno space between them.

    The empty string is also defined bythe String.Empty defined VB enumeratedvalue. Examples:

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    83/89

    NameTextBox.Text = "" MajorTextBox.Text = String.Empty

    However, the Clear method is used mostoften to clear a TextBox or

    MaskedTextBox. Example: NameTextBox.Clear()

    MajorTextBox.Clear()

    PhoneMaskedTextBox.Clear()

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    84/89

    You can select and unselect these controlsin program code by settingthe Checked property to either True (toselect) or False (to unselect). Examples:

    'Set the black RadioButton to be checked BlackRadioButton.Checked = True

    'This unchecks a RadioButton control notused very often

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    85/89

    BlueRadioButton.Checked = False 'This checks a CheckBox control MessageCheckBox.Checked = True To reset a group of RadioButtons so that one is

    checked and the others are not checked, youonly need to set the Checked propertyto True for one of the controls the others willautomatically be set to False.

    For CheckBox controls, you must set each ofthe control's Checked property to the desiredvalue.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    86/89

    If you wish to set the focus of the cursor toa specific control, such as the first TextBoxon a form, you usethe Focus method. Example:

    NameTextBox.Focus()

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    87/89

    If you need to set several properties for anindividual control, you can usethe With and End With statements to shortenthe coding, and to make your code easier to

    read. Additionally, programs written in thisfashion with these With blocks will execute alittle bit faster than those that do not use thisapproach

    The code shown here will make

    the NameTextBox control visible, set the textcolors for foreground and background, and setthe focus to this control.

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    88/89

    With NameTextBox .Visible = True .ForeColor = Color.Black .BackColor = Color.White .Focus() End With

    Lecturer: Abdirezak Muse Yusuf

  • 8/3/2019 Chapter 2 ( UID)

    89/89

    You'll notice that the code insidethe With block is indented fourspaces. This is normal coding procedure tomake the code easier to read Visual Basic

    will automatically indent the code.

    END


Recommended