HI
You can optimize for speed by calculating gadw and width2 after creating the gadget directly(It is needed only once)
Code:
Procedure _AddGadgetItem(gad,Text$)
gadw = GadgetWidth(gad,#PB_Gadget_ActualSize) - GadgetWidth(gad,#PB_Gadget_RequiredSize)
StartDrawing(WindowOutput(0))
DrawingFont(FontID(0))
width = TextWidth(Text$)
width2 = TextWidth(" ")
StopDrawing()
trim = (gadw - width)/2/width2
text$ = Space(trim)+text$
AddGadgetItem(gad, -1,text$)
EndProcedure
LoadFont(0,"Tahoma",12)
If OpenWindow(0, 0, 0, 400, 400, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ComboBoxGadget(0, 10, 10, 250, 24)
SetGadgetFont(0,FontID(0))
_AddGadgetItem(0,"IdeasVacuum")
_AddGadgetItem(0,"ComboBox item #!")
_AddGadgetItem(0,"RASHAD")
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
#2 :
Center text Val. & Hal.
You must take care for if the text length exceeds the gadget width
Code:
Procedure _GetGadgetText(gad)
text$ = LTrim(GetGadgetText(gad)," ")
Debug text$
EndProcedure
Procedure _AddGadgetItem(gad,Text$)
gadw = GadgetWidth(gad,#PB_Gadget_ActualSize); - GadgetWidth(gad,#PB_Gadget_RequiredSize)
StartDrawing(WindowOutput(0))
DrawingFont(FontID(0))
width = TextWidth(Text$)
width2 = TextWidth(" ")
StopDrawing()
trim = (gadw - width)/2/width2
text$ = Space(trim)+text$
AddGadgetItem(gad, -1,text$)
EndProcedure
LoadFont(0,"Tahoma",14)
If OpenWindow(0, 0, 0, 400, 400, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ComboBoxGadget(0, 10, 10, 250, 20)
SetGadgetFont(0,FontID(0))
StartDrawing(WindowOutput(0))
DrawingFont(FontID(0))
height = TextHeight("Q")+10
StopDrawing()
ResizeGadget(0,#PB_Ignore,#PB_Ignore,#PB_Ignore,height)
_AddGadgetItem(0,"IdeasVacuum")
_AddGadgetItem(0,"ComboBox item #!")
_AddGadgetItem(0,"RASHAD")
SetGadgetState(0,1)
ButtonGadget(1,10,370,80,25,"Get Text")
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 1
_GetGadgetText(0)
EndSelect
EndSelect
Until Quit = 1
EndIf