Updating static text in dialog

Corey080698
Corey080698 used Ask the Experts™
on
Hey All,

okay, i'm new to all this and am trying to find a way to do a countdown (30 down to 0) in a simple dialog window.  How can I update the static text that I'm using in Visual C++?

Cheers,
Corey
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hey Corey,

Try using:

SetDlgItemInt (IDC_Count, count);

where "IDC_Count" is the ID to your static text and "count" is an int variable you define elsewhere.  You can find this out by right clicking the the static text box in your dialog resources and clicking properties.

Now you just need to update the variable count with using something like OnTimer() and reseting the static text every time with SetDlgItemInt.

Hope that helps, let me know if you need more explanation.

Rick

Commented:
Hi,

You can also use this code when ever you want the text to be updated:
(
.
.
static int x=30; //or you can define it as a member of
                 //your dlg class
CString temp;
temp.Format("%d",x);
CWnd* pWnd = GetDlgItem(IDC_STATIC);  // passing the ID of
                                      // the static text
pWnd->SetWindowText(temp);
x--;
.
.
}


Regards,
  Mazen

Commented:
The following:

>>CWnd* pWnd = GetDlgItem(IDC_STATIC);  

will not work since IDC_STATIC is not unique in a dialog.  ALL static controls get the ID IDC_STATIC by default.

You need to open your dialog in the resource editor and change the ID for this static control to something other than IDC_STATIC.  Then you can use methods as indicated above to change its text.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

CWnd* pWnd = GetDlgItem(IDC_STATIC) will work if you only happen to have one static control, but it's not very tidy or robust...
The above code all are good, but I recommend you Using ClassWizard, if you can use ClassWizzard well, you can developping Visual C Applization faster.

In Visual C(When developping You Application), rightclick on text bar you want to count from 30 to 1, and click "Variable" Tab,and Click Add, Then write a VariableName...This Variable Name is the reference to your control(in this case is Static Text Control)...something like TestStatic

In Your Code, everywhere you want to Set the Static Control Text you can write TestStatic.SetWindowText("1") or some thing like this
Commented:
I also recommend that you use the ClassWizard.  First you need to make sure that your Static Text item has a unique name, such as "IDC_COUNT".

Second, you need a class for your dialog.  If you don't already have one, double-clicking on the dialog (from the dialog editor) will open up the Class Wizard and ask you if you want to create a new class...

Once you've created your new class, with the ClassWizard open and the new class selected (in the class name combo box) click on the second tab, labelled "Member Variables".
You should find your Static Text control listed there as "IDC_COUNT".  Double click on it.  You will be prompted to create a variable.  

You have two choices.  You can create a control variable, which gives you a handle to the control, or you can create a value variable, which is a string that stores the value that you want displayed in your static text control.

The easier option is to create a value variable.  You can name it something like "m_sCount".  The ClassWizard will create this variable in your class and initialize it in your constructor for you.  

Everytime you want to change the value, you can treat it as a string so you would do the following:
m_sCount= "30";
UpdateData(FALSE);
The UpdateData function takes the string value from the variable and puts it in the control.  The FALSE parameter indicates the direction of exchange.  If you wanted to get the value from the dialog and place it in your variable, the parameter would be TRUE instead.

Author

Commented:
Hey all...thanks for the replies (i've not forgoten about you).  Please keep them coming!  I've been a little bogged down with school I've not had a change to put a lot of effort into trying your suggestions.  I just wanted to let you know I appreciate the feedback and will accept an answer ASAP!  Thanks for your patients!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial