Here, I have tried to create the basic functionality of aChoice() for a client who is specific on this feature.
This is just another version of ComboSearchBox, thanks to Esgici.
Initial version
Code: Select all
#include <hmg.ch>
Function Main
private aCountries := HB_ATOKENS( MEMOREAD( "Countries.lst" ), CRLF )
public aVar := 1
set navigation extended
define window sample at 0,0 width 800 height 600 main
define label label1
row 10
col 10
width 100
value "Name"
end label
define textbox textbox1
row 10
col 110
width 100
end textbox
define label label2
row 40
col 10
width 100
value "Country"
end label
define textbox textbox2
row 40
col 110
width 100
on enter doachoice()
end textbox
define label label3
row 70
col 10
width 100
value "City"
end label
define textbox textbox3
row 70
col 110
width 100
end textbox
end window
sample.center
sample.activate
Return
function doachoice
local nTop := 10
local nLeft := 300
local nBottom := 900
local nRight := 800
local nDefault := 1
local nSelected := 0
if len(alltrim(this.value)) > 0
nDefault := ascan(aCountries,this.value)
endif
nSelected := HMG_AChoice( nTop, nLeft, , , aCountries, nDefault)
if nSelected > 0
this.value := aCountries[nSelected]
else
this.value := ""
endif
return nil
function HMG_Achoice(nTop,nLeft,nBottom,nRight,aList,nDefault,lAnyWhere)
local nRow := thiswindow.row
local nCol := thiswindow.col
local nWindowWidth := thiswindow.width
local nWindowHeight := thiswindow.height
local nWidth := 0
local nHeight := 0
local i := 0
private _aItems := aclone(aList)
private _nSelected := 0
private lAnyWhereSearch := .f.
default lAnyWhere := .f.
default nDefault := 0
default nBottom := thiswindow.height - 10
default nRight := thiswindow.width - 10
lAnyWhereSearch := lAnyWhere
nWidth := iif(nRight < nWindowWidth, nRight - nLeft,nWindowWidth - nLeft - 10)
nHeight := iif(nBottom < nWindowHeight, nBottom - nTop,nWindowHeight - nTop - 10)
if iswindowdefined(_HMG_aChoice)
release window _HMG_aChoice
endif
DEFINE WINDOW _HMG_aChoice AT nRow+nTop, nCol+nLeft ;
WIDTH nWidth ;
HEIGHT nHeight ;
TITLE '' ;
MODAL ;
NOCAPTION;
NOSIZE
define textbox _edit
row 5
col 5
width nWidth - 10
ON CHANGE _aChoiceTextChanged( lAnyWhere )
ON ENTER _aChoiceSelected( )
end textbox
define listbox _list
row 30
col 5
width nWidth - 10
height nHeight - 50
items aList
on change _achoicelistchanged()
ON DBLCLICK _aChoiceSelected( )
end listbox
end window
ON KEY UP OF _HMG_achoice ACTION _aChoiceDoUpKey()
ON KEY DOWN OF _HMG_achoice ACTION _aChoiceDoDownKey()
ON KEY ESCAPE OF _HMG_achoice ACTION _aChoiceDoEscKey()
_hmg_sysdata[296] := {||_aChoiceselected()}
if len(_aItems) > 0
if nDefault > 0
_HMG_aChoice._list.value := nDefault
_HMG_aChoice._edit.value := _aItems[nDefault]
endif
endif
_HMG_Achoice.activate
return _nSelected
STATIC PROC _aChoiceTextChanged( lAnyWhere )
LOCAL cCurValue := _HMG_aChoice._edit.value
LOCAL nItemNo := 0
local lFound := .f.
FOR nItemNo := 1 TO LEN(_aitems)
IF lAnyWhere
IF AT(UPPER(cCurValue),UPPER(_aItems[nItemNo])) > 0
_HMG_aChoice._list.value := nItemNo
lFound := .t.
exit
ENDIF
ELSE
IF UPPER( LEFT( _aitems[ nItemNo ], LEN(cCurValue))) == UPPER(cCurValue)
_HMG_aChoice._list.value := nItemNo
lFound := .t.
exit
ENDIF
ENDIF
NEXT nItemNo
if .not. lFound
_HMG_aChoice._list.value := 0
endif
RETURN
function _aChoiceselected
if _HMG_aChoice._List.value > 0
_nSelected := _HMG_aChoice._list.value
else
_nSelected := 0
endif
release window _HMG_aChoice
return nil
function _aChoiceDoUpKey()
IF _HMG_aChoice._List.value > 1
_HMG_aChoice._List.value := _HMG_aChoice._List.value - 1
_HMG_aChoice._edit.value := _HMG_aChoice._List.item(_HMG_aChoice._List.value)
textboxeditsetsel("_HMG_aChoice","_Edit",0,-1)
ENDIF
return nil
function _aChoiceDoDownKey()
IF _HMG_aChoice._List.value < _HMG_aChoice._List.ItemCount
_HMG_aChoice._List.value := _HMG_aChoice._List.value + 1
_HMG_aChoice._edit.value := _HMG_aChoice._List.item(_HMG_aChoice._List.value)
textboxeditsetsel("_HMG_aChoice","_Edit",0,-1)
ENDIF
return nil
function _aChoiceDoEscKey()
_nSelected := 0
release window _HMG_aChoice
return nil
function _achoicelistchanged
if upper(this.name) == "_EDIT" .and. upper(thiswindow.name) == "_HMG_ACHOICE" .and. .not. lAnyWhereSearch
_HMG_aChoice._edit.value := _HMG_aChoice._List.item(_HMG_aChoice._List.value)
textboxeditsetsel("_HMG_aChoice","_Edit",0,-1)
endif
return nil
function textboxeditsetsel(cParent,cControl,nStart,nEnd)
local nHandle := GetControlHandle (cControl,cParent)
textboxsetsel(nHandle,nStart,nEnd)
return nil
#pragma BEGINDUMP
#include <windows.h>
#include <commctrl.h>
#include "hbapi.h"
#include <wingdi.h>
HB_FUNC ( TEXTBOXSETSEL )
{
HWND hWnd1;
hWnd1 = (HWND) hb_parnl (1);
SendMessage((HWND) hWnd1,EM_SETSEL, (WPARAM)(int) hb_parni(2),(LPARAM) (int) hb_parni(3));
}
#pragma ENDDUMP
Code: Select all
#include <hmg.ch>
Function Main
private aCountries := HB_ATOKENS( MEMOREAD( "Countries.lst" ), CRLF )
public aVar := 1
set navigation extended
define window sample at 0,0 width 800 height 600 main
define label label1
row 10
col 10
width 100
value "Name"
end label
define textbox textbox1
row 10
col 110
width 100
end textbox
define label label2
row 40
col 10
width 100
value "Country"
end label
define textbox textbox2
row 40
col 110
width 100
on gotfocus HMG_AChoice( 10, 300, , , aCountries,,.f.)
end textbox
define label label3
row 70
col 10
width 100
value "City"
end label
define textbox textbox3
row 70
col 110
width 100
end textbox
end window
sample.center
sample.activate
Return
function HMG_Achoice(nTop,nLeft,nBottom,nRight,aList,nDefault,lAnyWhere)
local nRow := thiswindow.row
local nCol := thiswindow.col
local nWindowWidth := thiswindow.width
local nWindowHeight := thiswindow.height
local nWidth := 0
local nHeight := 0
local nFound := 0
local i := 0
local cInit := this.value
private _aItems := aclone(aList)
private _nSelected := 0
private lAnyWhereSearch := .f.
private cParentForm := thiswindow.name
private cThisControl := this.name
default lAnyWhere := .f.
default nDefault := 0
default nBottom := thiswindow.height - 10
default nRight := thiswindow.width - 10
lAnyWhereSearch := lAnyWhere
nWidth := iif(nRight < nWindowWidth, nRight - nLeft,nWindowWidth - nLeft - 10)
nHeight := iif(nBottom < nWindowHeight, nBottom - nTop,nWindowHeight - nTop - 10)
if .not. iswindowdefined(_HMG_AChoice)
DEFINE WINDOW _HMG_aChoice AT nRow+nTop, nCol+nLeft ;
WIDTH nWidth ;
HEIGHT nHeight ;
TITLE '' ;
MODAL ;
NOCAPTION;
NOSIZE
define textbox _edit
row 5
col 5
width nWidth - 10
ON CHANGE _aChoiceTextChanged( lAnyWhere )
ON ENTER _aChoiceSelected( )
end textbox
define listbox _list
row 30
col 5
width nWidth - 10
height nHeight - 50
items aList
on change _achoicelistchanged()
ON DBLCLICK _aChoiceSelected( )
end listbox
end window
ON KEY UP OF _HMG_achoice ACTION _aChoiceDoUpKey()
ON KEY DOWN OF _HMG_achoice ACTION _aChoiceDoDownKey()
ON KEY ESCAPE OF _HMG_achoice ACTION _aChoiceDoEscKey()
_hmg_sysdata[296] := {||_aChoiceselected()}
if len(_aItems) > 0
if nDefault > 0
_HMG_aChoice._list.value := nDefault
_HMG_aChoice._edit.value := _aItems[nDefault]
else
if len(alltrim(cInit)) > 0
nFound := ascan(_aItems,cInit)
if nFound > 0
_HMG_aChoice._list.value := nFound
_HMG_aChoice._edit.value := cInit
endif
endif
endif
endif
_HMG_Achoice.activate
_hmg_sysdata[296] := nil
inserttab()
else
_hmg_sysdata[296] := nil
inserttab()
endif
return nil
STATIC PROC _aChoiceTextChanged( lAnyWhere )
LOCAL cCurValue := _HMG_aChoice._edit.value
LOCAL nItemNo := 0
local lFound := .f.
FOR nItemNo := 1 TO LEN(_aitems)
IF lAnyWhere
IF AT(UPPER(cCurValue),UPPER(_aItems[nItemNo])) > 0
_HMG_aChoice._list.value := nItemNo
lFound := .t.
exit
ENDIF
ELSE
IF UPPER( LEFT( _aitems[ nItemNo ], LEN(cCurValue))) == UPPER(cCurValue)
_HMG_aChoice._list.value := nItemNo
lFound := .t.
exit
ENDIF
ENDIF
NEXT nItemNo
if .not. lFound
_HMG_aChoice._list.value := 0
endif
RETURN
function _aChoiceselected
if _HMG_aChoice._List.value > 0
setproperty(cParentForm,cThisControl,"VALUE",_HMG_aChoice._List.item(_HMG_aChoice._List.value))
else
setproperty(cParentForm,cThisControl,"VALUE","")
endif
release window _HMG_aChoice
return nil
function _aChoiceDoUpKey()
IF _HMG_aChoice._List.value > 1
_HMG_aChoice._List.value := _HMG_aChoice._List.value - 1
_HMG_aChoice._edit.value := _HMG_aChoice._List.item(_HMG_aChoice._List.value)
textboxeditsetsel("_HMG_aChoice","_Edit",0,-1)
ENDIF
return nil
function _aChoiceDoDownKey()
IF _HMG_aChoice._List.value < _HMG_aChoice._List.ItemCount
_HMG_aChoice._List.value := _HMG_aChoice._List.value + 1
_HMG_aChoice._edit.value := _HMG_aChoice._List.item(_HMG_aChoice._List.value)
textboxeditsetsel("_HMG_aChoice","_Edit",0,-1)
ENDIF
return nil
function _aChoiceDoEscKey()
setproperty(cParentForm,cThisControl,"VALUE","")
release window _HMG_aChoice
return nil
function _achoicelistchanged
if upper(this.name) == "_EDIT" .and. upper(thiswindow.name) == "_HMG_ACHOICE" .and. .not. lAnyWhereSearch
_HMG_aChoice._edit.value := _HMG_aChoice._List.item(_HMG_aChoice._List.value)
textboxeditsetsel("_HMG_aChoice","_Edit",0,-1)
endif
return nil
function textboxeditsetsel(cParent,cControl,nStart,nEnd)
local nHandle := GetControlHandle (cControl,cParent)
textboxsetsel(nHandle,nStart,nEnd)
return nil
#pragma BEGINDUMP
#include <windows.h>
#include <commctrl.h>
#include "hbapi.h"
#include <wingdi.h>
HB_FUNC ( TEXTBOXSETSEL )
{
HWND hWnd1;
hWnd1 = (HWND) hb_parnl (1);
SendMessage((HWND) hWnd1,EM_SETSEL, (WPARAM)(int) hb_parni(2),(LPARAM) (int) hb_parni(3));
}
#pragma ENDDUMP
Please give your comments and suggestions.