4. textwindow object

Upload: zozzon

Post on 12-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/21/2019 4. TextWindow Object

    1/7

    Page 1TextWindow Object

    15.9.2014 12:22:31tp://msdn.microsoft.com/hr-ba/beginner/hh182552(en-us).aspx

    Beginner Developer Learning CenterSearch MSDN with Bing

    Sign inUnited States - English

    4. TextWindow Object

    Chapter 1:IntroducingSmall Basic

    Chapter 2:Overview ofSmall BasicProgramming

    Chapter 3:ProgramObject

    Chapter 4:TextWindowObject

    Chapter 5:GraphicsWindow Object

    In this chapter, we look at theSmall BasicTextWindowobject.It is used to work with text inputand output.

    TheTextWindowis an objectwhere we can receive text inputand write text output. In SmallBasic, text windows are usuallyused for simple programs with nographic elements. We used thetext window in the examples inChapter 3.

    TextWindow Properties:

    BackgroundColor

    Gets or sets the backgroundcolor of the text to be output inthe text window.

    CursorLeftGets or sets the cursor's columnposition in the text window.

    CursorTopGets or sets the cursor's rowposition in the text window.

    ForegroundColor

    Gets or sets the foreground colorof the text to be output in thetext window.

    This chapter isadapted from thebookThe DevelopersReference Guide ToMicrosoft Small Basicby Philip Conrod andLou Tylee.

    To purchase this bookin its entirety, pleasesee theComputerScience For Kids web

    site.

    LeftGets or sets the left position of text window on yourcomputer screen.

    TitleGets or sets the title for the text window.

    TopGets or sets the top position of text window on yourcomputer screen.

    TextWindowMethods :

    Windows Development Aspiring Professional

    4. TextWindow

    Preview

    TextWindow Object

  • 7/21/2019 4. TextWindow Object

    2/7

    Page 2TextWindow Object

    15.9.2014 12:22:31tp://msdn.microsoft.com/hr-ba/beginner/hh182552(en-us).aspx

    Clear()Clears the text window.

    Hide()Hides the text window.

    Pause()Waits for user input before returning.

    PauseIfVisible()

    Waits for user input only when the text window is open.

    PauseWithoutMessage()Waits for user input before returning (but there is no PressAny Key message).

    Read()Reads a line of text from the text window. This method willnot return until the user pressesEnter. Returns enteredtext.

    ReadKey()Reads a key press from the text window. Returns the

    pressed key.ReadNumber()Reads a number from the text window. This method will notreturn until the user pressesEnter. Returns enterednumber.

    Show()Shows the text window.

    Write(data)Writes text or number (data) to the text window. UnlikeWriteLine, this will not append a new line character, whichmeans, anything written to the text window after this callwill be on the same line.

    WriteLine(data)Writes text or number (data) to the text window. A new linecharacter will be appended to the output, so that the nexttime something is written to the text window, it will go in anew line.

    By default, the text window displays white text on a black

    background:

    The default window has 25 rows (CursorTopvalues from 0to 24) and 80 columns (CursorLeftvalues from 0 to 79)

    To change the background color for the entire window, settheTextWindow.BackgroundColorproperty followed by a

    TextWindow Features

  • 7/21/2019 4. TextWindow Object

    3/7

    Page 3TextWindow Object

    15.9.2014 12:22:31tp://msdn.microsoft.com/hr-ba/beginner/hh182552(en-us).aspx

    Clearmethod. TheForegroundColorproperty sets the textcolor.

    The text window is located on your computer screen asfollows:

    Write a program that writes blue text on a yellowbackground. Write a line of text near the middle of thewindow.

    Small Basic Code:

    1. ' Guide to Small Basic, Example 4-1

    2. TextWindow.Title = "Example 4-1"

    3. TextWindow.BackgroundColor = "Yellow"

    4. TextWindow.ForegroundColor = "Blue"

    5. TextWindow.Clear()

    6. TextWindow.Left = 200

    7. TextWindow.Top = 100

    8. TextWindow.CursorLeft = 30

    9. TextWindow.CursorTop = 10

    10. TextWindow.WriteLine("This is Example 4-1")

    Saved asExample 4-1in Guide to Small Basic\Programs\Chapter 4folder.

    This program illustrates use of all the properties of the

    TextWindowobject. TheTitleproperty is set as areBackgroundColor andForegroundColor. We use theWriteLinemethod (preceded byCursorLeftandCursorTop) to position the text in the window. LeftandTopposition the window on your screen.

    SaveandRun the program to see the results:

    Example 4-1. TextWindow Properties

  • 7/21/2019 4. TextWindow Object

    4/7

    Page 4TextWindow Object

    15.9.2014 12:22:31tp://msdn.microsoft.com/hr-ba/beginner/hh182552(en-us).aspx

    There are three methods used to get user input in the textwindow. TheReadmethod is used to obtain textinformation. The key is pressed to complete theinput. Similarly, theReadNumbermethod is used to obtainnumeric information. WithReadNumber, non-numerickeystrokes are ignored. Lastly, theReadKeymethodobtains individual keystrokes with no need to press

    .

    Write a program where the user first inputs a line of textthen a numeric value. Lastly, intercept keystrokes typed bythe user.

    Small Basic Code:

    1. ' Guide to Small Basic, Example 4-2

    2. TextWindow.Title = "Example 4-2"3. A = TextWindow.Read()

    4. TextWindow.WriteLine(A)

    5. B = TextWindow.ReadNumber()

    6. TextWindow.WriteLine(B)

    7. GetKeystroke:

    8. C = TextWindow.ReadKey()

    9. TextWindow.WriteLine(C)

    10. Goto GetKeystroke

    Saved asExample 4-2in Guide to Small Basic\Programs\Chapter 4folder.

    SaveandRun the program. First, you see a flashing cursorwaiting for you to type a line of text, then press :

    TextWindow Input

    Example 4-2. TextWindow Input

  • 7/21/2019 4. TextWindow Object

    5/7

    Page 5TextWindow Object

    15.9.2014 12:22:31tp://msdn.microsoft.com/hr-ba/beginner/hh182552(en-us).aspx

    Type something in and press . Heres what I seenow:

    You see the line I input and the line written back out withWriteLine. The program is now waiting for a numericinput. Type such an input and press . You will onlybe able to type numeric characters (0, 1, 2, 3, 4, 5, 6, 7, 8, 9,single decimal point, single negative sign to start input). Trynon-numeric characters they wont appear. Heres what Ihave now (my input and its written value):

    Now, the program is waiting for individual keystrokes. Typesome. Notice there is no need to press after thekeystrokes. With each keystroke, the program will write outwhat you typed:

    To stop the program, click theXin the upper right corner ofthe window.

    There are two methods used to write information in the textwindow. We have used theWriteLinemethod. It writesthe information to the window and appends a new line

    character. Any subsequent output methods start on a newline. WriteLinecan also be used to write a blank line using:

    TextWindow.WriteLine("")

    TextWindow Output

  • 7/21/2019 4. TextWindow Object

    6/7

    Page 6TextWindow Object

    15.9.2014 12:22:31tp://msdn.microsoft.com/hr-ba/beginner/hh182552(en-us).aspx

    An alternate toWriteLineis theWritemethod. It writesinformation to the window, but does not append a new linecharacter. Subsequent output methods start at the end ofthe information written by theWritemethod. TheWritemethod is useful for providing prompts prior to aRead,ReadNumberorReadKeymethod.

    Output using bothWriteLineandWritecan be positionedanywhere in the text window by setting theCursorLeftandCursorTopproperties prior to using the output method. By

    default, each output method uses aCursorLeftvalue of 0.CursorTopis incremented by one with eachWriteLinemethod.

    Write a program that rolls a dice until an input number ofsixes is rolled (we showed this code when discussingWhileloops in Chapter 2). Once the rolls are complete, write outhow many rolls it took to achieve the input value.

    Small Basic Code:

    1. ' Guide to Small Basic, Example 4-3

    2. TextWindow.Title = "Example 4-3"

    3. TextWindow.BackgroundColor = "White"

    4. TextWindow.ForegroundColor = "Black"

    5. TextWindow.Clear()

    6. GetNumberSixes:

    7. TextWindow.Write("How many sixes must be rolled? ")

    8. Number = TextWindow.ReadNumber()

    9. Rolls = 0

    10. Counter = 011. While (Counter < Number)

    12. ' Roll a simulated die

    13. Rolls = Rolls + 1

    14. If (Math.GetRandomNumber(6) = 6) Then

    15. Counter = Counter + 1

    16. EndIf

    17. EndWhile

    18. TextWindow.WriteLine("It took "+ Rolls + " rolls to get "+ Number + " sixes.")

    19. TextWindow.WriteLine("")20. Goto GetNumberSixes

    Saved asExample 4-3in Guide to Small Basic\Programs\Chapter 4folder.

    SaveandRun the program. TheWritestatement is used toprompt the user for the needed input (notice no new line isstarted the flashing cursor is to the right of the questionmark):

    Example 4-3. Dice Rolling (TextWindow

  • 7/21/2019 4. TextWindow Object

    7/7

    Page 7TextWindow Object

    I picked 10 and see that it took 53 rolls to achieve 10 sixes:

    Theoretically, it should take 60 rolls (since each roll has a 1in 6 chance of rolling a six). At this point, I can type inanother value. Try several values. Stop the program whenyou are done.

    After completing this chapter, you should understand:

    Use of theTextWindowobject.

    SettingTextWindowproperties.

    Difference betweenRead,ReadNumberandReadKeymethods.

    Difference betweenWriteLineandWritemethods.

    TheTextWindowobject can only accept and output textinformation. Next, we look at theGraphicsWindowobjectwhich allows graphical elements.

    Next Chapter > >

    Copyright 2010 By BibleByte Books. All Rights Reserved. BibleByte

    Books, the BibleByte Books Logo, Computer Science For Kids, the

    Computer Science For Kids logo, and related trade dress are trademarks

    or registered trademarks of BibleByte Books.

    2014 Microsoft. All rights reserved.Terms of Use | Trademarks | PrivacyStatement

    Chapter Review