CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    [RESOLVED] Another Logic Question: Button Control Only To Fire Once.

    Well I have a Dialog with all kinds of things going on but for this question it regards a single Button Control. I would like the user to be able to use this button only once. It is pretty basic, but using a simple BOOL does not seem to do the trick - or at least how I have tried it.

    My version:
    Code:
    //Reset Button
    case IDC_BTN_RSTR:
    {
         BOOL var = TRUE;
         if(var == FALSE) return TRUE;
         SetDlgItemInt(hwndDlg, IDC_STR, 0, FALSE);
         SetDlgItemInt(hwndDlg, IDC_STR_, 0, FALSE);
         SetDlgItemInt(hwndDlg, IDC_STR__, 0, FALSE);
    
         var = FALSE;
         return TRUE;
    }
    Last edited by Morbane; November 11th, 2010 at 04:54 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Another Logic Question: Button Control Only To Fire Once.

    Quote Originally Posted by Morbane View Post
    My version:
    Code:
    //Reset Button
    case IDC_BTN_RSTR:
    {
         BOOL var = TRUE;
         if(var == FALSE) return TRUE;
    ...
    }
    Well, the line with if(var == FALSE) doesn't make any sense because above your set
    Code:
    BOOL var = TRUE;
    If you set it to TRUE, then it is TRUE, so why check whether it is FALSE????

    Why don't you want instead just to disable button after its first use?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2010
    Posts
    30

    Arrow Re: Another Logic Question: Button Control Only To Fire Once.

    HRRRRrrmmmmmmmmmhhhhhhhhhhhhhh??? Whatever that code is supposed to do, rightnow it just returns TRUE and nothing else! Maybe you meant:

    Code:
    //Reset Button
    case IDC_BTN_RSTR:
    {
         static BOOL var = FALSE;
         if(var == FALSE)
         {
            SetDlgItemInt(hwndDlg, IDC_STR, 0, FALSE);
            SetDlgItemInt(hwndDlg, IDC_STR_, 0, FALSE);
            SetDlgItemInt(hwndDlg, IDC_STR__, 0, FALSE);
            var = TRUE;
            return TRUE;
         }
         else
         {
               return FALSE;
         }
    }
    However this will run once, there's no way to reset the oneshot button. Unles "var" becomes a publicly available variable may that be by declaring it public or some function to reset it again.
    ????????

  4. #4
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: Another Logic Question: Button Control Only To Fire Once.

    ...So returning FALSE will make the button not reuseable? I imagined that was possible but was unsure about how doing so would effect the operation of the dialog.

    I will see what that does.

    Thanks for the help.

    Edit: Great - a simple solution. I think I enjoy posting here too much - I will try to be more diligent before asking such simple questions.
    Last edited by Morbane; November 11th, 2010 at 04:21 PM.

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Another Logic Question: Button Control Only To Fire Once.

    Great - a simple solution.
    Simple, but not great. The button still looks active, but pressing it produces no effect: very, very bad solution. A button intended to be non-functional should be either disabled or hidden:
    Code:
    case IDC_BTN_RSTR:
    {
            SetDlgItemInt(hwndDlg, IDC_STR, 0, FALSE);
            SetDlgItemInt(hwndDlg, IDC_STR_, 0, FALSE);
            SetDlgItemInt(hwndDlg, IDC_STR__, 0, FALSE);
            EnableWindow(GetDlgItem(hDlg, IDC_BTN_RSTR), FALSE);   // disable it
            //ShowWindow(GetDlgItem(hDlg, IDC_BTN_RSTR), SW_HIDE);   // or hide
            return TRUE;
    }
    Best regards,
    Igor

  6. #6
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: [RESOLVED] Another Logic Question: Button Control Only To Fire Once.

    Thanks again Igor!

    [RE-RESOLVED]

  7. #7
    Join Date
    Nov 2010
    Posts
    30

    Re: [RESOLVED] Another Logic Question: Button Control Only To Fire Once.

    Except that it wasn't intended as a solution but as a possible explanation on the behaviour he / she was looking for.

    ^^

    --edit:

    ...So returning FALSE will make the button not reuseable? I imagined that was possible but was unsure about how doing so would effect the operation of the dialog.
    No. Turning it false simply means that YOU will handle the the message and not the default handler:
    Code:
     return DefWindowProc( hwnd, message, wparam, lparam );
    .
    Any message that you do not whish to handle should be defaulted back in that case. It's not realy a matter of enabling nor disabling funcionality.

    In fact it would be better to return FALSE / NULL / 0 in both cases as you handle both cases.
    Last edited by CrashPilot; November 15th, 2010 at 08:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured