[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 685
  • Last Modified:

SetButtonText() is not working

its a very simple code, but cant make working.
CUserToolBar::CUserToolBar(CWnd* temp)
{
LPCTSTR text = "Add Node";
      Create(temp, WS_CHILD|WS_VISIBLE|CBRS_TOP, IDT_USRTOOLBAR);

SetButtons(NULL,6);
SetButtonInfo(5,IDB_ADDNODE_1,TBBS_BUTTON,NULL);

SetButtonText(5,text);
}
CUserToolBar is derived from CToolBar.
When  i creat an object of CUserToolBar its suppose to be with 6 button and one with text "Add node" but it only creates buttong not setting the text.
Can someone help?
0
daknight2000
Asked:
daknight2000
  • 2
  • 2
1 Solution
 
jhanceCommented:
I didn't try this but I think the problem is that your variable holding the text is gone by the time the CToolBar receives the SetButtonText message.  This is because:

LPCTSTR text = "Add Node";

creates "text" when this function is called and deletes it when it returns.

As a quick test, try:

static LPCTSTR text = "Add Node";

This is probably OK if you only have a few texts to add but will quickly get unmanageable with a bunch of them.  In that case, put pointers to your text in the CUserToolBar class and allocate text for them with new.  Don't forget to delete the allocated data when you are done with them.
0
 
daknight2000Author Commented:
jhance!
didnt help.
I also tried creating only a single button instead of 6, but still cant set the text.
Please help
0
 
mikeblasCommented:

darknight2000, you need to size the toolbar so that it's big enough to show the text you're adding. Here's revised code:

CUserToolBar::CUserToolBar(CWnd* temp)
{
   LPCTSTR text = "Add Node";
   Create(temp, WS_CHILD|WS_VISIBLE|CBRS_TOP, IDT_USRTOOLBAR);

   SetButtons(NULL,6);
   SetButtonInfo(5,IDB_ADDNODE_1,TBBS_BUTTON,NULL);
   SetButtonText(5,text);

   CRect rectBar;
   GetItemRect(0, &rectToolBar);

   // 50 pixels taller is arbitrary, but probably big enough
   rectToolBar.bottom += 50;

   // 16 wide x 15 high is the size of the default image.
   // if you're using your own images, or want something different,
   // you should pick it for the 2nd parameter.
   SetSizes(rectToolBar.Size(), CSize(16, 15));
}


 jhance> LPCTSTR text = "Add Node";
 jhance> creates "text" when this function is called and deletes it when it returns.

No, it doesn't.  You'll find that the compiler puts "Add Node" into the initialized data section of the image. Then, it initializes the pointer text to point at that spot in the initialized data segment. Even after the function is gone, the string remains in the initialized data segment.  Study the aseembler code emitted by this construct to see it in action.

..B ekiM
0
 
daknight2000Author Commented:
Mikeblas!
I have tried ur suggestion but result was really nasty.

here is my ful code.
CUserToolBar::CUserToolBar(CWnd* temp)
{
LPCTSTR text = "Add Node";
Create(temp, WS_CHILD|WS_VISIBLE|CBRS_TOP, IDT_USRTOOLBAR);
SetButtons(NULL,1);
SetButtonInfo(0,IDB_ADDNODE_1,TBBS_BUTTON,NULL);
SetButtonText(0,text);
CRect toolbarrect;
GetItemRect(0,&toolbarrect);
toolbarrect.bottom += 50;
SetSizes(toolbarrect.Size(), CSize(16,15));
}

and the point where i m creating this toolbar object is

CUserMainFrame::OnCreate(LPCREATESTRUCT temp)
{      
new CUserToolBar(this);
return 1;
}

my message map is like this
BEGIN_MESSAGE_MAP(CUserMainFrame, CFrameWnd)
      ON_BN_CLICKED(IDB_ADDNODE_1, OnAddNode)
      ON_WM_CREATE()
END_MESSAGE_MAP()

it produces a button and attaches the OnAddNode() with it, but no text is displayed, although i can see a effect on change of button size.

but if i comment the line
ON_BN_CLICKED(IDB_ADDNODE_1, OnAddNode)
 then two overlapped buttons are displayed.  both are disabled and the upper one contains the text.

I am really sorry to reject the answer. But i dont have alternate choice coz u havent mentioned ur email in ur info, else i was contacting u through that(after accepting the answer). Please dont take it as a "REJECT" i dont mean to reject, i only need ur attention :)




0
 
mikeblasCommented:
> CUserMainFrame::OnCreate(LPCREATESTRUCT temp)
 > {
 > new CUserToolBar(this);
 > return 1;
 > }

This is miserable!  When will you ever delete the CUserToolBar object?  What if creation of the toolbar fails?

The code I posted works fine for me; the only difference is that I dock the bar:

   pBar->EnableDocking(CBRS_ALIGN_ANY);
   DockControlBar(pBar);


 > but if i comment the line
 > ON_BN_CLICKED(IDB_ADDNODE_1, OnAddNode)

By default, MFC disables objects for which there are no command handlers.

 > But i dont have alternate choice coz u havent mentioned ur email in ur info

Of course not. I already answer about three dozen questions each day from direct email. For free. I don't have the time (or the patience, or the motivation) to do any more than that. You've initiated contact with me here, so it's appropriate that you resolve everything here.

..B ekiM
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now