Using Control Members

By Ken Pugh, ITworld |  How-to

In the previous column, we introduced the control classes. We'll
continue our exploration of these classes this week. Last time, we
created a handler for the WM_INITDIALOG message called OnInitDialog().
Inside of that function, we enabled or disabled the edit control.

You can use any of the CWnd functions on a dialog member that
represents a control. You'll notice in the documentation that there
are many functions for CWnd. They include functions dealing with the
appearance and state of the window, drawing the window, and message-
handling functions. We'll cover CWnd in more detail in a later column.

At any time, there is one window that receives the keystrokes. That
window has "keyboard focus". As a user, you typically change the focus
by using the tab key. You can programmatically change this by using
the SetFocus() function. The function prototype is:

CWnd* CWnd::SetFocus( );

The function returns the window that previously had the focus. For
example, if you want to set the focus to the edit box in the SetWinner
dialog, you can code:

m_winning_value_ctl.SetFocus();

You can change the text in a control by using the SetWindowText()
function. Its prototype is:

BOOL SetWindowText( LPCTSTR p_string );

It returns FALSE if the function fails to set the text. Corresponding
is the GetWindowText() function which looks like:

int GetWindowText( LPTSTR p_string_buffer int max_count) const;
void GetWindowText(CString & string);

Let's set up a test to see if the value in the edit box control are
within a certain range. When the OK button is pushed, a message is
generated for which we'll set up a handler. For the SetWinner dialog
and the IDOK id, set up a handler for the BN_CLICKED message. The
default name is OnOK(). Inside that function, we'll get the value from
the text box. If it is out of range, we'll put up an error message,
set it to a legal value, and set the focus back to the edit box. If we
don't call the default handler (CDialog::OnOK()) for this message, then
the dialog does not end. The code looks like this:

void CSetWinner::OnOK()
{
CString edit_string;
m_winning_value_ctl.GetWindowText(edit_string);
int value;
// Convert to an int
sscanf((const char *) edit_string, "%d", &value);
if (value < 1 || value > 40)
{
MessageBox("Winner outside of limits");
m_winning_value_ctl.SetWindowText("1");
m_winning_value_ctl.SetFocus();
}
else

CDialog::OnOK();
}

If you have complicated inter-field dependencies, you'll probably want
to set up some sort of validation like the above in the OnOK()
function.

If all you need to do is change the value in a control, rather than use
a control functions, you simply use a value member, as we've done
before. Note that the value member can be a string or a primitive such
as a int or a double.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Ask a Question
randomness