Solved

Problems with switch statement using Visual C++

Posted on 2004-03-30
16
745 Views
Last Modified: 2013-11-20
Hi,

I am posting some code involving some switch statements.  For some reason this code doesn't run as expected.  Using the debugger I followed it's execution -- it works as expected up to the switch statement.  At which point it jumps over all the case statments -- including the default statement.  I am at a lose to why...  Any help would be appreciated


      if (pScrollBar->GetDlgCtrlID() == IDC_GovernmentSlider)
      {
            switch (nSBCode)
            {
                  case TB_BOTTOM:
                        break;
                  case TB_ENDTRACK:
                        break;
                  case TB_LINEDOWN:
                        break;
                  case TB_LINEUP:
                        break;
                  case TB_PAGEDOWN:
                        break;
                  case TB_PAGEUP:
                        break;
                  case TB_THUMBPOSITION :
                        m_GovValue = nPos/10;
                        m_GovValCtrl.UpdateData(TRUE);
                        break;
                  case TB_THUMBTRACK :
                        m_GovValue = nPos/10;
                        m_GovValCtrl.UpdateData(TRUE);
                        break;
                  case TB_TOP:
                        break;
                  default :
                        break;
            }
      }
      else if (pScrollBar->GetDlgCtrlID() == IDC_ContractorSlider)

      {
            switch(nSBCode)
            {
                  case TB_BOTTOM:
                        break;
                  case TB_ENDTRACK:
                        break;
                  case TB_LINEDOWN:
                        break;
                  case TB_LINEUP:
                        break;
                  case TB_PAGEDOWN:
                        break;
                  case TB_PAGEUP:
                        break;
                  case TB_THUMBPOSITION:
                        break;
                  case TB_THUMBTRACK:
                        break;
                  case TB_TOP:
                        break;
                  default:
                        break;
            }
      }
0
Comment
Question by:nasa
  • 6
  • 6
  • 4
16 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10716331
Are you assigning any value to  'nSBCode' before entering the switch blocks?

-Lakshman
0
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10716402
Or where is this code written, is it in some message handling like OnHScroll or OnVScroll()?

-Lakshman
0
 
LVL 1

Author Comment

by:nasa
ID: 10716420
Thanks for the reply,

And Yes I am using this in OnHScroll.

Nasa
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10716516
If you are handling the event OnHScroll for the Scroll Bar the switches should have SB_
TB_ is used for the event handling on the slider control.

-Lakshman
0
 
LVL 1

Author Comment

by:nasa
ID: 10716662

I am handling events for a slider bar control, so TB_ is correct (as you note)

-Nasa
0
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10716701
Can you plz tell what u r checking here?
pScrollBar->GetDlgCtrlID() == IDC_GovernmentSlider
Is nSBCode belongs to the slider bar control?
0
 
LVL 1

Author Comment

by:nasa
ID: 10716902
sure,

My dialog has 2 slider controls (one name IDC_GovernmentSlider and the other IDC_ContractorSlider) I am checking to see which one is giving me the HScroll event.  

As far as the nSBCode is concerned -- it doesn't "belong" but it does provided the new position(as the following link talks about)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_slider_notification_messages.asp

nasa
0
 
LVL 14

Expert Comment

by:wayside
ID: 10717168
What is pScrollBar->GetDlgCtrlID()  returning?

Are you sure it is equal to either IDC_GovernmentSlider or IDC_ContracterSlider?
0
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10717184
I think if statement should work

  if( nSBCode == TB_THUMBPOSITION)
                    {
                     m_GovValue = nPos/10;
                    m_GovValCtrl.UpdateData(TRUE);
}

-Lakshman
     
0
 
LVL 1

Author Comment

by:nasa
ID: 10717382
Ok,

tried that with no love... the if statement just reolved to false...

are nSBCode and TB_THUMBPOSITION compatiable (they should be from msdn -- but to make sure....)

Nasa
0
 
LVL 15

Accepted Solution

by:
lakshman_ce earned 100 total points
ID: 10717725
No.The following code works
int x=1;
      
      if(pScrollBar->GetDlgCtrlID() == IDC_SLIDER1)
      {

            switch (nSBCode)
              {
                  case TB_THUMBTRACK:
                        x=2;
                        break;
                  case TB_THUMBPOSITION:
                        x=3;
                        break;
                     default :
                        break;
              }
      }
      else
      if(pScrollBar->GetDlgCtrlID() == IDC_SLIDER2)
      {

            switch (nSBCode)
              {
                  case TB_THUMBTRACK:
                        x=2;
                        break;
                  case TB_THUMBPOSITION:
                        x=3;
                        break;
                     default :
                        break;
              }
      }

Just click the slider on your control and you can see x value changing to 2 and 3 through the debugger..

-Lakshman
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 100 total points
ID: 10717775
What is the value of nSBCode?

Your default: statements have no code, so there would be nothing to step over. That's why you seem to skip over it.

Add some code in there like AfxMessageBox("Shouldn't get here");

andn see what happens.
0
 
LVL 1

Author Comment

by:nasa
ID: 10718056
Cool,

I am not sure how my code was different than  lakshman_ce's -- but ....
Adding the AfxMessageBox caused the switch statement to work as expected.... ,

Now the problem is ....

 m_GovValue = nPos/10;
 m_GovValCtrl.UpdateData(TRUE);

where m_GovValue is a double and m_GovValCtrl is a edit control set to accept doubles -- shouldn't it update the value within the edit box as the slider changes?

Nasa
0
 
LVL 14

Expert Comment

by:wayside
ID: 10718111
> m_GovValCtrl.UpdateData(TRUE);

You can't update just one control using UpdateData(); call

UpdateData(FALSE);

to put all the values back into the dialog. The ones you didn't change will have no visible effect on what you see.





0
 
LVL 14

Expert Comment

by:wayside
ID: 10718137
>Adding the AfxMessageBox caused the switch statement to work as expected.... ,

I would have expected this to make only the default part of it work.

The fact that the whole thing now works tells me you might have had some stringe build problem.

Rule #1 when weird stuff happens in Visual Studio: rebuild the entire project.
0
 
LVL 1

Author Comment

by:nasa
ID: 10718454
And there was much rejoicing!!!

Thanks a lot to both of you for your time and effort

Nasa
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 96
How to know only "File created" with EventLog 3 35
zeroMAx challenge 20 114
Way to decrease size of apk file 9 87
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question