Fixed "Apply" not saving settings back to the config prooperly.

This commit is contained in:
David Khachaturov 2020-01-18 13:47:18 +00:00
parent fb503e7449
commit 2bfc7e5546
3 changed files with 68 additions and 49 deletions

View file

@ -47,9 +47,9 @@
this.startInTrayBox = new System.Windows.Forms.CheckBox(); this.startInTrayBox = new System.Windows.Forms.CheckBox();
this.btn_open3rdP = new System.Windows.Forms.Button(); this.btn_open3rdP = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.settingsTable = new System.Windows.Forms.TableLayoutPanel();
this.rightPanel = new System.Windows.Forms.Panel(); this.rightPanel = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button(); this.settingsApply = new System.Windows.Forms.Button();
this.AutoCalibrate = new System.Windows.Forms.Button(); this.AutoCalibrate = new System.Windows.Forms.Button();
this.contextMenu.SuspendLayout(); this.contextMenu.SuspendLayout();
this.conCntrls.SuspendLayout(); this.conCntrls.SuspendLayout();
@ -273,7 +273,7 @@
// //
// groupBox1 // groupBox1
// //
this.groupBox1.Controls.Add(this.tableLayoutPanel1); this.groupBox1.Controls.Add(this.settingsTable);
this.groupBox1.Location = new System.Drawing.Point(3, 11); this.groupBox1.Location = new System.Drawing.Point(3, 11);
this.groupBox1.Margin = new System.Windows.Forms.Padding(2); this.groupBox1.Margin = new System.Windows.Forms.Padding(2);
this.groupBox1.Name = "groupBox1"; this.groupBox1.Name = "groupBox1";
@ -283,25 +283,25 @@
this.groupBox1.TabStop = false; this.groupBox1.TabStop = false;
this.groupBox1.Text = "Config"; this.groupBox1.Text = "Config";
// //
// tableLayoutPanel1 // settingsTable
// //
this.tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) this.settingsTable.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.tableLayoutPanel1.AutoScroll = true; this.settingsTable.AutoScroll = true;
this.tableLayoutPanel1.ColumnCount = 2; this.settingsTable.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 58.90411F)); this.settingsTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 58.90411F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 41.09589F)); this.settingsTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 41.09589F));
this.tableLayoutPanel1.Location = new System.Drawing.Point(4, 17); this.settingsTable.Location = new System.Drawing.Point(4, 17);
this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(2); this.settingsTable.Margin = new System.Windows.Forms.Padding(2);
this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.settingsTable.Name = "settingsTable";
this.tableLayoutPanel1.RowCount = 1; this.settingsTable.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.settingsTable.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.Size = new System.Drawing.Size(219, 219); this.settingsTable.Size = new System.Drawing.Size(219, 219);
this.tableLayoutPanel1.TabIndex = 1; this.settingsTable.TabIndex = 1;
// //
// rightPanel // rightPanel
// //
this.rightPanel.Controls.Add(this.button1); this.rightPanel.Controls.Add(this.settingsApply);
this.rightPanel.Controls.Add(this.groupBox1); this.rightPanel.Controls.Add(this.groupBox1);
this.rightPanel.Location = new System.Drawing.Point(289, 1); this.rightPanel.Location = new System.Drawing.Point(289, 1);
this.rightPanel.Margin = new System.Windows.Forms.Padding(2, 2, 12, 2); this.rightPanel.Margin = new System.Windows.Forms.Padding(2, 2, 12, 2);
@ -310,18 +310,18 @@
this.rightPanel.TabIndex = 11; this.rightPanel.TabIndex = 11;
this.rightPanel.Visible = false; this.rightPanel.Visible = false;
// //
// button1 // settingsApply
// //
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.settingsApply.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.settingsApply.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(165, 252); this.settingsApply.Location = new System.Drawing.Point(165, 252);
this.button1.Margin = new System.Windows.Forms.Padding(2); this.settingsApply.Margin = new System.Windows.Forms.Padding(2);
this.button1.Name = "button1"; this.settingsApply.Name = "settingsApply";
this.button1.Size = new System.Drawing.Size(61, 21); this.settingsApply.Size = new System.Drawing.Size(61, 21);
this.button1.TabIndex = 10; this.settingsApply.TabIndex = 10;
this.button1.Text = "Apply"; this.settingsApply.Text = "Apply";
this.button1.UseVisualStyleBackColor = true; this.settingsApply.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click); this.settingsApply.Click += new System.EventHandler(this.settingsApply_Click);
// //
// AutoCalibrate // AutoCalibrate
// //
@ -391,9 +391,9 @@
private System.Windows.Forms.CheckBox startInTrayBox; private System.Windows.Forms.CheckBox startInTrayBox;
private System.Windows.Forms.Button btn_open3rdP; private System.Windows.Forms.Button btn_open3rdP;
private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; private System.Windows.Forms.TableLayoutPanel settingsTable;
private System.Windows.Forms.Panel rightPanel; private System.Windows.Forms.Panel rightPanel;
private System.Windows.Forms.Button button1; private System.Windows.Forms.Button settingsApply;
private System.Windows.Forms.Label foldLbl; private System.Windows.Forms.Label foldLbl;
private System.Windows.Forms.Button AutoCalibrate; private System.Windows.Forms.Button AutoCalibrate;
} }

View file

