• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 764
  • Last Modified:

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;
            }
      }
0
nasa
Asked:
nasa
  • 6
  • 6
  • 4
2 Solutions
 
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
 
nasaAuthor Commented:
Thanks for the reply,

And Yes I am using this in OnHScroll.

Nasa
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
lakshman_ceCommented:
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
 
waysideCommented:
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now