List Box Select Only One

If you are converting a text-based Clipper application, a FoxPro application or just writing an application from scratch, use this forum for your eXpress++ questions.
Post Reply
Message
Author
User avatar
alepap
Posts: 94
Joined: Tue Jul 28, 2015 5:15 am

List Box Select Only One

#1 Post by alepap »

I have this code that show Items in a List Box.
You can select one or more Items.
You can select all items and you may Clear all items with 2 buttons.

Now I would like to modify this and force only one Item can be selected.
Like at each selection, it does a clear all prior selection.

Here is the code.

Code: Select all

FUNCTION sLISTBOX(aITEMS)

   LOCAL GetList := {}, aFields:={}, cField:={}
   LOCAL oListBox

   aFields := aITEMS

   @ 0,0 DCLISTBOX cField LIST aFields ;
      FONT "10.Courier" ;
      SIZE 15,10 ;
      OBJECT oListBox 
      // SELECT {2,4,5,6,7}

   @ 0, 17 DCPUSHBUTTON ;
      CAPTION '~Mark All' ;
      SIZE 10, 1.1 ;
      FONT '8.MS Sans Serif' ;
      ACTION {|| AEVAL(aFields, {|x,i| oListBox:setData(i, .T.)}), ;
                 DC_VarFromListBox(oListBox)} ;
      RELATIVE oListBox

   @ 1.5, 17 DCPUSHBUTTON ;
      CAPTION 'C~lear All' ;
      SIZE 10, 1.1 ;
      FONT '8.MS Sans Serif' ;
      ACTION {|| AEVAL(aFields, {|x,i| oListBox:setData(i, .F.)}), ;
                 DC_VarFromListBox(oListBox)} ;
      RELATIVE oListBox

   DCREAD GUI ;
     TITLE 'Select Multiple' ;
     FIT ;
     ADDBUTTONS ;
     MODAL

   // DC_MsgBox(cField)

RETURN cField
I need to remove this

Code: Select all

   @ 0, 17 DCPUSHBUTTON ;
      CAPTION '~Mark All' ;
      SIZE 10, 1.1 ;
      FONT '8.MS Sans Serif' ;
      ACTION {|| AEVAL(aFields, {|x,i| oListBox:setData(i, .T.)}), ;
                 DC_VarFromListBox(oListBox)} ;
      RELATIVE oListBox

   @ 1.5, 17 DCPUSHBUTTON ;
      CAPTION 'C~lear All' ;
      SIZE 10, 1.1 ;
      FONT '8.MS Sans Serif' ;
      ACTION {|| AEVAL(aFields, {|x,i| oListBox:setData(i, .F.)}), ;
                 DC_VarFromListBox(oListBox)} ;
      RELATIVE oListBox
But, I need to do this action every time I click on one Item.
Clear all
Select the Item clicked.

Code: Select all

      ACTION {|| AEVAL(aFields, {|x,i| oListBox:setData(i, .F.)}), ;
                 DC_VarFromListBox(oListBox)} ;
Thanks for your help.

User avatar
rdonnay
Site Admin
Posts: 4340
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: List Box Select Only One

#2 Post by rdonnay »

Compile and run the below code.
I think this is what you want.

Code: Select all

#INCLUDE "dcdialog.CH"

FUNCTION Main()

LOCAL aItems[0], aDir, i

aDir := Directory()

FOR i := 1 TO Len(aDir)
  AAdd( aItems, aDir[i,1] )
NEXT

RETURN sListBox( aItems )

PROC appsys ; RETURN

FUNCTION sLISTBOX(aITEMS)

   LOCAL GetList := {}, aFields:={}, cField:={}
   LOCAL oListBox, bClearAll

   bClearAll := {|| AEVAL(aFields, {|x,i| oListBox:setData(i, .F.)}), ;
                 DC_VarFromListBox(oListBox)}

   aFields := aITEMS

   @ 0,0 DCLISTBOX cField LIST aFields ;
      FONT "10.Courier" ;
      SIZE 25,10 ;
      OBJECT oListBox ;
      MARKMODE XBPLISTBOX_MM_SINGLE

   DCREAD GUI ;
     TITLE 'Select Multiple' ;
     FIT ;
     ADDBUTTONS ;
     MODAL ;

RETURN cField
The eXpress train is coming - and it has more cars.

User avatar
alepap
Posts: 94
Joined: Tue Jul 28, 2015 5:15 am

Re: List Box Select Only One

#3 Post by alepap »

Wonderfull

I added this line to THE LISTBOX.

Code: Select all

MARKMODE XBPLISTBOX_MM_SINGLE
Works perfectly.

Thank you!

User avatar
alepap
Posts: 94
Joined: Tue Jul 28, 2015 5:15 am

Re: List Box Select Only One

#4 Post by alepap »

Here it is

The code should look like this.

Code: Select all

   @ 0,0 DCLISTBOX cField LIST aFields ;
      FONT "10.Courier" ;
      SIZE 15,10 ;
      OBJECT oListBox ;
      MARKMODE XBPLISTBOX_MM_SINGLE
      
      // MARKMODE XBPLISTBOX_MM_SINGLE
      // MARKMODE XBPLISTBOX_MM_MULTIPLE
      // MARKMODE XBPLISTBOX_MM_EXTENDED
      // SELECT {2,4,5,6,7}
By default, it is the Multiple.

SINGLE allow you to click only one
MULTIPLE allow you to click on more than one DEFAULT
EXTENDED allow you to click on more then one, Using Click + Shift Click select many.

Thank you Roger.
You made my day.

Very happy!

Post Reply