Page 1 of 1

Problems with DCTOOLBAR and DCADDBUTTON

Posted: Thu Sep 21, 2017 12:06 pm
by Viktor
I am new to express ++. I'm testing the xdemo program and I would like to add more buttons to the top toolbar with DCADDBUTTON. Xdemo always displays only the first two buttons, while the other five are not displayed. What am I doing wrong?

Re: Problems with DCTOOLBAR and DCADDBUTTON

Posted: Thu Sep 21, 2017 1:27 pm
by hz_scotty
show some code from the buttons, please :!:

Re: Problems with DCTOOLBAR and DCADDBUTTON

Posted: Thu Sep 21, 2017 10:21 pm
by Viktor
Code is from original folder Express++ >samples >xdemo
program xdemo.prg

// Top Statusbar
DCSTATUSBAR oStatTop HEIGHT 31 ALIGN DCGUI_ALIGN_TOP ;
HELPCODE '.STATUSBAR_TOP' ;
HIDE {||Empty(oToolBar:childList())}

@ 0,0 DCTOOLBAR oToolBar SIZE 1000,31 PIXEL PARENT oStatTop ;
BUTTONSIZE 32,31 HELPCODE '.TOOLBAR_TOP' FANCY ;
TYPE XBPSTATIC_TYPE_TEXT ;
EVAL {|o|o:setColorBG(DC_XbpMenuConfig()[10])}

DCADDBUTTON CAPTION BITMAP_TREE_M PARENT oToolbar ; //original code
ACTION {|o|o:=Thread():new(),o:start({|| TreeMenu()})} ;
TOOLTIP 'Tree-View style menu;Build custom Tool-Bar'

DCADDBUTTON CAPTION '&Open' PARENT oToolBar PIXEL ; //button added in oToolBar
TOOLTIP 'Exit'

DCADDBUTTON CAPTION '&Exit' PARENT oToolBar PIXEL ;
TOOLTIP 'Exit'

What I'm doing wrong?

Re: Problems with DCTOOLBAR and DCADDBUTTON

Posted: Fri Sep 22, 2017 7:31 am
by Cliff Wiernik
Try expanding the oToolBar Size to account for more buttons.

Re: Problems with DCTOOLBAR and DCADDBUTTON

Posted: Wed Sep 27, 2017 5:52 am
by Viktor
Even if I Expanding the oToolBar Size, only two buttons are displayed.

Now I have a new problem. I have the buttons created (DCADDBUTTONXP) with FOR loop and now I need to figure out which button was pressed. Which DCADDBUTTONXP parameter returns, for example, a button caption or button reference? How can I do this?

Re: Problems with DCTOOLBAR and DCADDBUTTON

Posted: Thu Sep 28, 2017 1:07 am
by reganc
Viktor wrote:Even if I Expanding the oToolBar Size, only two buttons are displayed.

Now I have a new problem. I have the buttons created (DCADDBUTTONXP) with FOR loop and now I need to figure out which button was pressed. Which DCADDBUTTONXP parameter returns, for example, a button caption or button reference? How can I do this?
It mentions in the docs, DCADDBUTTONXP uses the same parameters as the @ DCPUSHBUTTONXP command.
So you can add an ID clause to give the button a name and also add an OBJECT clause to put the button object into a variable.
You can then use 'cID := dc_getobjectid(oOBJECT)' to get the ID and use 'cCaption := oOBJECT:caption' to get the caption.
Does that help?

By the way, you will get more help with your other issues if you can post your small test program showing the issues you are having to allow someone else to build it and have a look.

Re: Problems with DCTOOLBAR and DCADDBUTTON

Posted: Thu Sep 28, 2017 2:54 am
by Tom
Hi, Viktor.
How can I do this?
Look's like you're trying to do something like this:

Code: Select all

aButtons := {'Button 1','Button 2',Button 3'}
FOR i := 1 TO Len(aButtons)
  DCADDBUTTON CAPTION aButtons[i] ACTION {||MyFunction(i)} PARENT oButtons
NEXT
You will get 3 buttons with the captions 'Button 1' ... 'Button 3', since the CAPTION clause will be evaluated while the buttons are created. But the ACTION will fail, since this code is evaluated when the button is pressed (!). The variable 'i' is 4 in that situation, not 1, 2 or 3.

There are several ways to solve this. The easiest is this one:

Code: Select all

aButtons := {{'Button 1',{||MyFunction(1)}},;
{'Button 2',{||MyFunction(2)}},;
{'Button 3',{||MyFunction(3)}}}

FOR i := 1 TO Len(aButtons)
  DCADDBUTTON CAPTION aButtons[i,1] ACTION aButtons[i,2] PARENT oButtons
NEXT
This works, but it's not very elegant. This will work aswell:

Code: Select all

[code]aButtons := {'Button 1','Button 2',Button 3'}
FOR i := 1 TO Len(aButtons)
  bButtAction := &("{||MyFunction("+Str(i,1,0)+")}")
  DCADDBUTTON CAPTION aButtons[i] ACTION bButtAction PARENT oButtons
NEXT
[/code]

It's a little more elegant, as long as "MyFunction" is not a STATIC function, but the best way would be to use detached/anchored codeblocks.

Re: Problems with DCTOOLBAR and DCADDBUTTON

Posted: Thu Sep 28, 2017 3:34 am
by Tom
Add: If you just need/evaluate the button caption, you may do this:

Code: Select all

aButtons := {'Button 1','Button 2',Button 3'}
FOR i := 1 TO Len(aButtons)
  DCADDBUTTON CAPTION aButtons[i] ACTION {|a,b,o|MsgBox(o:Caption)} PARENT oButtons
NEXT
The button itself is the third parameter handed to the ACTION codeblock. "Caption" is the iVar holding the button caption.

Re: Problems with DCTOOLBAR and DCADDBUTTON

Posted: Fri Sep 29, 2017 8:23 am
by Viktor
The code works perfectly, especially the third example.
Thank you for your guidance.
Is it possible that when I select this button, I get the caption bask (as in the example described) and at the same time closes the subdialog with the defined buttons? Or is it necessary action with an additional button in the subdialog?
Thanks in advance

Re: Problems with DCTOOLBAR and DCADDBUTTON

Posted: Fri Sep 29, 2017 10:50 am
by Wolfgang Ciriack
A Action block like

Code: Select all

{|a,b,o| retwert:=o:caption, DC_ReadGuiEvent(DCGUI_EXIT_OK,GetList)}
and return the retwert from this function should do this.