Hi,
Haven't done a list box before so I'm having a bit of a problem. I've set up a listbox in a MDI window using this CONTROL command:
CONTROL(listboxMDI,@LISTBOX|@CTLISTNOTIFY,"Choose User Name",30,50,70,60,0x50800140,99).
I populate it using an ADDSTRING command.
In the handle subroutine for this window I have this code:
SELECT @CLASS
CASE @IDCONTROL
IF @NOTIFYCODE = 0
SELECT @CONTROLID
CASE 0
Username = GETSTRING(listboxMDI,99,0)
CASE 1
Username = GETSTRING(listboxMDI,99,1)
ENDSELECT
ENDIF
ENDSELECT
The listbox display's nicely in the window and contains the expected two list entries. However, Username in the CASE statements above does not get filled when I double-click on an entry. I added PRINT statements in the CASE statements but they don't show up so I'm assuming this code is not getting executed.
Two questions:
What's wrong with the above code?
I'm using a tight loop in my main code while I wait for Username to be filled. Is that what one should do?
Thanks.
that's because you are using
IF @NOTIFYCODE = 0from the help file:
QuoteNotification messages
An list box control sends notification messages to the parent window or dialog in the @NOTIFYCODE variable. The ID of the control is found in @CONTROLID. To receive @LBNDBLCLK or click messages it is necessary to create the control with the @CTLISTNOTIFY style. The following notification messages are defined:
@LBNDBLCLK
The user double-clicks an item in the list box.
@LBNERRSPACE
The list box cannot allocate enough memory to fulfill a request.
@LBNKILLFOCUS
The list box loses the keyboard focus.
@LBNSETFOCUS
The list box receives the keyboard focus.
@LBNSELCHANGE
The selection in a list box is about to change.
@LBNSELCANCEL
The user cancels the selection of an item in the list box.
you should be using
IF @NOTIFYCODE = @LBNDBLCLK which is the same as
IF @NOTIFYCODE = 5QuoteI'm using a tight loop in my main code while I wait for Username to be filled. Is that what one should do?
your code should be structured like the
mdidemo.eba sample file
LarryMc
Thanks Larry. I made an adjustment to the handler code which now looks like this:
SELECT @CLASS
CASE @IDCONTROL
IF @NOTIFYCODE = @LBNDBLCLK <--- Changed this ---------------
SELECT @CONTROLID
CASE 0
Username = GETSTRING(listboxMDI,99,0)
CASE 1
Username = GETSTRING(listboxMDI,99,1)
ENDSELECT
ENDIF
ENDSELECT
Result: still can't get anything in Username. There are 2 entries in the listbox. The listbox ID is 99. The first CASE is testing for zero, which I assumed would be the first string in the listbox, but then I use 0 again in the GETSTRING command. Is this correct? Is something else missing?
Thanks.
'
You trying to select wrong thing insted of position you select control.
Oups i forget GetSelected ::)
try this:
'Listbox
window win
int pos
'Open the window and add a menu and controls.
openwindow win,0,0,600,350,@SIZE|@MINBOX|@MAXBOX,0,"Control Demo",&mainwindow
'menu win,"T,&Project,0,0","I,&Save,0,1","I,&Quit,0,2","T,&Edit,0,0","I,&Cut,0,3"
CONTROL win,@Listbox,"",45,47,100,100,@CTLISTNOTIFY,99
ADDSTRING win,99,"Name1"
ADDSTRING win,99,"Name2"
run=1
waituntil run=0
closewindow win
end
Sub mainwindow
SELECT @CLASS
case @IDCLOSEWINDOW
run=0
CASE @IDCONTROL
IF @NOTIFYCODE = @LBNDBLCLK :'<--- Changed this ---------------
IF @CONTROLID=99
Select pos
CASE 0
pos=GetSelected(win,99)
Username = GETSTRING(win,99,pos)
MESSAGEBOX 0,Username,"OK"
CASE 1
pos=GetSelected(win,99)
Username = GETSTRING(win,99,pos)
MESSAGEBOX 0,Username,"OK"
Endselect
ENDIF
ENDIF
ENDSELECT
ENDSUB
@Aurel
Your code works if, and only if, the window has no other controls.
By testing for the @notifycode before identifying the control that is sending the message the only message that will get through for ANY control is @LBNDBLCLK
Also, there is no need to have a select statement for pos unless you are going to do something different depending on which item is selected.
Here is your code modified to allow other controls to work also:
'Listbox
window win
int pos
'Open the window and add a menu and controls.
openwindow win,0,0,600,350,@SIZE|@MINBOX|@MAXBOX,0,"Control Demo",&mainwindow
'menu win,"T,&Project,0,0","I,&Save,0,1","I,&Quit,0,2","T,&Edit,0,0","I,&Cut,0,3"
CONTROL win,@Listbox,"",45,47,100,100,@CTLISTNOTIFY,99
ADDSTRING win,99,"Name1"
ADDSTRING win,99,"Name2"
run=1
waituntil run=0
closewindow win
end
Sub mainwindow
SELECT @CLASS
case @IDCLOSEWINDOW
run=0
CASE @IDCONTROL
select @CONTROLID
case 99
IF @NOTIFYCODE = @LBNDBLCLK
pos=GetSelected(win,99)
Username = GETSTRING(win,99,pos)
MESSAGEBOX 0,Username,"OK -pos"+str$(pos)
endif
case 443 'dummy placement for one of your other controls
Endselect
ENDSELECT
ENDSUB
LarryMc
Yes Larry you are right...
Im to quick... ::)
Thanks to Larry and AurellCB for your help.
I am now able to return the clicked item in the list box. However, there is one more symptom which is that after the user doubleclicks and control returns to my code outside the handler, I issue a CLOSEWINDOW for the window containing the listbox (I also tried this inside the handler). On screen, the listbox is still there and doesn't go away until I click outside the window.
Is there a better way to do this?
Thanks again.
I can't help you without seeing all your code.
A couple of comments you've made (coupled with the serious structure flaws in the code you have posted) leads me to believe you have other serious flaws.
Quotereturns to my code outside the handler,
QuoteI'm using a tight loop in my main code while I wait for Username to be filled. Is that what one should do?
LarryMc
Do you mean something like this:
Case 99
IF @NOTIFYCODE = @LBNDBLCLK
pos=GetSelected(win,99)
Username = GETSTRING(win,99,pos)
MESSAGEBOX 0,Username,"OK -pos"+str$(pos)
IF Username="Close" THEN run=0
ENDIF