@ -42,8 +42,8 @@ namespace BetterJoyForCemu {
string[] myConfigs = ConfigurationManager.AppSettings.AllKeys; string[] myConfigs = ConfigurationManager.AppSettings.AllKeys;
Size childSize = new Size(87, 20); Size childSize = new Size(87, 20);
for (int i = 0; i != myConfigs.Length; i++) { for (int i = 0; i != myConfigs.Length; i++) {
tableLayoutPanel1.RowCount++; settingsTable.RowCount++;
tableLayoutPanel1.Controls.Add(new Label() { Text = myConfigs[i], TextAlign = ContentAlignment.BottomLeft, AutoEllipsis = true, Size = childSize }, 0, i); settingsTable.Controls.Add(new Label() { Text = myConfigs[i], TextAlign = ContentAlignment.BottomLeft, AutoEllipsis = true, Size = childSize }, 0, i);
var value = ConfigurationManager.AppSettings[myConfigs[i]]; var value = ConfigurationManager.AppSettings[myConfigs[i]];
Control childControl; Control childControl;
@ -53,8 +53,8 @@ namespace BetterJoyForCemu {
childControl = new TextBox() { Text = value, Size = childSize }; childControl = new TextBox() { Text = value, Size = childSize };
} }
tableLayoutPanel1.Controls.Add(childControl, 1, i);
childControl.MouseClick += cbBox_Changed; childControl.MouseClick += cbBox_Changed;
settingsTable.Controls.Add(childControl, 1, i);
} }
} }
@ -229,7 +229,27 @@ namespace BetterJoyForCemu {
partyForm.ShowDialog(); partyForm.ShowDialog();
} }
private void button1_Click(object sender, EventArgs e) { private void settingsApply_Click(object sender, EventArgs e) {
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
for (int row = 0; row < ConfigurationManager.AppSettings.AllKeys.Length; row++) {
var valCtl = settingsTable.GetControlFromPosition(1, row);
var KeyCtl = settingsTable.GetControlFromPosition(0, row).Text;
if (valCtl.GetType() == typeof(CheckBox) && settings[KeyCtl] != null) {
settings[KeyCtl].Value = ((CheckBox)valCtl).Checked.ToString().ToLower();
} else if (valCtl.GetType() == typeof(TextBox) && settings[KeyCtl] != null) {
settings[KeyCtl].Value = ((TextBox)valCtl).Text.ToLower();
}
}
try {
configFile.Save(ConfigurationSaveMode.Modified);
} catch (ConfigurationErrorsException) {
AppendTextBox("Error writing app settings.\r\n");
}
Application.Restart(); Application.Restart();
Environment.Exit(0); Environment.Exit(0);
} }
@ -250,29 +270,31 @@ namespace BetterJoyForCemu {
} }
private void cbBox_Changed(object sender, EventArgs e) { private void cbBox_Changed(object sender, EventArgs e) {
var coord = tableLayoutPanel1.GetPositionFromControl(sender as Control); var coord = settingsTable.GetPositionFromControl(sender as Control);
var valCtl = tableLayoutPanel1.GetControlFromPosition(coord.Column, coord.Row); var valCtl = settingsTable.GetControlFromPosition(coord.Column, coord.Row);
var KeyCtl = tableLayoutPanel1.GetControlFromPosition(coord.Column - 1, coord.Row).Text; var KeyCtl = settingsTable.GetControlFromPosition(coord.Column - 1, coord.Row).Text;
try { try {
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings; var settings = configFile.AppSettings.Settings;
if (sender.GetType() == typeof(CheckBox) && settings[KeyCtl] != null) { if (valCtl.GetType() == typeof(CheckBox) && settings[KeyCtl] != null) {
settings[KeyCtl].Value = ((CheckBox)valCtl).Checked.ToString().ToLower(); settings[KeyCtl].Value = ((CheckBox)valCtl).Checked.ToString().ToLower();
} else if (sender.GetType() == typeof(TextBox) && settings[KeyCtl] != null) { } else if (valCtl.GetType() == typeof(TextBox) && settings[KeyCtl] != null) {
settings[KeyCtl].Value = ((TextBox)valCtl).Text.ToLower(); settings[KeyCtl].Value = ((TextBox)valCtl).Text.ToLower();
} }
if(KeyCtl == "HomeLEDOn") {
bool on = settings[KeyCtl].Value.ToLower() == "true"; if (KeyCtl == "HomeLEDOn") {
foreach(Joycon j in Program.mgr.j) { bool on = settings[KeyCtl].Value.ToLower() == "true";
j.SetHomeLight(on); foreach (Joycon j in Program.mgr.j) {
} j.SetHomeLight(on);
} }
}
configFile.Save(ConfigurationSaveMode.Modified); configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
} catch (ConfigurationErrorsException) { } catch (ConfigurationErrorsException) {
Console.WriteLine("Error writing app settings"); AppendTextBox("Error writing app settings\r\n");
Trace.WriteLine(String.Format("rw {0}, column {1}, {2}, {3}", coord.Row, coord.Column, sender.GetType(), KeyCtl)); Trace.WriteLine(String.Format("rw {0}, column {1}, {2}, {3}", coord.Row, coord.Column, sender.GetType(), KeyCtl));
} }
} }

View file

@ -765,9 +765,6 @@
<metadata name="btnTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="btnTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>247, 17</value> <value>247, 17</value>
</metadata> </metadata>
<metadata name="btnTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>247, 17</value>
</metadata>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>
AAABAAUAEBAAAAEAIABoBAAAVgAAABgYAAABACAAiAkAAL4EAAAgIAAAAQAgAKgQAABGDgAAMDAAAAEA AAABAAUAEBAAAAEAIABoBAAAVgAAABgYAAABACAAiAkAAL4EAAAgIAAAAQAgAKgQAABGDgAAMDAAAAEA