Listbox by Anonymous Coder18th May 2005 11:38
|
---|
Summary demonstrate how to make a listbox Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com REM ------------------------------------------------------------------------ REM This program demonstrate, how to making a listbox REM The listbox of items are the available display modes REM Program by Robert Rosta REM Dark Basic V1.13. REM Email: rrosta@freemail.hu REM ------------------------------------------------------------------------ DisplayWidth=640 DisplayHeight=480 DisplayDepth=16 set display mode DisplayWidth,DisplayHeight,DisplayDepth cls sync on Do Inc Counter C$=DisplayListBox(10,10,200,200,200,"Available Display Modes") Cls PCounter=0 Length=Len(C$) IF C$<>"" For I=1 to Length D$=Mid$(C$,I) If D$="0" Or D$="1" Or D$="2" Or D$="3" Or D$="4" Or D$="5" Or D$="6" Or D$="7" Or D$="8" Or D$="9" Then DD$=DD$+D$ If D$="x" Then Inc PCounter If PCounter=1 And D$="x" DisplayWidth=Val(DD$) DD$="" Endif If PCounter=2 And D$="x" DisplayHeight=Val(DD$) DD$="" Endif If PCounter=3 And Length=I DisplayDepth=Val(DD$) DD$="" Endif Next I DD$="" set display mode DisplayWidth,DisplayHeight,DisplayDepth cls Endif Center Text DisplayWidth/2,DisplayHeight/2,"Display Mode:"+C$ Center Text DisplayWidth/2,(DisplayHeight/2)+20,"Select other display modes, or press ESC to Exit.." sync Loop End REM --------------------------------------------------- REM --------- Make a Listbox ------------------------- REM --------------------------------------------------- REM -- No using to long caption$, because no check the length. -- REM --- The R,G,B, values minimum 60, maximum 225 ! -------------- REM -------------------------------------------------------------- Function DisplayListBox(X,Y,R,G,B,Caption$) REM array for list items positions Dim ValueBox(4,1,1) REM detect the available display modes and load in stringarray -Display$- perform checklist for display modes DIM Display$(CheckList Quantity()) ListEnd=Checklist Quantity() for i=1 to checklist quantity() Display$(I)=Checklist string$(i) next i FormHeight=Y+(6 * Text Height(Caption$))+10 FormWidth=X+220 DisplayBoxX=X+10 DisplayBoxY=Y+30 DisplayBoxWidth=DisplayBoxX+110 DisplayBoxHeight=FormHeight-13 REM --- Create Form ----------- Ink RGB(R,G,B),0 Box X,Y,FormWidth,FormHeight RR=R;RG=G;RB=B REM Form Border and header For I=1 to 3 Dec RR,30;Dec RG,30;Dec RB,30 Ink RGB(RR,RG,RB),0 Line X+I,Y+I,FormWidth-I,Y+I Line FormWidth-I,Y+I,FormWidth-I,FormHeight-I Line FormWidth-I,FormHeight-I,X+I,FormHeight-I Line X+I,FormHeight-I,X+I,Y+I Line X+I,Y+20+I,FormWidth-I,Y+20+I Next I Ink RGB(R+30,G+30,B+30),0 Box X+I,Y+I,FormWidth-I,Y+20 Ink RGB(0,0,0),0 Center Text X+(FormWidth-X)/2,Y+I,Caption$ REM Draw border for items area Ink RGB(200,0,0),0 For I=1 to 2 Ink RGB(R-30,G-30,B-30),0 Line DisplayBoxX-I,DisplayBoxY-I,DisplayBoxWidth+I,DisplayBoxY-I Line DisplayBoxWidth+I,DisplayBoxY-I,DisplayBoxWidth+I,DisplayBoxHeight+I Ink RGB(R+30,G+30,B+30),0 Line DisplayBoxWidth+I,DisplayBoxHeight+I,DisplayboxX-I,DisplayBoxHeight+I Line DisplayBoxX-I,DisplayBoxHeight+I,DisplayBoxX-I,DisplayBoxY-I Next I Ink RGB(R,G,B),0 Box DisplayBoxX,DisplayBoxY,DisplayBoxWidth,DisplayBoxHeight ScRBarX=DisplayBoxWidth+2 ScrBarY=DisplayBoxY-2 ScrBarWidth=ScrBarX+17 ScrBarHeight=DisplayBoxHeight+2 For I=1 to 2 Ink RGB(R-30,G-30,B-30),0 Line ScrBarX,ScrBarY,ScrBarWidth,ScrBarY Line ScrBarWidth,ScrBarY,ScrBarWidth,ScrBarHeight Ink RGB(R+30,G+30,B+30),0 Line ScrBarWidth,ScrBarHeight,ScrBarX,ScrBarHeight Line ScrBarX,ScrBarHeight,ScrBarX,ScrBarY Next I REM ------------------ REM -------------- ScrUpButton ------------ REM ------------------------------------------- UpButtonX=ScrBarX+2 UpButtonY=ScrBarY+2 UpButtonWidth=ScrBarWidth-2 UpButtonHeight=UpButtonY+15 Ink RGB(R-30,G-30,B-30),0 Box UpButtonX,UpButtonY,UpButtonWidth,UpButtonHeight DotX=UpButtonX+7:DotY=UpButtonY+5 Ink RGB(R,G,B),0 Line UpButtonX,UpButtonY,UpButtonWidth,UpButtonY Line UpButtonWidth,UpButtonY,UpButtonWidth,UpButtonHeight Ink RGB(R-60,G-60,B-60),0 Line UpButtonWidth,UpButtonHeight,UpButtonX,UpButtonHeight Line UpButtonX,UpButtonHeight,UpButtonX,UpButtonY REM Draw UpArrow into button Ink RGB(0,0,0),0 Counter=0 Repeat Line DotX-Counter,DotY,DotX+Counter,DotY Inc Counter Inc DotY Until Counter=4 REM ------------------------------------------ REM --------------- ScrDownButton --------- REM ------------------------------------------ DownButtonX=ScrBarX+2 DownButtonY=ScrBarHeight-17 DownButtonWidth=ScrBarWidth-2 DownButtonHeight=DownButtonY+15 Ink RGB(R-30,G-30,B-30),0 Box DownButtonX,DownButtonY,DownButtonWidth,DownButtonHeight DotX=DownButtonX+7:DotY=DownButtonY+9 Ink RGB(R,G,B),0 Line DownButtonWidth,DownButtonY,DownButtonWidth,DownButtonHeight Line DownButtonWidth,DownButtonY,DownButtonWidth,DownButtonHeight Ink RGB(R-60,G-60,B-60),0 Line DownButtonWidth,DownButtonHeight,DownButtonX,DownButtonHeight Line DownButtonX,DownButtonHeight,DownButtonX,DownButtonY REM Draw DownArrow into button Ink RGB(0,0,0),0 Counter=0 Repeat Line DotX-Counter,DotY,DotX+Counter,DotY Inc Counter Dec DotY Until Counter=4 REM ------------------------------------------ REM ----------- O.K. And Cancel Button ------- REM ------------------------------------------ REM Cancel button CancelX=ScrBarWidth+7 CanCelY=ScrBarY CancelWidth=CancelX+65 CancelHeight=CancelY+(ScrBarHeight-ScrBarY)/2 INK RGB(R-30,G-30,B-30),0 Box CancelX,CancelY,CancelWidth,CancelHeight For I=1 to 2 Ink RGB(R,G,B),0 Line CancelX+I,CancelY+I,CancelWidth-I,CancelY+I Line CancelWidth-I,CancelY+I,CancelWidth-I,CancelHeight-I INK RGB(R-60,G-60,B-60),0 Line CancelWidth-I,CancelHeight-I,CancelX+I,CancelHeight-I Line CancelX+I,CancelHeight-I,CancelX+I,CancelY+I Next I Ink RGB(0,0,0),0 Center Text CancelX+(CancelWidth-CancelX)/2,CancelY+(CancelHeight-CancelY)/2-Text Height(Caption$)/2,"Cancel" REM Ok Button OkX=CancelX OkY=CancelHeight+2 OkWidth=OkX+65 OkHeight=OkY+(ScrBarHeight-ScrBarY)/2 INK RGB(R-30,G-30,B-30),0 Box OkX,OkY,OkWidth,OkHeight For I=1 to 2 Ink RGB(R,G,B),0 Line OkX+I,OkY+I,OkWidth-I,OkY+I Line OkWidth-I,OkY+I,OkWidth-I,OkHeight-I INK RGB(R-60,G-60,B-60),0 Line OkWidth-I,OkHeight-I,OkX+I,OkHeight-I Line OkX+I,OkHeight-I,OkX+I,OkY+I Next I Ink RGB(0,0,0),0 Center Text OkX+(OkWidth-OkX)/2,OkY+(OkHeight-OkY)/2-Text Height(Caption$)/2," O.k. " REM ------------------------------------------------ REM ---------- Put text into a listbox ------------- REM ------------------------------------------------ Ink RGB(R-50,G-50,B-50),0 TextArea=DisplayBoxheight-DisplayBoxY TextX=DisplayBoxX+5 TextSize=Text Height(Caption$) TextY=DisplayBoxY Pointer=1 Counter=1 Repeat Text TextX,TextY,Display$(Pointer) ValueBox(Pointer,1,0)=TextY:ValueBox(Pointer,0,1)=TextY+TextSize Inc Pointer Inc TextY,TextSize Inc Counter Until Counter=5 Counter=4 Dec Pointer LastY=TextY-(TextSize) Get Image 64970,DisplayBoxX,DisplayBoxY+TextSize,DisplayBoxWidth,DisplayBoxHeight Repeat REM ------------------------------------------------ REM --------------- Handling mouse click ----------- REM ------------------------------------------------ MX=MouseX() MY=MouseY() MC=MouseClick() Ink RGB(200,200,200),RGB(200,0,0) If Mc=1 REM When click on the scrbutton If MX>UpButtonX And MX<UpButtonWidth And MY>UpButtonY And MY<UpButtonHeight Then ClickValue=1 If MX>DownButtonX And MX<DownButtonWidth And MY>DownButtonY And MY<DownButtonHeight Then ClickValue=2 If MX>CancelX And MX<CancelWidth And MY>CancelY And MY<CancelHeight Then ClickValue=3 If MX>OkX And MX<OkWidth And MY>OkY And MY<OkHeight Then ClickValue=4 REM when click on the items If MX>DisplayBoxX And MX<DisplayBoxWidth And MY>ValueBox(1,1,0) And MY<ValueBox(1,0,1) Then ClickValue=5 If MX>DisplayBoxX And MX<DisplayBoxWidth And MY>ValueBox(2,1,0) And MY<ValueBox(2,0,1) Then ClickValue=6 If MX>DisplayBoxX And MX<DisplayBoxWidth And MY>ValueBox(3,1,0) And MY<ValueBox(3,0,1) Then ClickValue=7 If MX>DisplayBoxX And MX<DisplayBoxWidth And MY>ValueBox(4,1,0) And MY<ValueBox(4,0,1) Then ClickValue=8 Endif REM when click on the item in the items area.( ClickValue=5 to 8) REM Repaint the items area - Make box, with RGB values REM Marked the actual items with blue box and print the items REM Setting the - SelectedItem - variable ( apoint the selected item) If ClickValue=5 INK RGB(R,G,B),0 Box DisplayBoxX,DisplayBoxY,DisplayBoxWidth,DisplayBoxHeight Ink RGB(R-50,G-50,B-50),0 II=1 For I=Pointer-3 To Pointer Text TextX,ValueBox(II,1,0),Display$(I) Inc II Next I Ink RGB(0,0,100),0 Box DisplayBoxX,ValueBox(1,1,0)+1,DisplayBoxWidth,ValueBox(1,0,1)-1 Ink RGB(R,G,B),0 Text TextX,ValueBox(1,1,0),Display$(Pointer-3) SelectedItem=Pointer-3 Endif If ClickValue=6 INK RGB(R,G,B),0 Box DisplayBoxX,DisplayBoxY,DisplayBoxWidth,DisplayBoxHeight Ink RGB(R-50,G-50,B-50),0 II=1 For I=Pointer-3 To Pointer Text TextX,ValueBox(II,1,0),Display$(I) Inc II Next I Ink RGB(0,0,100),0 Box DisplayBoxX,ValueBox(2,1,0)+1,DisplayBoxWidth,ValueBox(2,0,1)-1 Ink RGB(R,G,B),0 Text TextX,ValueBox(2,1,0),Display$(Pointer-2) SelectedItem=Pointer-2 Endif If ClickValue=7 INK RGB(R,G,B),0 Box DisplayBoxX,DisplayBoxY,DisplayBoxWidth,DisplayBoxHeight Ink RGB(R-50,G-50,B-50),0 II=1 For I=Pointer-3 To Pointer Text TextX,ValueBox(II,1,0),Display$(I) Inc II Next I Ink RGB(0,0,100),0 Box DisplayBoxX,ValueBox(3,1,0)+1,DisplayBoxWidth,ValueBox(3,0,1)-1 Ink RGB(R,G,B),0 Text TextX,ValueBox(3,1,0),Display$(Pointer-1) SelectedItem=Pointer-1 Endif If ClickValue=8 INK RGB(R,G,B),0 Box DisplayBoxX,DisplayBoxY,DisplayBoxWidth,DisplayBoxHeight Ink RGB(R-50,G-50,B-50),0 II=1 For I=Pointer-3 To Pointer Text TextX,ValueBox(II,1,0),Display$(I) Inc II Next I Ink RGB(0,0,100),0 Box DisplayBoxX,ValueBox(4,1,0)+1,DisplayBoxWidth,ValueBox(4,0,1)-1 Ink RGB(R,G,B),0 Text TextX,ValueBox(4,1,0),Display$(Pointer) SelectedItem=Pointer Endif REM When click the schroll button REM Get the button area REM Imitate pushing button down REM Get the listitems area (not all just 1 to 3) REM Repaint items area REM Paste the image into firs position REM Print the text last position REM Imitate Button up (paste button image) If ClickValue=1 Get Image 64960,UpButtonX,UpButtonY,UpButtonWidth+1,UpButtonHeight+1 Ink RGB(R-60,G-60,B-60),0 Line UpButtonX,UpButtonY,UpButtonWidth,UpButtonY Line UpButtonWidth,UpButtonY,UpButtonWidth,UpButtonHeight Ink RGB(R,G,B),0 Line UpButtonWidth,UpButtonHeight,UpButtonX,UpButtonHeight Line UpButtonX,UpButtonHeight,UpButtonX,UpButtonY REM -- Schroll Up ------ If Pointer<ListEnd Get Image 64970,DisplayBoxX,DisplayBoxY+TextSize,DisplayBoxWidth,DisplayBoxHeight Ink RGB(R,G,B),0 Box DisplayBoxX,DisplayBoxY,DisplayBoxWidth,DisplayBoxHeight Paste Image 64970,DisplayBoxX,DisplayBoxY Inc Pointer Ink RGB(R-50,G-50,B-50),0 Text TextX,LastY,Display$(Pointer) Endif wait 100 Paste Image 64960,UpButtonX,UpButtonY ClickValue=0 Endif REM ---------- DownButtom ------ REM When click the schroll button REM Get the button area REM Imitate pushing button down REM Get the listitems area (not all just 2 to 4) REM Repaint items area REM Paste the image into firs position REM Print the text first position REM Imitate Button up (paste button image) If ClickValue=2 Get Image 64960,DownButtonX,DownButtonY,DownButtonWidth+1,DownButtonHeight+1 Ink RGB(R-60,G-60,B-60),0 Line DownButtonX,DownButtonY,DownButtonWidth,DownButtonY Line DownButtonWidth,DownButtonY,DownButtonWidth,DownButtonHeight Ink RGB(R,G,B),0 Line DownButtonWidth,DownButtonHeight,DownButtonX,DownButtonHeight Line DownButtonX,DownButtonHeight,DownButtonX,DownButtonY REM ------ Schroll Down ------ If Pointer=>5 Get Image 64970,DisplayBoxX,DisplayBoxY,DisplayBoxWidth,DisplayBoxHeight-TextSize Ink RGB(R,G,B),0 Box DisplayBoxX,DisplayBoxY,DisplayBoxWidth,DisplayBoxHeight Paste Image 64970,DisplayBoxX,DisplayBoxY+TextSize Dec Pointer Ink RGB(R-50,G-50,B-50),0 Text TextX,DisplayBoxY,Display$(Pointer-3) Endif wait 100 Paste Image 64960,DownButtonX,DownButtonY ClickValue=0 Endif REM ----- CANCEL BUTTON If ClickValue=3 Get Image 64960,CancelX,CancelY,CancelWidth+1,CancelHeight+1 Ink RGB(R-60,G-60,B-60),0 Line CancelX,CancelY,CancelWidth,CancelY Line CancelWidth,CancelY,CancelWidth,CancelHeight Ink RGB(R,G,B),0 Line CancelWidth,CancelHeight,CancelX,CancelHeight Line CancelX,CancelHeight,CancelX,CancelY Wait 300 Paste Image 64960,CancelX,CancelY wait 200 Endif REM ------ O.K. Button If ClickValue=4 Get Image 64960,OkX,OkY,OkWidth+1,OkHeight+1 Ink RGB(R-60,G-60,B-60),0 Line OkX,OkY,OkWidth,OkY Line OkWidth,OkY,OkWidth,OkHeight Ink RGB(R,G,B),0 Line OkWidth,OkHeight,OkX,OkHeight Line OkX,OkHeight,OkX,OkY Wait 300 Paste Image 64960,OkX,OkY wait 200 Endif sync REM Until click on the "Cancel" Or click on the "O.K." buttons Until ClickValue=4 Or ClickValue=3 If ClickValue=4 Then Value$=Display$(SelectedItem) If ClickValue=3 Then Value$="" UNDIM Display$() ClickValue=0 Endfunction Value$ |