p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer (http://p2p.wrox.com/forumdisplay.php?f=574)
-   -   Lesson 13 Exercise 3 (http://p2p.wrox.com/showthread.php?t=87868)

1rf2aa3oa4tb June 27th, 2012 07:25 PM

Lesson 13 Exercise 3
 
I am having trouble with this exercise. There is no initial highlight for the PickSessionForm List View Box. The selection and highlight are not saved when I click the button to open the form again. Everything else works, the text is transferred between forms, just no initial or saved highlighted selection when the other form is opened.

The solution code works and my code was very similar. I spent an hour looking at my code on one monitor and the solution code on the other. The book mentions the load event handler is what controls what should be selected. My load event and OK button events are the same as the solution, as well as the button event handlers on the main form.

the load handler....
Code:

  // Initialize the selection
        private void PickSessionForm_Load(object sender, EventArgs e)
        {
            sessionsListView.SelectedIndices.Add(SessionIndex);
           
            // Ensure the selection is visible
            sessionsListView.SelectedItems[0].EnsureVisible();
        }

Could the problem be in the designer code or somewhere else?

Rod Stephens June 28th, 2012 01:19 PM

Quote:

Could the problem be in the designer code or somewhere else?
That's good intuition. If you're pretty sure the code is correct, then there is sometimes a property on the form set by the Form Designer that changes the way things work.

Here, however, I don't think there are any properties that would change this.

I suspect the problem is in the code that displays the form. If you look at the pick button event handlers on the main form, one of the things they do before displaying the session form is setting the session form's SessionIndex property with code like this:

Code:

frm.SessionIndex = SessionIndex1;
Then the session form's Load event handler uses the form's SessionIndex to initialize the selected item. If the main form's event handler doesn't set SessionIndex, then it would default to 0 and the session form would always initially select the first item (with index 0).

Let me know if that works.

Rod Stephens July 2nd, 2012 11:24 AM

After looking at the project you emailed me, I found it. You did everything correctly but the list control on the selection form doesn't have the input focus when the form is displayed and Microsoft in its infinite wisdom decided that this type of control should not show you what items are selected unless it has the focus.

If you display the form and press tab until the list has focus, you'll see that the correct item is selected.

To fix this, open the form in the designer. Be sure the form is selected and not one of its controls. Then open the View menu and select Tab Order. Now click on the control in the order in which you want them to get focus. Click the list first so it gets the initial focus. Now when you run the program, the form should work as expected.

Sorry about the confusion.

1rf2aa3oa4tb July 2nd, 2012 12:04 PM

Quote:

Originally Posted by Rod Stephens (Post 285176)
To fix this, open the form in the designer. Be sure the form is selected and not one of its controls. Then open the View menu and select Tab Order. Now click on the control in the order in which you want them to get focus. Click the list first so it gets the initial focus. Now when you run the program, the form should work as expected.

Success. I can put this one week old mystery to bed. Thank you.


All times are GMT -4. The time now is 08:34 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.