Text Boxes - Elements - Hag GUI for Cobra
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):
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.
Example:
Use username "Joe Bloggs"
And password "swansea"
}
Program (icon:"hag\icons\window.ico")
Uses
hagC2D,
cobra2D,
keyset
Const
USERNAME = "Joe Bloggs"
PASSWORD = "swansea"
Procedure UpdateMyCursor()
Var
lastMouse : Static Integer
lastTip : Static String
Begin
Case hag_mouse of
(HAG_MOUSE_NORMAL):
If lastMouse <> HAG_MOUSE_NORMAL then
ShowMouse()
SpriteVisible(cursor_hand,FALSE)
SpriteVisible(cursor_text,FALSE)
lastMouse = HAG_MOUSE_NORMAL
Endif
(HAG_MOUSE_HAND):
If lastMouse <> HAG_MOUSE_HAND then
HideMouse()
SpriteVisible(cursor_hand,TRUE)
SpriteVisible(cursor_text,FALSE)
lastMouse = HAG_MOUSE_HAND
Endif
PositionSprite(cursor_hand,MouseX,MouseY)
(HAG_MOUSE_TEXT):
If lastMouse <> HAG_MOUSE_TEXT then
HideMouse()
SpriteVisible(cursor_hand,FALSE)
SpriteVisible(cursor_text,TRUE)
lastMouse = HAG_MOUSE_TEXT
Endif
PositionSprite(cursor_text,MouseX,MouseY)
EndCase
{ Tooltip }
If hag_tooltip <> lastTip then
If hag_tooltip <> "" then
CLS(ToRGBA(0,0,0,0),tooltip)
PenColor(ToRGBA(0,0,0),tooltip)
SetFont("arial",8,0,tooltip)
Rect(0,0,TextWidth(hag_tooltip,tooltip)+10,TextHeight(hag_tooltip,tooltip)+4,ToRGBA(255,255,225),TRUE,tooltip)
Rect(0,0,TextWidth(hag_tooltip,tooltip)+10,TextHeight(hag_tooltip,tooltip)+4,ToRGBA(0,0,0),FALSE,tooltip)
SpriteVisible(tooltip,TRUE)
Text(5,2,hag_tooltip,tooltip)
Else
SpriteVisible(tooltip,FALSE)
Endif
lastTip = hag_tooltip
Endif
PositionSprite(tooltip,MouseX+15,MouseY+10)
End
Var
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
Begin
SetAppName("Textbox example")
OpenScreen(320, 110, 32, FALSE, COB_SHOWBORDER+COB_SHOWCLOSE)
Randomize(Millisecs)
background = CreateSprite(320,240)
Cls(ToRGBA(235,233,237),background) ; Flip
// Init GUI
HagBaseDir("hag\")
HagInit(320, 240, 1) // Start at sprite index 1 because of the background sprite
HagLoadGuiTheme("xp")
// 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")
FocusButton(btnCheck)
HagUpdateOnce()
// Mouse
tooltip = CreateSprite(320,20)
SpriteVisible(tooltip,FALSE)
cursor_hand = LoadSprite("img\cursor_hand.png")
SpriteVisible(cursor_hand,FALSE)
cursor_text = LoadSprite("img\cursor_text.png")
SpriteVisible(cursor_text,FALSE)
While ExitRequested = FALSE
HagUpdateAll_AutoKeys()
UpdateMyCursor()
If TextBoxLength(tbxUsername) > 0 then
If btnLolwat.enabled = FALSE then EnableButton(btnLolwat, TRUE)
Else
If btnLolwat.enabled = TRUE then EnableButton(btnLolwat, FALSE)
Endif
If ButtonClicked(btnLolwat) then
For i = 0 to TextBoxLength(tbxUsername)
If i mod 2 = 1 then
TextBoxSetFontColor(tbxUsername, i, i, ToRGBA(0,0,255))
TextBoxSetFontStyle(tbxUsername, i, i, 1)
Else
TextBoxSetFontColor(tbxUsername, i, i, ToRGBA(255,0,0))
TextBoxSetFontStyle(tbxUsername, i, i, 4)
Endif
Next
Endif
If 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!")
Else
MessageBox("Error! That's the wrong username or password!")
Endif
Endif
If ButtonClicked(btnClose) then RequestExit
If KeyHits(VK_ESCAPE) > 0 then RequestExit
Flip
Pause(1)
Wend
HagFreeAll()
CloseScreen()
End
Stay Subscribed
RSS feed or get updates by e-mail.
, subscribe to theYou can also contact me directly - I make an effort to reply to every e-mail.