views:

370

answers:

3

Hi,

I'm developing a GUI in C++ using dev-c++.

I have an edit control like this:

hctrl = CreateWindowEx(
                       0,
                       "EDIT",          /* Nombre de la clase */
                       "",              /* Texto del título, no tiene */
                       ES_LEFT | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP |
                       ES_NUMBER , /* Estilo */ 
                       85, 43,          /* Posición */
                       90, 25,         /* Tamaño */
                       hwnd,            /* Ventana padre */
                       (HMENU)ID_TEXTO2, /* Identificador del control */
                       hInstance,       /* Instancia */
                       NULL);           /* Sin datos de creación de ventana */ 
SendMessage(hctrl, WM_SETFONT, (WPARAM)hfont,
MAKELPARAM(TRUE, 0));

I want users to introduce a phone number in this field. It's a compulsory field.

I need that the OK button of this GUI is disabled until the field is correctly fill. It could be possible also that you could push the button but a message was shown saying you have to fill the empty field.

I tried this:

switch (HIWORD(wParam)) {
    case BN_CLICKED:
        switch (LOWORD(wParam)) {
            ...
            ... 
            case ID_BOTON9:
                hctrl = GetDlgItem(hwnd,ID_TEXTO2);     
                len = GetWindowTextLength(GetDlgItem(hwnd,ID_TEXTO2));
                if (len == 0) 
                    MessageBox(hctrl, "Número no válido","Error", MB_ICONEXCLAMATION | MB_OK);
                break;
            ...
        } 
    break; 
}

But this doesn't work.

Can anybody shed any light on it?

Thanks in advance.

A: 

Create a validating function that returns a bool indicating whether input in your window is correct or not. If it returns false, disable the OK button and optionally show a message box or, preferably, trigger a balloon notification on the edit control so the user isn't annoyed by another OK he has to push in order to correct her mistake.

Then you can listen for EN_CHANGE notification coming from the Editbox and validate the input with the above function.

But first, debug your application to make sure the BN_CLICKED event is handled by you properly.

macbirdie
+1  A: 

Use this :

switch (uMsg)

{ 

case WM_COMMAND:       

 {  
  switch (LOWORD(wParam)) 

  { 
   case YourButton:

   { 

    //Check your Number 

    //If the Number validate do what you need to do, if not, show a message and break.

    MessageBox(hwnd,"Your message","Mesage",0);

    break;

   }
anno
A: 

Thanks a lot, I've finally made it work correctly. I don't know what was wrong exactly, because I started again from scratch and I don't think I've made things differently, maybe it was some stupid error.

deb