Skip to content

Commit 8d1e176

Browse files
committed
Improve Winforms example browser.
Save selected example in settings. Open the same example when restarting the application.
1 parent 44bfefe commit 8d1e176

9 files changed

Lines changed: 83 additions & 48 deletions

File tree

Source/Examples/WindowsForms/ExampleBrowser/ExampleBrowser.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<AutoGen>True</AutoGen>
8282
<DependentUpon>Resources.resx</DependentUpon>
8383
</Compile>
84+
<None Include="app.config" />
8485
<None Include="Properties\Settings.settings">
8586
<Generator>SettingsSingleFileGenerator</Generator>
8687
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -130,5 +131,4 @@
130131
<Target Name="AfterBuild">
131132
</Target>
132133
-->
133-
</Project>
134-
134+
</Project>

Source/Examples/WindowsForms/ExampleBrowser/ExampleBrowser_NET40.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<AutoGen>True</AutoGen>
6868
<DependentUpon>Resources.resx</DependentUpon>
6969
</Compile>
70+
<None Include="app.config" />
7071
<None Include="Properties\Settings.settings">
7172
<Generator>SettingsSingleFileGenerator</Generator>
7273
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

Source/Examples/WindowsForms/ExampleBrowser/MainForm.Designer.cs

Lines changed: 6 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Examples/WindowsForms/ExampleBrowser/MainForm.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ private void InitTree()
3333
this.treeView1.Nodes.Add(node);
3434
}
3535

36-
node.Nodes.Add(new TreeNode(ex.Title) { Tag = ex });
36+
var exNode = new TreeNode(ex.Title) { Tag = ex };
37+
node.Nodes.Add(exNode);
38+
if (ex == this.vm.SelectedExample)
39+
{
40+
this.treeView1.SelectedNode = exNode;
41+
}
3742
}
38-
3943
this.treeView1.AfterSelect += this.TreeView1AfterSelect;
4044
}
4145

Source/Examples/WindowsForms/ExampleBrowser/MainWindowViewModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class MainWindowViewModel : INotifyPropertyChanged
2121
public MainWindowViewModel()
2222
{
2323
this.Examples = ExampleLibrary.Examples.GetList().OrderBy(e => e.Category);
24+
this.SelectedExample = this.Examples.FirstOrDefault(ei => ei.Title == Properties.Settings.Default.SelectedExample);
2425
}
2526

2627
public event PropertyChangedEventHandler PropertyChanged;
@@ -34,7 +35,12 @@ public IEnumerable<ExampleInfo> Examples
3435
public ExampleInfo SelectedExample
3536
{
3637
get { return this.selectedExample; }
37-
set { this.selectedExample = value; this.RaisePropertyChanged("SelectedExample"); }
38+
set
39+
{
40+
this.selectedExample = value; this.RaisePropertyChanged("SelectedExample");
41+
Properties.Settings.Default.SelectedExample = value != null ? value.Title : null;
42+
Properties.Settings.Default.Save();
43+
}
3844
}
3945

4046
protected void RaisePropertyChanged(string property)

Source/Examples/WindowsForms/ExampleBrowser/Program.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
// </summary>
88
// --------------------------------------------------------------------------------------------------------------------
99

10-
using System;
11-
using System.Collections.Generic;
12-
using System.Linq;
13-
using System.Windows.Forms;
14-
1510
namespace ExampleBrowser
1611
{
12+
using System;
13+
using System.Windows.Forms;
14+
1715
static class Program
1816
{
1917
/// <summary>
@@ -24,7 +22,10 @@ static void Main()
2422
{
2523
Application.EnableVisualStyles();
2624
Application.SetCompatibleTextRenderingDefault(false);
27-
Application.Run(new MainForm());
25+
using (var mainForm = new MainForm())
26+
{
27+
Application.Run(mainForm);
28+
}
2829
}
2930
}
3031
}

Source/Examples/WindowsForms/ExampleBrowser/Properties/Settings.Designer.cs

Lines changed: 31 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
3-
<Profiles>
4-
<Profile Name="(Default)" />
5-
</Profiles>
6-
<Settings />
7-
</SettingsFile>
2+
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="ExampleBrowser.Properties" GeneratedClassName="Settings">
3+
<Profiles />
4+
<Settings>
5+
<Setting Name="SelectedExample" Type="System.String" Scope="User">
6+
<Value Profile="(Default)" />
7+
</Setting>
8+
</Settings>
9+
</SettingsFile>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<configSections>
4+
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
5+
<section name="ExampleBrowser.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
6+
</sectionGroup>
7+
</configSections>
8+
<userSettings>
9+
<ExampleBrowser.Properties.Settings>
10+
<setting name="SelectedExample" serializeAs="String">
11+
<value />
12+
</setting>
13+
</ExampleBrowser.Properties.Settings>
14+
</userSettings>
15+
</configuration>

0 commit comments

Comments
 (0)