May 07, 2024, 10:53:59 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Vertical text Centring in Edit Boxes

Started by GWS, January 27, 2008, 02:15:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

Here's an experiment I've been playing with.

No, it's not possible (as far as I can determine) to vertically center text in an Edit Box.  For some reason Microsoft didn't provide for it.

So using a bit of smoke and mirror stuff, here's what I've come up with:


' GWS 2008 - Creative Basic

' An experiment to give vertically centered text in an 'Edit Box' - something Microsoft doesn't provide.

' The dummy edit boxes are actually flat buttons.  These are used so their position can be found
' when they are clicked, and a real Edit Box is then moved into position to receive the editing.
' When done, the Edit Box is moved out of vision, leaving the text on the Button which automatically centres it.

' Buttons are set for multi-line input when needed (0x2000) - in this case, the address Button.

def w:window

def wW,wH,sW,sH,active:int
def cL,cT,cW,cH:int
def varL,varT,varW,varH:int
def i,j,x,y,border:int

def a$,b$,c$:string

wW = 640
wH = 500

getscreensize sW,sH

window w,0,0,wW,wH,@MINBOX|@MAXBOX,0,"Editing Experiment",main
getclientsize w,cL,cT,cW,cH
border = (wW - cW)/2
setwindowcolor w,rgb(0,20,170)

control w,"E,,-10,-10,10,10,@cteditcenter|@cteditmulti,1"
setcontrolcolor w, 1, 0, rgb(0,100,155)

control w,"B,name,300,100,180,35,@ctlbtnflat,2"
control w,"B,address,300,180,180,80,@ctlbtnflat|0x2000,3"
control w,"B,phone,300,300,180,35,@ctlbtnflat,4"
control w,"B,Exit,(wW-100)/2,400,100,60,0,5"

control w,"T,,150,100,150,40,0,6"
control w,"T,,150,180,150,40,0,7"
control w,"T,,150,300,150,80,0,8"

control w,"T,,(wW-450)/2,20,450,80,@cteditcenter,10"
setcontrolcolor w,10,rgb(100,205,225),rgb(0,20,170)
setfont w, "Arial", 10, 500, 0, 10

a$= "When you click on an Edit Box (actually a button), a real edit box takes over." + chr$(10)
b$= "When the edit is complete and you click away from the box,"  + chr$(10)
c$= "the edit box moves away, leaving the button containing the edited text."
setcontroltext w,10,a$ + b$ + c$

for i = 6 to 8
setcontrolcolor w,i,rgb(0,190,255),rgb(0,20,170)
setfont w, "Arial", 12, 600, 0, i
next i
setcontroltext w,6,"Name"
setcontroltext w,7,"Address"
setcontroltext w,8,"Phone" + chr$(10) + "Number"
centerwindow w

active = 0 :' no editing in progress

run = 1

waituntil run = 0

closewindow w
END


SUB main
select @CLASS
case @IDCLOSEWINDOW
run = 0
case @IDCONTROL
select @CONTROLID
case 1
if (@NOTIFYCODE = @ENKILLFOCUS)
' edit control has lost focus ..
Leave
endif
case 5
run = 0
default
if (active = 0)
active = @CONTROLID
Acquire
endif
endselect
endselect
return

sub Acquire
' move the edit control to the position of the calling button ..
getsize w,varL,varT,varW,varH,active
' we need to allow for border and banner width here to find the position of each control ..
x = varL - (sW - cW)/2
y = varT - (sH - wH)/2 - (wH - cH - border)
a$ = getcontroltext(w,active)
setcontroltext w,1, a$
showwindow w,@swhide,active
' resize the edit control to fit ..
setsize w,x,y,varW,varH,1
controlcmd w, 1, @edsetselection, len(a$), len(a$)
setfocus w,1

return

sub Leave
' Editing done .. leave this control as edited.
setcontroltext w,active,getcontroltext(w,1)
' move the edit control off-screen ..
setsize w,-10,-10,10,10,1
showwindow w,@swrestore,active
active = 0
return



Hope someone will find it of some use ..

best wishes,

Graham
Tomorrow may be too late ..

mrainey

Thanks Graham - definitely a "creative" solution.   :)

I'll work with it and see about weaving it into one of my programs.

I wonder why Microsoft left it out?


                            Mike
Software For Metalworking
http://closetolerancesoftware.com