Wednesday, 7 November 2012

Windows Mobile .NET CF: Form Title Inheritance

I recently came across a series of issues when dealing with my field trial testers for a large-scale PDA deployment. A change I made to the way forms were opened happened to change the title text from the Job Number and was only displaying the name of the application. The original title was only being displayed for testing purposes but it turns out it was a handy feature for them to have.
In the process of our testing they had also picked up how to use Task Manager (Running Programs) on the PDA so were experiencing unexpected results when they lost the app behind their email client, and selected the wrong form out of the many that were showing in in the Running Programs.

This prompted me to try to get a fix - both to their newfound usability issue and to the problems they were causing when messing around in task manager. Savvy users!

Case:

  • You are developing a WM6.1 .NET CF 2.0/3.5 (or similar) forms app that has multiple forms.
  • You do want to be able to change the visible title (Form.Text) at the the top of the screen
  • You do not want multiple forms to display in task manager (Memory > Running Programs)

Example Situation:

Your user starts the app and is presented with a login form. After logging in they are presented with a menu form (a bunch of buttons), and from here they open a Job List Form, and then select and open a job in the Job Details form.
You require the title of the application to read 'Job Manager' until they open a job, then you require it to display the job number (J123456). Only one job at a time can be open and jobs must be closed before they go back to the previous page.
You also require that if the users go into the task manager they only see the currently active form.

Rules of engagement:

If Form.Owner is set the child form will inheret the parent forms title
If Form.Owner is set the child form cannot change the title
If Form.Owner is not set the child form is able to change the title but the parent form will display in task manager.
If a foms title is set to an empty string it does not appear in task manager.

Solution:

When opening a child form, set the parent forms title (.text property) to an empty string to hide it from task manager, don't set the child forms owner, and display the form. After returning from the child form reset the title for the parent form.

If dealing with multiple levels of forms, as with the Example Situation:
Each form needs to specify what the title will be - this means that each form title will need to be set to 'Job Management' when active and '' when opening a child form. When we get to the Job List Form and select a job the Job List form will set it's title to '' and the child form (Job Details) will set its title to whatever the Job Number is. In the case of this Job Details form it has multiple child forms capturing various aspects of the job data - but all of these are opened using Form.Owner to retain the title of the parent form. If we so desired we could treat this form the same as all the others in the solution so far, and have our titles read 'J123456 - Widgets' or 'J123456 - Sign Off', but due to the screen res on most WM6.1 devices this wouldn't fit anyway.

Point of interest - if you don't set the name of a child form (and there is no name to inherit etc) the title will say 'Start' - this is the Start Button (I actually got confused for a second there).

In hindsight this does seem like a bit of a hack, but the more I develop in Windows Mobile 6.1 and .NET CF the more I realise that this is the way things are done.

Sample code:

string myTitle = this.Text;
this.Text = "";
this.SuspendLayout();
frmIndex.ShowDialog();
this.ResumeLayout();
this.Text = myTitle;
The Suspend/ResumeLayout() calls are not specifically required for this example however I have had problems with the parent form responding to events and consider it important to remember when opening child forms. Specifically the issues I have had are with textboxes on the child forms automatically calling the SIP and having the call handled by the parent form instead. In my case this resulted in the child form becoming invisible and the parent form showing but being non-responsive. Your results may vary.

References:

http://www.mobilepractices.com/2007/12/working-with-forms-in-windows-mobile.html

No comments:

Post a Comment