DecryptR Part 2

In a recent post, I discussed the DecryptR application I have been toying with.  DecryptR V2While the fundamentals are working, there is much to do.  Today I’ll make a few minor improvements.

First, let’s set the font in the list box to a fixed font, so that the columns align better.  To do so I’ll open the application in Expression Blend.  In the Objects and Timeline I’ll click my way down to Results– the list box and then in the Text properties window I’ll set the font family to Courier New (a fixed width font) and the font size to 24.

Let’s save this as is, and return to Visual Studio.  The next job is to implement the event handler for the Configure button.  Create a new page Configuration.xaml.

We’ll want prompts and text boxes for the user to fill in how many letters should be used in the game and how many letters in the code.  We also want a check box to indicate whether the code should be revealed or printed as asterisks.

Here’s the Xaml for that page’s ContentPanel,

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
 <Grid.RowDefinitions>
    <RowDefinition
       Height="1*" />
    <RowDefinition
       Height="1*" />
    <RowDefinition
       Height="1*" />
    <RowDefinition
       Height="1*" />
    <RowDefinition
       Height="1*" />
    <RowDefinition
       Height="1*" />
    <RowDefinition
       Height="3*" />

 </Grid.RowDefinitions>
 <Grid.ColumnDefinitions>
    <ColumnDefinition
       Width="184*" />
    <ColumnDefinition
       Width="272*" />
 </Grid.ColumnDefinitions>
 <TextBlock
    Text="Number of letters"
    Margin="0,0,6,0" />
 <TextBox
    Name="NumLetters"
    Width="50"
    HorizontalAlignment="Left"
    Grid.Row="0"
    Grid.Column="1" />
 <TextBlock
    Text="Size of code"
    Grid.Row="1" />
 <TextBox
    Name="SizeOfCode"
    Width="50"
    HorizontalAlignment="Left"
    Grid.Row="1"
    Grid.Column="1" />
 <CheckBox
    Content="Show Code?"
    IsChecked="False"
    Name="ShowCode"
    Grid.Row="2"
    Grid.Column="1" />
 <Button
    Content="Done"
    Name="Done"
    Grid.Row="3"
    Grid.Column="1" />
</Grid>

The job of the code-behind is to store these choices for the main page, which we’ll do with Isolated Storage.  To do so, create a private member of type Isolated Storage Settings

private IsolatedStorageSettings _isoStorage;

and initialize it in the constructor,

_isoStorage = IsolatedStorageSettings.ApplicationSettings;

All the real work is done in Navigated_From, called when you leave the page (to return to Main.xaml),

protected override void OnNavigatedFrom( 
    System.Windows.Navigation.NavigationEventArgs e )
{
    if (_isoStorage.Contains( "NumberOfLetters" ))
        _isoStorage["NumberOfLetters"] = NumLetters.Text;
    else
        _isoStorage.Add( "NumberOfLetters", NumLetters.Text );

    if (_isoStorage.Contains( "SizeOfCode" ))
        _isoStorage["SizeOfCode"] = SizeOfCode.Text;
    else
        _isoStorage.Add( "SizeOfCode", SizeOfCode.Text );

    if (_isoStorage.Contains( "ShowCode" ))
        _isoStorage["ShowCode"] = ShowCode.IsChecked;
    else
        _isoStorage.Add( "ShowCode", ShowCode.IsChecked );
}

 

With these values tucked away in Isolated Storage we can retrieve them and assign them to the appropriate variables in OnNavigatedTo in MainPage,

protected override void OnNavigatedTo( 
    System.Windows.Navigation.NavigationEventArgs e )
{
    if (_isoStorage.Contains( "NumberOfLetters" ))
        Number_Of_Letters =  
            int.Parse(_isoStorage["NumberOfLetters"].ToString());

    if (_isoStorage.Contains( "SizeOfCode" ))
        Answer_Size = 
            int.Parse( _isoStorage["SizeOfCode"].ToString() );

    if (_isoStorage.Contains( "ShowCode" ))
        ShowCode = 
            bool.Parse( _isoStorage["ShowCode"].ToString() );

    string initialRow = GenerateAnswer();
    InterimResults.Add( initialRow );
    Results.ItemsSource = InterimResults;

}

 

Note that the last three lines (generating the answer and setting the initial row) were moved from the constructor to the bottom of the OnNavigatedTo method.

The small changes greatly enhance the game, allowing the user to set the number of letters in play, the number of letters in the code and whether or not the code is revealed.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in Essentials, Mango, Mini-Tutorial, Patterns & Skills and tagged . Bookmark the permalink.