Example #1
0
        //  updateScreen method
        //  Standardized update routine to write the form depending on the current state.
        //  It sets form controls and properties redundantly for the sake of simplicity.
        //  There's no noticeable performance penalty from doing so.
        private void updateScreen()
        {
            // set the form caption from the application name "Cyryx Self Study" and the course name
            // courseName is a private property of Slide
            Text = appName + " - " + course.Name;
            // make the session panel visible if in session view
            sessions.Visible = course.View == View.sessions;
            // session view
            if (sessions.Visible)
            {
                // disable callbacks for session view so they're not triggered when set here
                rename.TextChanged           -= rename_TextChanged;
                listBox.SelectedIndexChanged -= listBox1_SelectedIndexChanged;
                // set the text box for renaming a session
                rename.Text = course.CurrentSession.Title;
                // set the list box that lists sessions in session view
                listBox.Items.Clear();
                listBox.Items.AddRange(course.SessionItems);
                listBox.SelectedIndex = course.Session;
                // re-enable the calbacks
                rename.TextChanged           += rename_TextChanged;
                listBox.SelectedIndexChanged += listBox1_SelectedIndexChanged;
            }
            else  // slide or student view
                  // set the title of the current session
                  // session numbering is added automatically
            {
                title.Text = ((1 + course.Session).ToString()) + ". " + course.CurrentSession.Title;
                // set the "Slide n of p" message
                slideNumber.Text = course.SlideNumber;
                // set the screen image
                picture.Image = course.CurrentScreen.Image;
                // set the screen text
                text.Rtf = course.CurrentScreen.Text;
                // change the text cursor to arrow when in student view
                text.Cursor = (course.View == View.student) ? Cursors.Arrow : Cursors.IBeam;
                // the question panel is visible only if this screen is a question
                // make text read only in student view
                text.ReadOnly     = course.View == View.student;
                questions.Visible = course.CurrentScreen.Question;
                // set up the question's answer panel if this is a question screen
                // the question itself is given in the text field
                if (questions.Visible)
                {
                    // set the answer text
                    answer1.Text = course.CurrentScreen.Answer(1);
                    answer2.Text = course.CurrentScreen.Answer(2);
                    answer3.Text = course.CurrentScreen.Answer(3);
                    answer4.Text = course.CurrentScreen.Answer(4);
                    // set the cursor to arrow for student view
                    answer1.Cursor = (course.View == View.student) ? Cursors.Arrow : Cursors.IBeam;
                    answer2.Cursor = (course.View == View.student) ? Cursors.Arrow : Cursors.IBeam;
                    answer3.Cursor = (course.View == View.student) ? Cursors.Arrow : Cursors.IBeam;
                    answer4.Cursor = (course.View == View.student) ? Cursors.Arrow : Cursors.IBeam;
                    // reset normal background color which changes for correct/incorrect answers
                    answer1.BackColor = text.BackColor;
                    answer2.BackColor = text.BackColor;
                    answer3.BackColor = text.BackColor;
                    answer4.BackColor = text.BackColor;
                    // disable radio button callbacks so they're not triggered when setting them here
                    radioButton1.CheckedChanged -= radioButton1_CheckedChanged;
                    radioButton2.CheckedChanged -= radioButton2_CheckedChanged;
                    radioButton3.CheckedChanged -= radioButton3_CheckedChanged;
                    radioButton4.CheckedChanged -= radioButton4_CheckedChanged;
                    // set all radio buttons to false initially
                    radioButton1.Checked = false;
                    radioButton2.Checked = false;
                    radioButton3.Checked = false;
                    radioButton4.Checked = false;
                    // set answer text to read only in student view
                    answer1.ReadOnly = course.View == View.student;
                    answer2.ReadOnly = course.View == View.student;
                    answer3.ReadOnly = course.View == View.student;
                    answer4.ReadOnly = course.View == View.student;
                    // set choice to correct choice if not in instructor view
                    if (course.View != View.student)
                    {
                        choice = course.CurrentScreen.Correct;
                    }
                    // set radio button and answer text background color depending on choice
                    RadioButton rb = null;
                    TextBox     tb = null;
                    // get the radio button and answer control for the choice
                    switch (choice)
                    {
                    case 1: rb = radioButton1; tb = answer1; break;

                    case 2: rb = radioButton2; tb = answer2; break;

                    case 3: rb = radioButton3; tb = answer3; break;

                    case 4: rb = radioButton4; tb = answer4; break;
                    }
                    if (rb != null)        // if there is a valid choice
                    {
                        rb.Checked = true; // set checked to true
                        // if in authoring (slide) view, green background indicates correct choice
                        if (course.View != View.student)
                        {
                            tb.BackColor = Color.LightGreen;
                        }
                        else // in student view, background and sound depend on whether selection is correct or not
                        if (course.CurrentScreen.Correct != choice)
                        {
                            tb.BackColor = Color.LightPink;
                            course.GBSound(false);
                        }
                        else
                        {
                            tb.BackColor = Color.LightGreen;
                            course.GBSound(true);
                        }
                    }
                    // re-enable radio button callbacks
                    radioButton1.CheckedChanged += radioButton1_CheckedChanged;
                    radioButton2.CheckedChanged += radioButton2_CheckedChanged;
                    radioButton3.CheckedChanged += radioButton3_CheckedChanged;
                    radioButton4.CheckedChanged += radioButton4_CheckedChanged;
                }
                // enable context menus only in slide view
                contextMenuStrip1.Enabled = course.View == View.slide;
                contextMenuStrip2.Enabled = course.View == View.slide;
                // initialized text and answer modified states to false, to detect when they change
                text.Modified    = false;
                answer1.Modified = false;
                answer2.Modified = false;
                answer3.Modified = false;
                answer4.Modified = false;
            }
            // make the question toggle button visible only in slide view
            questionsShow.Visible = course.View == View.slide;
            // disable callback for button to toggle question panel on and off
            questionsShow.CheckedChanged -= checkBox1_CheckedChanged;
            // set the toggle
            questionsShow.Checked = questions.Visible;
            // re-enable the callback
            questionsShow.CheckedChanged += checkBox1_CheckedChanged;
            // turn "Student View" label on or off
            StudentView.Visible = course.View == View.student;
            // set menu checked states for the 3 different views
            toolStripMenuItemSessions.CheckState =
                (course.View == View.sessions) ? CheckState.Checked : CheckState.Unchecked;
            toolStripMenuItemSlides.CheckState =
                (course.View == View.slide) ? CheckState.Checked : CheckState.Unchecked;
            toolStripMenuItemStudent.CheckState =
                (course.View == View.student) ? CheckState.Checked : CheckState.Unchecked;
            // enable or disable undo and redo depending on the undo state
            undoToolStripMenuItem.Enabled = course.UnDoCtr > -1;
            redoToolStripMenuItem.Enabled = course.UnDoPos <= course.UnDoCtr;
            // enable or disable session menu items depending on view
            cutToolStripMenuItem.Enabled    = course.View == View.slide;
            copyToolStripMenuItem.Enabled   = course.View == View.slide;
            pasteToolStripMenuItem.Enabled  = course.View == View.slide;
            insertToolStripMenuItem.Enabled = course.View == View.slide;
            // "Sound" menu item is enabled if there is a sound available for this screen
            // clicking it plays the sound clip, which normally doesn't play automatically
            soundToolStripMenuItem.Enabled =
                course.CurrentScreen.SoundFile != "" &&
                course.CurrentScreen.SoundFile != null;
            // make the panel at the bottom of the screen (with Back and Next buttons) invisible in session view
            panel1.Visible = course.View != View.sessions;
            // when the screen changes...
            if (course.Directory != courseDirectory ||
                course.Session != session ||
                course.Screen != screen ||
                course.View != view)
            {
                choice = -1;    // set the question response to invalid
                course.Sound(); // play the sound clip for this screen
                // save the navigation state so we can detect screen change next time
                courseDirectory = course.Directory;
                session         = course.Session;
                screen          = course.Screen;
                view            = course.View;
                // set initial focus to the Next button
                nextButton.Focus();
            }
        }