Skip to content

Commit dbbab3c

Browse files
committed
Last windows Size and Location saved in config file support added
1 parent da4e413 commit dbbab3c

5 files changed

Lines changed: 44 additions & 8 deletions

File tree

Serial COM/Config.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,25 @@ public static class Config
1111
{
1212
public static string PortName { get; set; }
1313
public static string BaudRate { get; set; }
14+
public static string WindowSize { get; set; }
15+
public static string WindowLocation { get; set; }
1416

1517
public static void Load()
1618
{
1719
var config = GetConfiguration();
1820
PortName = config.AppSettings.Settings["PortName"].Value;
1921
BaudRate = config.AppSettings.Settings["BaudRate"].Value;
22+
WindowSize = config.AppSettings.Settings["WindowSize"].Value;
23+
WindowLocation = config.AppSettings.Settings["WindowLocation"].Value;
2024
}
2125

2226
public static void Save()
2327
{
2428
var config = GetConfiguration();
2529
config.AppSettings.Settings["PortName"].Value = PortName;
2630
config.AppSettings.Settings["BaudRate"].Value = BaudRate;
31+
config.AppSettings.Settings["WindowSize"].Value = WindowSize;
32+
config.AppSettings.Settings["WindowLocation"].Value = WindowLocation;
2733
config.Save(ConfigurationSaveMode.Modified);
2834
}
2935

Serial COM/MainForm.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ public void UpdateSerialPortDescription()
171171
else toolStripPortDetails.Text = portName;
172172
}
173173

174+
public static string EncodeNumConfig(int[] param)
175+
{
176+
return string.Join(",", param);
177+
}
178+
179+
public static int DecodeNumConfig(string config, int index)
180+
{
181+
return int.Parse(config.Split(',')[index]);
182+
}
183+
174184
public static string ByteArrayToFormatedString(byte[] bytes)
175185
{
176186
var sb = new StringBuilder();
@@ -221,11 +231,6 @@ public MainForm()
221231
}
222232

223233
private void MainForm_Load(object sender, EventArgs e)
224-
{
225-
226-
}
227-
228-
private void MainForm_Shown(object sender, EventArgs e)
229234
{
230235
try
231236
{
@@ -237,11 +242,31 @@ private void MainForm_Shown(object sender, EventArgs e)
237242
comboBoxPortName.SelectedIndex = comboBoxPortName.Items.IndexOf(Config.PortName);
238243
if (comboBoxBaudRate.Items.Contains(Config.BaudRate))
239244
comboBoxBaudRate.SelectedIndex = comboBoxBaudRate.Items.IndexOf(Config.BaudRate);
245+
this.Size = new Size(DecodeNumConfig(Config.WindowSize, 0), DecodeNumConfig(Config.WindowSize, 1));
246+
var winlocation = new Point(DecodeNumConfig(Config.WindowLocation, 0), DecodeNumConfig(Config.WindowLocation, 1));
247+
248+
// check location within screen bounds
249+
if (winlocation.X >= Screen.AllScreens.Sum(p => p.Bounds.Width))
250+
{
251+
winlocation.X = (Screen.PrimaryScreen.Bounds.Width - this.Width) / 2;
252+
winlocation.Y = (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2;
253+
}
254+
this.Location = winlocation;
255+
}
256+
catch (Exception ex)
257+
{
258+
PopupException(ex.Message);
259+
}
260+
}
240261

262+
private void MainForm_Shown(object sender, EventArgs e)
263+
{
264+
try
265+
{
241266
MonitorDeviceChanges();
242-
toolStripStatusLabel1.Text = "Ready to Connect";
243267
labelRxSelection.Text = "RxSel: 0";
244268
labelTxSelection.Text = "TxSel: 0";
269+
toolStripStatusLabel1.Text = "Ready to Connect";
245270
}
246271
catch (Exception ex)
247272
{
@@ -258,6 +283,8 @@ private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
258283
portName = _portNames[comboBoxPortName.SelectedIndex];
259284
Config.PortName = portName;
260285
Config.BaudRate = comboBoxBaudRate.Text;
286+
Config.WindowSize = EncodeNumConfig(new[] { this.Size.Width, this.Size.Height });
287+
Config.WindowLocation = EncodeNumConfig(new[] { this.Location.X, this.Location.Y });
261288
Config.Save();
262289
_serialCom.Dispose();
263290
}

Serial COM/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("1.0.1.0")]
36+
[assembly: AssemblyFileVersion("1.0.1.0")]

Serial COM/app.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<appSettings>
44
<add key="PortName" value="COM1"/>
55
<add key="BaudRate" value="9600"/>
6+
<add key="WindowSize" value="850,800"/>
7+
<add key="WindowLocation" value="535,100"/>
68
</appSettings>
79
<startup>
810
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

Serial COM/mainForm.Designer.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)