switch statement 
Author Message
 switch statement

How can I use a switch statement to open a certain dialog box:
I have numerous dialogs that I need to allow the user to open based on a
selection from a combination of a scroll bar position and a button. When the
user presses a button it gets the position of a scroll bar which then
decides which dialog to open.

void CDetInfoDlg::OnBtnSelectsitu()
{
 file://get current scroll position
 int nPos = m_cScrollCtrl.GetScrollPos();
 int rc;
 switch(nPos){
  case 1:
    CAngRipDlg dlg; // **************cannot be declared
here!!!!******************
    ShowWindow(SW_HIDE);
    rc = dlg.DoModal();
    if(rc == IDOK || rc == IDCANCEL){
    ShowWindow(SW_SHOW);
   }
   break;
  case 2:
   break;
  }

Quote:
}

Problem is: If you look at the above situation, the CAngRipDlg dlg; cannot
be declared there because the compiler complains that "initialization of dlg
is skipped by case label".

I need each case to open a different dialog. I just don't show anymore for
clarity!
All in all, there are about 10 different cases, so if someone knows of a
better way without using a switch statement that would be ok as well.

Any help on this would be greatly appreciated!
Thanks for the time!
Mike



Sat, 16 Aug 2003 04:41:09 GMT  
 switch statement
Try using { } around your case statments.

case 1:
    {
    CAngRipDlg dlg;
    ShowWindow(SW_HIDE);
    rc = dlg.DoModal();
    if(rc == IDOK || rc == IDCANCEL){
        ShowWindow(SW_SHOW);
    }

Ali


Quote:
> How can I use a switch statement to open a certain dialog box:
> I have numerous dialogs that I need to allow the user to open based on a
> selection from a combination of a scroll bar position and a button. When
the
> user presses a button it gets the position of a scroll bar which then
> decides which dialog to open.

> void CDetInfoDlg::OnBtnSelectsitu()
> {
>  file://get current scroll position
>  int nPos = m_cScrollCtrl.GetScrollPos();
>  int rc;
>  switch(nPos){
>   case 1:
>     CAngRipDlg dlg; // **************cannot be declared
> here!!!!******************
>     ShowWindow(SW_HIDE);
>     rc = dlg.DoModal();
>     if(rc == IDOK || rc == IDCANCEL){
>     ShowWindow(SW_SHOW);
>    }
>    break;
>   case 2:
>    break;
>   }
> }

> Problem is: If you look at the above situation, the CAngRipDlg dlg; cannot
> be declared there because the compiler complains that "initialization of
dlg
> is skipped by case label".

> I need each case to open a different dialog. I just don't show anymore for
> clarity!
> All in all, there are about 10 different cases, so if someone knows of a
> better way without using a switch statement that would be ok as well.

> Any help on this would be greatly appreciated!
> Thanks for the time!
> Mike



Sat, 16 Aug 2003 06:17:48 GMT  
 switch statement

says...

Quote:
> How can I use a switch statement to open a certain dialog box:
> I have numerous dialogs that I need to allow the user to open based on a
> selection from a combination of a scroll bar position and a button. When the
> user presses a button it gets the position of a scroll bar which then
> decides which dialog to open.

First of all, I feel obliged to warn you that this sounds like
something that could get you nominated for one of the (many) web
pages devoted to how terrible user interfaces can get.

Quote:
> Problem is: If you look at the above situation, the CAngRipDlg dlg; cannot
> be declared there because the compiler complains that "initialization of dlg
> is skipped by case label".

This is much easier to deal with -- the most obvious way would be to
enclose the code for each case in braces.

Quote:
> I need each case to open a different dialog. I just don't show anymore for
> clarity!  All in all, there are about 10 different cases, so if someone knows
> of a better way without using a switch statement that would be ok as well.

How about a single dialog that's split: list the 10 categories of
"stuff" along the left side, and then have different controls show up
on the right side in response to selecting from the list on the left.

--
    Later,
    Jerry.

The Universe is a figment of its own imagination.



Sat, 16 Aug 2003 08:39:14 GMT  
 switch statement
Try this
void CDetInfoDlg::OnBtnSelectsitu()
{
 //get current scroll position
 int nPos = m_cScrollCtrl.GetScrollPos();
 int rc;
CDialog *pdlg;
 switch(nPos){
  case 1:
    pdlg = (CDialog*)new CAngRipDlg();
    ShowWindow(SW_HIDE);
    rc = pdlg->DoModal();
    if(rc == IDOK || rc == IDCANCEL){
    ShowWindow(SW_SHOW);
      }
   break;
  case 2:
   break;
  }

Quote:
}



Quote:
> How can I use a switch statement to open a certain dialog box:
> I have numerous dialogs that I need to allow the user to open based on a
> selection from a combination of a scroll bar position and a button. When
the
> user presses a button it gets the position of a scroll bar which then
> decides which dialog to open.

> void CDetInfoDlg::OnBtnSelectsitu()
> {
>  file://get current scroll position
>  int nPos = m_cScrollCtrl.GetScrollPos();
>  int rc;
>  switch(nPos){
>   case 1:
>     CAngRipDlg dlg; // **************cannot be declared
> here!!!!******************
>     ShowWindow(SW_HIDE);
>     rc = dlg.DoModal();
>     if(rc == IDOK || rc == IDCANCEL){
>     ShowWindow(SW_SHOW);
>    }
>    break;
>   case 2:
>    break;
>   }
> }

> Problem is: If you look at the above situation, the CAngRipDlg dlg; cannot
> be declared there because the compiler complains that "initialization of
dlg
> is skipped by case label".

> I need each case to open a different dialog. I just don't show anymore for
> clarity!
> All in all, there are about 10 different cases, so if someone knows of a
> better way without using a switch statement that would be ok as well.

> Any help on this would be greatly appreciated!
> Thanks for the time!
> Mike



Tue, 19 Aug 2003 20:31:39 GMT  
 switch statement

Quote:

> void CDetInfoDlg::OnBtnSelectsitu()
> {
>  file://get current scroll position
>  int nPos = m_cScrollCtrl.GetScrollPos();
>  int rc;
>  switch(nPos){
>   case 1:
>     CAngRipDlg dlg; // **************cannot be declared
> here!!!!******************
>     ShowWindow(SW_HIDE);
>     rc = dlg.DoModal();
>     if(rc == IDOK || rc == IDCANCEL){
>     ShowWindow(SW_SHOW);
>    }
>    break;
>   case 2:
>    break;
>   }
> }

should be:

void CDetInfoDlg::OnBtnSelectsitu(){
  int nPos = m_cScrollCtrl.GetScrollPos();
  int rc;
  switch(nPos){
  case 1: {  // <- note
    CAngRipDlg dlg;
    ShowWindow(SW_HIDE);
    rc = dlg.DoModal();
    if(rc == IDOK || rc == IDCANCEL){
      ShowWindow(SW_SHOW);
    }
    break; } // <- note
  case 2:
    break;
  }

Quote:
}

You need to enter a {} block for each case that declares local variables
that should be local to the case.


Wed, 20 Aug 2003 05:15:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Switch Statement

2. Switch Statement Headaches

3. Labels in a switch statement

4. switch statement efficiency

5. user defined switch statements

6. Help needed: Code generation for CASE/SWITCH statements

7. Menu's using switch statements

8. switch statement efficiency

9. C# switch statement

10. switch statement problem

11. Stange behaviour with switch statement

12. functions to pointers vs switch statements

 

 
Powered by phpBB® Forum Software