Problems with switch statement using Visual C++

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;
            }
      }
LVL 1
nasaAsked:
Who is Participating?
 
lakshman_ceConnect With a Mentor Commented:
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
 
lakshman_ceCommented:
Are you assigning any value to  'nSBCode' before entering the switch blocks?

-Lakshman
0
 
lakshman_ceCommented:
Or where is this code written, is it in some message handling like OnHScroll or OnVScroll()?

-Lakshman
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
nasaAuthor Commented:
Thanks for the reply,

And Yes I am using this in OnHScroll.

Nasa
0
 
lakshman_ceCommented:
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
 
nasaAuthor Commented:

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

-Nasa
0
 
lakshman_ceCommented:
Can you plz tell what u r checking here?
pScrollBar->GetDlgCtrlID() == IDC_GovernmentSlider
Is nSBCode belongs to the slider bar control?
0
 
nasaAuthor Commented:
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
 
waysideCommented:
What is pScrollBar->GetDlgCtrlID()  returning?

Are you sure it is equal to either IDC_GovernmentSlider or IDC_ContracterSlider?
0
 
lakshman_ceCommented:
I think if statement should work

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

-Lakshman
     
0
 
nasaAuthor Commented:
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
 
waysideConnect With a Mentor Commented:
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
 
nasaAuthor Commented:
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
 
waysideCommented:
> 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
 
waysideCommented:
>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
 
nasaAuthor Commented:
And there was much rejoicing!!!

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

Nasa
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.