Reading a combo box

Here you can talk about anything related to BBC BASIC, not covered in another category
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Reading a combo box

Post by KenDown »

In my program I have a combo box with a number of entries. I would like to search for an entry which contains a certain string. This call seems to offer what I need:

https://docs.microsoft.com/en-us/window ... findstring

and according to the Windows Constants utility, CB_FINDSTRING has the value of 332. func$ contains the string for which I am searching (and I have checked and the search string is correct) and the combo box contains an entry with that string in it. (The combo box entry is "FNring(x%,y%,size%,col%,bg%)" and func$ has the value "FNring")

The dialog box has the handle editdlg% and the combo box has the number 439, so this is what I have done:

Code: Select all

SYS"SendMessage",!editdlg%,439,332,-1,func$+CHR$0TOindex%
Unfortunately index% is always zero. If anyone has any ideas for what I am doing wrong, I would be grateful.
DDRM

Re: Reading a combo box

Post by DDRM »

Agreed that looks like the command you want.

I assume your nul-terminated string is correctly constructed...

Just checking that the item you are searching for isn't the first one in the list, since the documentation says the returned value from CB_FINDSTRING is 0-based? And that there isn't a leading space or something (I note it matches only the INITIAL part of the lines, rather than looking for the string anywhere).

Without trying it myself, that's all I've got to suggest - let me know if it helps!

Best wishes,

D
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: Reading a combo box

Post by KenDown »

I presume that by "correctly constructed" you mean that it is actually one of the options (or part of one of the options) in the combo box. Yes.

No, the first item in the combo box is FNring and the one I am looking for it actually "FNangled" which is item 17 in the combo box.

I certainly didn't include any leading spaces in the combo box and there don't appear to be any when I click on it.

So thanks for the reply, but no help. I just wish I could be sure about things like are the parameters in the right order - dialog number, combo box number, message number, -1. All very annoying if they are right.
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

Re: Reading a combo box

Post by svein »

I haven't tested this, but it should work.
You need a handle to the item itself for sendmessage to work.
And you don't need to add CHR$0 after func$, that's done automatically by SYS.

Code: Select all

      CB_FINDSTRING = 332
      CB_ERR = -1
      SYS "GetDlgItem", !editdlg%, 439 TO combo%
      IF combo%=0 THEN ERROR 100,"invalid dialog box handle or a nonexistent control"
      SYS "SendMessage", combo%, CB_FINDSTRING, -1, func$ TO index%
      IF index%=CB_ERR THEN ERROR 100,"string not found"
Svein
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: Reading a combo box

Post by KenDown »

Wow! Brilliant. It works perfectly. Thanks so much.
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: Reading a combo box

Post by KenDown »

My Display program has the facility to call various functions, most of which are held in a separate library and so can be added to by the user. The functions are accessible from a combo-box which is populated from the library at the start of the program, which merely looks for "DEFFN" and adds that line to the box. The problem is forgetting the correct number of parameters or making a mistake in them such as omitting quotes around a string or a comma that separates parameters - we've all done the kind of thing.

The line that was being executed at the time is sl$ and a mistake in the function call will generate an error, some of which are trapped. PROCwarn() displays a dialog box with a title and a message. text% is a previously defined block used for holding text for various purposes.

Code: Select all

      DEFPROCerror:LOCALerr$
      CASEERR OF
        WHEN9:err$="You have omitted a quotation mark somewhere in your function call to"
          err$+=CHR$10+sl$
        WHEN26:err$="There is no such variable - which may be a programming error or "
          err$+=CHR$10+"a missing opening quotation mark. The error was in"
          err$+=CHR$10+sl$
        WHEN31:err$="You do not have the correct number of parameters in the call to"
          err$+=CHR$10+sl$
          IFINSTR(err$,"line")>0err$+=CHR$10+CHR$10+"If you have called FNline you may need FNfancyline."
          IFINSTR(err$,"angle")>0err$+=CHR$10+CHR$10+"If you have called FNangled you may need FNanglet."
        OTHERWISE
          PROCtwarn("Error handler",REPORT$+" at line "+STR$ERL):PROCquit:END
      ENDCASE
      IFINSTR(err$,"|NFN")>0THEN
        i%=INSTR(err$,"|NFN"):j%=INSTR(err$,"(",i%)
        func$=MID$(err$,i%+2,j%-i%-2)
        SYS"GetDlgItem",!editdlg%,439TOcombo%
        SYS"SendMessage",combo%,332,-1,func$TOindex%
        SYS"SendMessage",combo%,328,index%,text%TOlen%
        err$+=CHR$10+"For a call to "+func$+" you need"+CHR$10+$$text%
      ENDIF
      IFerr$>""PROCwarn("Error handler",err$)
      ENDPROC
This provides output such as the following, which I hope is more useful to the user than a bald "Missing parameter". "|N" is the code which tells the Display program that what follows is a function to be executed.

You do not have the correct number of parameters in the call to
|NFNring(200,200)
For a call to FNring you need
FNring(x%,y%,size%,col%,bg%)