This content is provided AS IS for archive purposes only. Content may be out of date with no guarantee of correctness.

Text Boxes - Elements - Hag GUI for Cobra - Static

Main Article

Text Boxes - Elements - Hag GUI for Cobra

Example text boxes using the XP theme.

Textboxes are rectangular elements used for string entry.

Hag text boxes fully support the mouse for selecting text and most keyboard shortcuts including Ctrl+C, Ctrl+X, Ctrl+V, Ctrl+B (bold), Ctrl+I (italic), Ctrl+U (underline), Shift+Left/Right (selection), delete, selection replacement, etc.

All Hag text boxes are rich text (each character can be fully styled):

Example rich-text boxes demonstrating individual styling of letters.

Textboxes can also be used for password entry, displaying asterisks in place of input.

Textbox elements expose the following read-only variables:

textbox.x: Integer - Textbox x position
textbox.y: Integer - Textbox y position
textbox.w: Integer - Textbox width
textbox.h: Integer - Textbox height
textbox.enabled: Boolean - If textbox is enabled
textbox.visible: Boolean - If textbox is visible
textbox.password: Boolean - If textbox is for password entry
textbox.gotUpdated: Boolean - If textbox got updated

For a ready-to-run example, see Example_Textbox or Example2D in the Examples download.



   Use username "Joe Bloggs"
   And password "swansea"


Program (icon:"hag\icons\window.ico")
   USERNAME = "Joe Bloggs"
   PASSWORD = "swansea"

Procedure UpdateMyCursor()
   lastMouse : Static Integer
   lastTip : Static String

hag_mouse of
           If lastMouse <> HAG_MOUSE_NORMAL then
               lastMouse = HAG_MOUSE_NORMAL

           If lastMouse <> HAG_MOUSE_HAND then
               lastMouse = HAG_MOUSE_HAND
           If lastMouse <> HAG_MOUSE_TEXT then
               lastMouse = HAG_MOUSE_TEXT


   { Tooltip }
   If hag_tooltip <> lastTip then
hag_tooltip <> "" then
       lastTip = hag_tooltip


   y: Integer
   frmMain: ^forms
   txtUsername: ^hTexts
   txtPassword: ^hTexts
   tbxUsername: ^textboxes
   tbxPassword: ^textboxes
   btnLolwat: ^buttons
   btnCheck: ^buttons
   btnClose: ^buttons         
   background: Element
   cursor_hand : Element
   cursor_text : Element
   tooltip : Element
   i: Integer
   str1, str2: String


   SetAppName("Textbox example")
   OpenScreen(320, 110, 32, FALSE, COB_SHOWBORDER+COB_SHOWCLOSE)
   background = CreateSprite(320,240)
   Cls(ToRGBA(235,233,237),background) ; Flip

   // Init GUI
   HagInit(320, 240, 1) // Start at sprite index 1 because of the background sprite    
   // Set up GUI Elements
   frmMain = CreateForm()
   txtUsername = CreateText(frmMain, 20, 20, "Username:")
   txtPassword = CreateText(frmMain, 20, 45, "Password:")
   tbxUsername = CreateTextBox(frmMain, 100, 16, 200, "")
   tbxPassword = CreatePasswordBox(frmMain, 100, 41, 200, "")   
   btnLolwat = CreateButton(frmMain, 115, 75, 75, "Color")
   btnCheck = CreateButton(frmMain, 195, 75, 50, "Check")
   btnClose = CreateButton(frmMain, 250, 75, 50, "Close")

   // Mouse
   tooltip = CreateSprite(320,20)
   cursor_hand = LoadSprite("img\cursor_hand.png")
   cursor_text = LoadSprite("img\cursor_text.png")
   While ExitRequested = FALSE
       If TextBoxLength(tbxUsername) > 0 then
btnLolwat.enabled = FALSE then EnableButton(btnLolwat, TRUE)
btnLolwat.enabled = TRUE then EnableButton(btnLolwat, FALSE)
ButtonClicked(btnLolwat) then
i = 0 to TextBoxLength(tbxUsername)
               If i mod 2 = 1 then
                   TextBoxSetFontColor(tbxUsername, i, i, ToRGBA(0,0,255))
                   TextBoxSetFontStyle(tbxUsername, i, i, 1)
                   TextBoxSetFontColor(tbxUsername, i, i, ToRGBA(255,0,0))
                   TextBoxSetFontStyle(tbxUsername, i, i, 4)
ButtonClicked(btnCheck) then
           str1 = TextBoxContents(tbxUsername)
           str2 = TextBoxContents(tbxPassword)
           If (USERNAME = str1) and (PASSWORD = str2) then
               MessageBox("Correct! That's the right username and password!")
               MessageBox("Error! That's the wrong username or password!")
ButtonClicked(btnClose) then RequestExit
       If KeyHits(VK_ESCAPE) > 0 then RequestExit


Stay Subscribed

Follow me on twitter @golightlyb, subscribe to the RSS feed or get updates by e-mail.

You can also contact me directly - I make an effort to reply to every e-mail.