Solved

Problems with switch statement using Visual C++

Posted on 2004-03-30
16
719 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now