Thread: Dynamically add statis text to a dialog box

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Dynamically add statis text to a dialog box

    I'm tryign to add static text with a value read from a file once a button is pressed, but the text doesn't appear on the dialog window.
    Code:
    void CInitialGUIDlg::OnButton6() 
    {
    	// TODO: Add your control notification handler code here
    /*	CEdit myEditBox;
    	myEditBox.Create();	*/
    
    /*	CEdit* pEdit = new CEdit;
      pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
          CRect(5, 5, 100, 100), this, 1);*/
    
    
    	// TODO: Add extra initialization here
       CCustom MyStatic;
    
    // Create a child static control that centers its text horizontally.
     int i=MyStatic.Create(_T("my static"), WS_CHILD | WS_VISIBLE | SS_NOTIFY,
       CRect(200,200,150,50), this,1);
    
     m_Edit6 = i;
     UpdateData(false);
    
    
    	
    }
    the value of i is returned as 1, so create was successful but there's no text.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    try....

    remove
    UpdateData() //this does not do what you think it does.

    If still not showing up add
    InvalidateRect(hWnd, NULL,FALSE); //tell windows we want to redraw entire dlg
    UpdateWindow(hWnd);//bypass OS que and send directly to callback
    Last edited by novacain; 06-22-2005 at 09:52 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Still nothing happens..

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I think you have to call invalidaterect, for the window to update.

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Quote Originally Posted by Darryl
    I think you have to call invalidaterect, for the window to update.
    I called it like this
    Code:
    InvalidateRect(this,NULL,FALSE);
    But the error comes up:
    Code:
    error C2660: 'InvalidateRect' : function does not take 3 parameters
    Error executing cl.exe.
    That's the description of the function in the MSDN, Oh man.
    With this call:
    Code:
    InvalidateRect(CRect(10,10,150,50),NULL);
    Still nothing happens.......
    Last edited by earth_angel; 06-23-2005 at 09:01 AM.

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Your first attempt didn't work because the locally defined(MFC) invalidaterect was hiding the global invalidaterect, adding scope resolution operator :: to front would fix that (however, your parameters are still wrong, so it still wouldn't had worked)

    Your second attemp, still seems to have some problems with parameters, use true for the second parameter. Also, just to see if it works, use the less efficient:

    InvalidateRect(NULL, TRUE);

    less efficient because it will repaint the whole window instead of just the area where the control is.

    Finally, just making sure... you are putting this after your updatedata call, correct?

    EDIT: I just look at your code, and for the rect, you probably should have used the same one you use to create the control ie. CRect(200,200,150,50) for the static control.
    Last edited by Darryl; 06-23-2005 at 11:11 AM.

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    With all of the adjustments, here's what the button looks like, but doesn't make the text appear:
    Code:
    void CInitialGUIDlg::OnButton2() 
    {
    	// TODO: Add your control notification handler code here
    	
    	CEdit* pEdit = new CEdit;
    	pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
          CRect(325, 100,400, 125), this, 1);
    	
    	CStatic myStatic;
    	
    	// Create a child static control that centers its text horizontally.
    	
    	myStatic.Create(_T("my static"), WS_CHILD|WS_VISIBLE|SS_CENTER,
    		CRect(10,10,150,50), this);
    
    	UpdateData(false);
    
    	InvalidateRect(NULL, TRUE);
    
    
    	//CPoint pText(10,10); 
    }
    Maybe there is something wrong with the red code.

  8. #8
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Ok, I think I know what's going on.... you are creating a local object with:

    CStatic myStatic;

    so at the end of your OnButton2() function, it goes out of scope and is destroyed. Now I am not so familiar with MFC internals, but I would guess that when the object is destroyed its destructor destroys the windows it contains as well (in this case, the static control)

    so what you are going to have to do is "new" the CStatic object (like you did with your cedit object)

    CStatic* myStatic = new CStatic;

    If you will need further access to this class(like deleting it), you will need to find a way to transfer the pointer to your dialog class, otherwise, you can just leave it to be destroyed(deleted) when your app ends... but this may be considered bad practice
    Last edited by Darryl; 06-23-2005 at 01:08 PM.

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    that's exactly what it was. I'll keep that inmind for the rest of the program.
    Hey, if you got a second could you take a look at the this post:
    Dynamically adding method to created controls


    Thanks,
    A.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set Printer Prior To Loading Print Dialog Box
    By Beaner in forum Windows Programming
    Replies: 3
    Last Post: 10-10-2008, 01:02 PM
  2. Obtaining a value from a text box
    By thetinman in forum Windows Programming
    Replies: 1
    Last Post: 11-23-2006, 05:50 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  5. dialog box fonts
    By emu_rob in forum Windows Programming
    Replies: 2
    Last Post: 10-09-2001, 06:05 AM