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?
daknight2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.