Skip to content

Commit 37a489b

Browse files
mjrousosRon Petrusha
authored andcommitted
WinForms matching sample (#455)
* Copy existing sample from https://code.msdn.microsoft.com/windowsdesktop/Complete-Matching-Game-4cffddba * Port to netcoreapp3.0 and remove unused files. * Remove unused using statements * Replace description.html with README.md and update parent README * Remove Apache 2.0 license, since this sample will be covered by the MIT license in the repo root * Add a screenshot to the readme * Update README.md
1 parent 9d8e9de commit 37a489b

File tree

10 files changed

+739
-0
lines changed

10 files changed

+739
-0
lines changed

windowsforms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ If you're new to .NET Core, here are a few resources to help you understand the
1313
| Sample Name | Description |
1414
| ----------- | ----------- |
1515
| [Hello World - shared source](helloworld-sharedsource) | This sample shows you how to share source between a .NET Framework WinForms application and a .NET Core WinForms application. Use this to get the full .NET Framework tooling experience while still building for .NET Core. |
16+
| [Matching Game](matching-game) | This sample demonstrates simple event handling and timers in a .NET Core 3 WinForms application |
1617

1718
## Getting Started
1819

windowsforms/matching-game/MatchingGame/Form1.Designer.cs

Lines changed: 321 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Windows.Forms;
5+
6+
namespace MatchingGame
7+
{
8+
public partial class Form1 : Form
9+
{
10+
// firstClicked points to the first Label control
11+
// that the player clicks, but it will be null
12+
// if the player hasn't clicked a label yet.
13+
Label firstClicked = null;
14+
15+
// secondClicked points to the second Label control
16+
// that the player clicks.
17+
Label secondClicked = null;
18+
19+
// Use this Random object to choose random icons for the squares.
20+
Random random = new Random();
21+
22+
// Each of these letters is an interesting icon
23+
// in the Webdings font,
24+
// and each icon appears twice in this list.
25+
List<string> icons = new List<string>()
26+
{
27+
"!", "!", "N", "N", ",", ",", "k", "k",
28+
"b", "b", "v", "v", "w", "w", "z", "z"
29+
};
30+
31+
/// <summary>
32+
/// Assign each icon from the list of icons to a random square
33+
/// </summary>
34+
private void AssignIconsToSquares()
35+
{
36+
// The TableLayoutPanel has 16 labels,
37+
// and the icon list has 16 icons,
38+
// so an icon is pulled at random from the list
39+
// and added to each label.
40+
foreach (Control control in tableLayoutPanel1.Controls)
41+
{
42+
Label iconLabel = control as Label;
43+
if (iconLabel != null)
44+
{
45+
int randomNumber = random.Next(icons.Count);
46+
iconLabel.Text = icons[randomNumber];
47+
iconLabel.ForeColor = iconLabel.BackColor;
48+
icons.RemoveAt(randomNumber);
49+
}
50+
}
51+
}
52+
53+
54+
public Form1()
55+
{
56+
InitializeComponent();
57+
AssignIconsToSquares();
58+
}
59+
60+
/// <summary>
61+
/// Every label's Click event is handled by this event handler.
62+
/// </summary>
63+
/// <param name="sender">The label that was clicked.</param>
64+
/// <param name="e"></param>
65+
private void label_Click(object sender, EventArgs e)
66+
{
67+
// The timer is only on after two non-matching
68+
// icons have been shown to the player,
69+
// so ignore any clicks if the timer is running
70+
if (timer1.Enabled == true)
71+
return;
72+
73+
Label clickedLabel = sender as Label;
74+
75+
if (clickedLabel != null)
76+
{
77+
// If the clicked label is black, the player clicked
78+
// an icon that's already been revealed --
79+
// ignore the click.
80+
if (clickedLabel.ForeColor == Color.Black)
81+
// All done - leave the if statements.
82+
return;
83+
84+
// If firstClicked is null, this is the first icon
85+
// in the pair that the player clicked,
86+
// so set firstClicked to the label that the player
87+
// clicked, change its color to black, and return.
88+
if (firstClicked == null)
89+
{
90+
firstClicked = clickedLabel;
91+
firstClicked.ForeColor = Color.Black;
92+
93+
// All done - leave the if statements.
94+
return;
95+
}
96+
97+
// If the player gets this far, the timer isn't
98+
// running and firstClicked isn't null,
99+
// so this must be the second icon the player clicked
100+
// Set its color to black.
101+
secondClicked = clickedLabel;
102+
secondClicked.ForeColor = Color.Black;
103+
104+
// Check to see if the player won.
105+
CheckForWinner();
106+
107+
// If the player clicked two matching icons, keep them
108+
// black and reset firstClicked and secondClicked
109+
// so the player can click another icon.
110+
if (firstClicked.Text == secondClicked.Text)
111+
{
112+
firstClicked = null;
113+
secondClicked = null;
114+
return;
115+
}
116+
117+
// If the player gets this far, the player
118+
// clicked two different icons, so start the
119+
// timer (which will wait three quarters of
120+
// a second, and then hide the icons).
121+
timer1.Start();
122+
}
123+
}
124+
125+
/// <summary>
126+
/// This timer is started when the player clicks
127+
/// two icons that don't match,
128+
/// so it counts three quarters of a second
129+
/// and then turns itself off and hides both icons.
130+
/// </summary>
131+
/// <param name="sender"></param>
132+
/// <param name="e"></param>
133+
private void timer1_Tick(object sender, EventArgs e)
134+
{
135+
// Stop the timer.
136+
timer1.Stop();
137+
138+
// Hide both icons.
139+
firstClicked.ForeColor = firstClicked.BackColor;
140+
secondClicked.ForeColor = secondClicked.BackColor;
141+
142+
// Reset firstClicked and secondClicked
143+
// so the next time a label is
144+
// clicked, the program knows it's the first click.
145+
firstClicked = null;
146+
secondClicked = null;
147+
}
148+
149+
/// <summary>
150+
/// Check every icon to see if it is matched, by
151+
/// comparing its foreground color to its background color.
152+
/// If all of the icons are matched, the player wins.
153+
/// </summary>
154+
private void CheckForWinner()
155+
{
156+
// Go through all of the labels in the TableLayoutPanel,
157+
// checking each one to see if its icon is matched.
158+
foreach (Control control in tableLayoutPanel1.Controls)
159+
{
160+
Label iconLabel = control as Label;
161+
162+
if (iconLabel != null)
163+
{
164+
if (iconLabel.ForeColor == iconLabel.BackColor)
165+
return;
166+
}
167+
}
168+
169+
// If the loop didn’t return, it didn't find
170+
// any unmatched icons.
171+
// That means the user won. Show a message and close the form.
172+
MessageBox.Show("You matched all the icons!", "Congratulations!");
173+
Close();
174+
}
175+
176+
}
177+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
<metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
121+
<value>17, 17</value>
122+
</metadata>
123+
</root>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
<RootNamespace>MatchingGame</RootNamespace>
7+
<AssemblyName>MatchingGame</AssemblyName>
8+
9+
<!-- Don't automatically generate assembly info attributes
10+
found in AssemblyInfo.cs. This property is useful for
11+
applications ported from NetFx, which may have used
12+
AssemblyInfo.cs -->
13+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<FrameworkReference Include="Microsoft.DesktopUI"/>
18+
</ItemGroup>
19+
20+
<!-- By default, all .cs and .resx files in the current
21+
directory or descendant directories will be included.
22+
Optionally, they can be updated as demonstrated here
23+
so that dependent files will appear as expected in
24+
Visual Studio's solution explorer. -->
25+
<ItemGroup>
26+
<Compile Update="Form1.Designer.cs">
27+
<DependentUpon>Form1.cs</DependentUpon>
28+
</Compile>
29+
<EmbeddedResource Update="Form1.resx">
30+
<DependentUpon>Form1.cs</DependentUpon>
31+
</EmbeddedResource>
32+
</ItemGroup>
33+
34+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Windows.Forms;
3+
4+
namespace MatchingGame
5+
{
6+
static class Program
7+
{
8+
/// <summary>
9+
/// The main entry point for the application.
10+
/// </summary>
11+
[STAThread]
12+
static void Main()
13+
{
14+
Application.EnableVisualStyles();
15+
Application.SetCompatibleTextRenderingDefault(false);
16+
Application.Run(new Form1());
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)