private void btnSave_Click(object sender, EventArgs e) { // gather all user selected keys and look up in dictionary to get the int value // add int to list to make a user list saved as a string // put string and name of string into preset object and save to db try { List <int> saveUsersSelectedKeys = new List <int>(); foreach (CheckBox cb in gbKeyBoard.Controls.OfType <CheckBox>()) { if (cb.CheckState == CheckState.Checked) { foreach (KeyValuePair <int, CheckBox> keyString in numberedCheckBoxNames) { // look up checkbox name in Dictionary and add number to list if (cb == keyString.Value) { saveUsersSelectedKeys.Add(keyString.Key); } } } else if (cb.CheckState == CheckState.Indeterminate) { foreach (KeyValuePair <int, CheckBox> keyString in numberedCheckBoxNamesStateIndeterminate) { // look up tristate indeterminate checkbox name in Dictionary and add number to list if (cb == keyString.Value) { saveUsersSelectedKeys.Add(keyString.Key); } } } } string myPresetList = ""; string name = cbPreset.Text; foreach (int keyNum in saveUsersSelectedKeys) { myPresetList += keyNum.ToString() + " "; } if (string.IsNullOrEmpty(name) || (string.IsNullOrEmpty(myPresetList))) { throw new ApplicationException(); // to ensure that blank presets are not saved } Preset newPreset = new Preset(name, myPresetList); PresetDBHelper.connectToDatabase(); PresetDBHelper.InsertPreset(newPreset); refreshPresetComboBox(); MessageBox.Show("New Preset Created "); } catch (Exception exception) { MessageBox.Show("Saving preset requires a name and keys selected "); } }
private void btnSave_Click(object sender, EventArgs e) { // gather all user selected keys and look up in dictionary to get the int value // add int to list to make a user list saved as a string // put string and name of string into preset object and save to db try { List <int> usersSelectedKeys = new List <int>(); foreach (CheckBox cb in pnCheckBoxes.Controls.OfType <CheckBox>()) { if (cb.Checked == true) { string cbParse = cb.Name; cbParse = cbParse.Remove(0, 2); int cbNumber = Convert.ToInt16(cbParse); usersSelectedKeys.Add(cbNumber); } } string myPresetList = ""; string name = cbPreset.Text; foreach (int keyNum in usersSelectedKeys) { myPresetList += keyNum.ToString() + " "; } if (string.IsNullOrEmpty(name) || (string.IsNullOrEmpty(myPresetList))) { throw new ApplicationException(); // to ensure that blank presets are not saved } Preset newPreset = new Preset(name, myPresetList); PresetDBHelper.ConnectToDatabase(); PresetDBHelper.InsertPreset(newPreset); RefreshPresetComboBox(); MessageBox.Show("New Preset Created "); } catch (Exception exception) { MessageBox.Show("Saving preset requires a name and keys selected "); } }