Count the Islands - An Example Program for WPF and Recursion

106 22
< Continued from page 3

Finally, to nail down what was actually happening in the program, I decided to mark each cell as it was tested so I could be completely sure that everything was working. That involves more WPF coding. Here's the MarkGridCell routine that does it. ("MAP" is the name of the Grid control.)

Friend Sub MarkGridCell()
   MapChars(RGL, CGL) = IC.ToString
   Dim c As New Canvas
   c.Background = Brushes.LightGreen
   Grid.SetRow(c, RGL)
   Grid.SetColumn(c, CGL)
   Map.Children.Add(c)
   Dim tb As New TextBlock
   tb.FontSize = 22
   tb.Text = IC.ToString
   Grid.SetRow(tb, RGL)
   Grid.SetColumn(tb, CGL)
   Map.Children.Add(tb)
End Sub



This code adds two more WPF controls to each "Land" cell: another TextBlock control and a Canvas control so I can set the background. I use the same "attached property" coding technique to set these. Here's a 10 x 10 map where the islands are highlighted and each cell is marked to show which island it's in.



--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Click Here to download the program.
Source...

Leave A Reply

Your email address will not be published.