using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace FCC_Uber_MDX_Configurator { public partial class AddVariableLocationImageForm : Form { public GlobalStaticClass.FusionImage_VariableLocationImage CurrentVariableLocationImage = new GlobalStaticClass.FusionImage_VariableLocationImage(); public AddVariableLocationImageForm() { SetUp(); } public AddVariableLocationImageForm(GlobalStaticClass.FusionImage_VariableLocationImage inputVariableLocationImage) { CurrentVariableLocationImage = inputVariableLocationImage; SetUp(); COMBO_MONITORTYPE.Text = CurrentVariableLocationImage._target; COMBO_MONITOR_ID.Text = CurrentVariableLocationImage._targetID; for (int i = 0; i < COMBO_MONITORTYPE.Items.Count; i++) { if (COMBO_MONITORTYPE.Items[i].ToString().ToLower() == CurrentVariableLocationImage._target.ToLower()) { COMBO_MONITORTYPE.SelectedIndex = i; break; } } COMBO_MONITOR_TYPE_SelectedValueChanged(COMBO_MONITORTYPE, null); COMBO_MONITOR_ID.Enabled = true; COMBO_MONITOR_ID.Text = CurrentVariableLocationImage._targetID; COMBO_MONITOR_ID.SelectedText = CurrentVariableLocationImage._targetID; string _axis = "Horizontal"; switch(CurrentVariableLocationImage._translateAround) { case GlobalStaticClass.Axis.HORIZONTAL: _axis = "Horizontal"; break; case GlobalStaticClass.Axis.VERTICAL: _axis = "Vertical"; break; default: break; } COMBO_AXIS.SelectedText = _axis; COMBO_AXIS.Text = _axis; for (int i = 0; i < COMBO_MONITOR_ID.Items.Count; i++) { if (COMBO_MONITOR_ID.Items[i].ToString().ToLower() == CurrentVariableLocationImage._targetID.ToLower()) { COMBO_MONITOR_ID.SelectedIndex = i; break; } } for (int i = 0; i < CHECKLIST_PAGES.Items.Count; i++) { int _pageParse = -1; if (int.TryParse(CHECKLIST_PAGES.Items[i].ToString(), out _pageParse)) { CHECKLIST_PAGES.SetItemChecked(i, CurrentVariableLocationImage._pages.Contains(_pageParse - 1)); } } UpdateForm(); } private void SetUp() { InitializeComponent(); CHECKLIST_PAGES.Items.Clear(); for (int i = 0; i < GlobalStaticClass._Global_List_Graphics_PageList.Count; i++) { CHECKLIST_PAGES.Items.Add(((int)(i + 1)).ToString()); } } private void BTN_SAVE_Click(object sender, EventArgs e) { if (CurrentVariableLocationImage == null) { (new ErrorForm("Invalid Variable Location Image", "Image has not been fully setup")).ShowDialog(); return; } int loc_x = 0, loc_y = 0, width = 0, height = 0; float z = 0.0f; float min = 0, max = 0; if (!int.TryParse(TXT_LOCATION_X.Text, out loc_x) || !int.TryParse(TXT_LOCATION_Y.Text, out loc_y) || !int.TryParse(TXT_SHOWSIZE_HEIGHT.Text, out height) || !int.TryParse(TXT_SHOWSIZE_WIDTH.Text, out width) || !float.TryParse(TXT_ZORDER.Text, out z)) { (new ErrorForm("Invalid Number", "A numeric input is invalid. Make sure that the location, size, and z-order inputs contain numbers only.")).ShowDialog(); return; } if (!float.TryParse(TXT_VALUE_MIN.Text, out min) || !float.TryParse(TXT_VALUE_MAX.Text, out max)) { (new ErrorForm("Invalid Number", "Invalid Minimum and/or Maximum value in text boxes")).ShowDialog(); return; } if (min >= max) { (new ErrorForm("Invalid Min/Max Value", "Minimum cannot be greater than or equal to Maximum. Minimum must be less")).ShowDialog(); return; } else { CurrentVariableLocationImage._value_minimum = min; CurrentVariableLocationImage._value_maximum = max; } if (!int.TryParse(TXT_LOCATION_MIN.Text, out CurrentVariableLocationImage._location_minimum) || !int.TryParse(TXT_LOCATION_MAX.Text, out CurrentVariableLocationImage._location_maximum)) { (new ErrorForm("Invalid Min/Max Location", "Cannot parse minimum and/or maximum location")).ShowDialog(); return; } string _monitorType = COMBO_MONITORTYPE.Text; string _axis = COMBO_AXIS.Text; List _possibleTypes = new List(new string[] {"variable", "analogue input"}); List _possibleAxis = new List(new string[] { "horizontal", "vertical" }); if (_possibleTypes.Contains(_monitorType.ToLower())) { CurrentVariableLocationImage._target = _monitorType; } else { (new ErrorForm("Invalid Target Type", "Target type is invalid")).ShowDialog(); return; } if (_possibleAxis.Contains(_axis.ToLower())) { switch (_axis.ToLower()) { case "horizontal": CurrentVariableLocationImage._translateAround = GlobalStaticClass.Axis.HORIZONTAL; break; case "vertical": CurrentVariableLocationImage._translateAround = GlobalStaticClass.Axis.VERTICAL; break; default: break; } } else { (new ErrorForm("Invalid Axis Type", "Target axis is invalid")).ShowDialog(); return; } bool isFound = false; string _monitorID = COMBO_MONITOR_ID.Text; switch (_monitorType.ToLower()) { case "variable": foreach (GlobalStaticClass._GlobalVariableClass _var in GlobalStaticClass._Global_List_Variables) { if (_var._id.ToLower() == _monitorID.ToLower()) { isFound = true; break; } } break; case "analogue input": foreach (GlobalStaticClass._GlobalAnalogueInputClass _ain in GlobalStaticClass._Global_List_AnalogueInputs) { if (_ain.id.ToLower() == _monitorID.ToLower()) { isFound = true; break; } } break; default: break; } if (isFound) { CurrentVariableLocationImage._targetID = _monitorID; } else { (new ErrorForm("Invalid Target ID", "The ID of the " + CurrentVariableLocationImage._target + " that you want to monitor is invalid")).ShowDialog(); return; } if (!(CHECKLIST_PAGES.CheckedIndices.Count > 0)) { (new ErrorForm("Invalid Page", "There must be at least 1 visible page selected.")).ShowDialog(); return; } List tempPages = new List(); for (int i = 0; i < CHECKLIST_PAGES.CheckedItems.Count; i++) { int _tempI = -1; if (int.TryParse(CHECKLIST_PAGES.CheckedItems[i].ToString(), out _tempI)) { tempPages.Add((int)(_tempI - 1)); } } CurrentVariableLocationImage._x = loc_x; CurrentVariableLocationImage._y = loc_y; CurrentVariableLocationImage._width = width; CurrentVariableLocationImage._height = height; CurrentVariableLocationImage._z_order = z; CurrentVariableLocationImage._pages = new List(tempPages.ToArray()); this.DialogResult = DialogResult.OK; } private void BTN_CANCEL_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } private void COMBO_MONITOR_TYPE_SelectedValueChanged(object sender, EventArgs e) { COMBO_MONITOR_ID.Items.Clear(); COMBO_MONITOR_ID.SelectedText = ""; COMBO_MONITOR_ID.Enabled = false; ComboBox _combo = (ComboBox)sender; if (_combo.SelectedItem == null || _combo.SelectedItem.ToString() == "") { return; } List _possibilites = new List(); switch (_combo.SelectedItem.ToString().ToLower()) { case "variable": { foreach (GlobalStaticClass._GlobalVariableClass _var in GlobalStaticClass._Global_List_Variables) { _possibilites.Add(_var._id); } } break; case "analogue input": { foreach (GlobalStaticClass._GlobalAnalogueInputClass _ain in GlobalStaticClass._Global_List_AnalogueInputs) { _possibilites.Add(_ain.id); } } break; default: break; } if (_possibilites.Count > 0) { COMBO_MONITOR_ID.Enabled = true; COMBO_MONITOR_ID.Items.AddRange(_possibilites.ToArray()); COMBO_MONITOR_ID.Text = ""; COMBO_MONITOR_ID.Text = _possibilites[0]; } } #region Verify Textboxes Only get Number Inputs private void KeyDown_IsANumber(object sender, KeyEventArgs e) { if (!char.IsDigit((char)e.KeyValue) && e.KeyData != Keys.Back && e.KeyData != Keys.Left && e.KeyData != Keys.Right && e.KeyData != Keys.Decimal && e.KeyValue != 190 && !e.KeyCode.Equals(Keys.NumPad0) && !e.KeyCode.Equals(Keys.NumPad1) && !e.KeyCode.Equals(Keys.NumPad2) && !e.KeyCode.Equals(Keys.NumPad3) && !e.KeyCode.Equals(Keys.NumPad4) && !e.KeyCode.Equals(Keys.NumPad5) && !e.KeyCode.Equals(Keys.NumPad6) && !e.KeyCode.Equals(Keys.NumPad7) && !e.KeyCode.Equals(Keys.NumPad8) && !e.KeyCode.Equals(Keys.NumPad9)) { e.SuppressKeyPress = true; } if (e.KeyData == Keys.Decimal || e.KeyValue == 190) { if (sender == TXT_ZORDER || sender == TXT_VALUE_MIN || sender == TXT_VALUE_MAX) { if (((TextBox)sender).Text.Contains(".")) { e.SuppressKeyPress = true; } } else { e.SuppressKeyPress = true; } } return; } #endregion #region Verify a Number is entered in TextBox private void Leave_VerifyTXTIsValid(object sender, EventArgs e) { try { TextBox _senderBox = (TextBox)sender; double _tempDbl; if (!double.TryParse(_senderBox.Text, out _tempDbl)) { ErrorForm _tempErrorForm = new ErrorForm(_senderBox.Name, "The value you entered is not valid"); _tempErrorForm.ShowDialog(); _senderBox.Focus(); } } catch (Exception) { ErrorForm _tempErrorForm = new ErrorForm("Unknown Error", "There was an unexpected untraceable error caught in the application. If you can reproduce this error, please let me know at FusionControlCentre@gmail.com"); _tempErrorForm.ShowDialog(); } } #endregion #region Verify a Proper Item has Been Selected in Combo Box private void Leave_VerifyCOMBOIsValid(object sender, EventArgs e) { try { ComboBox _senderBox = (ComboBox)sender; if (!_senderBox.Items.Contains(_senderBox.Text)) { (new ErrorForm(_senderBox.Name, "The value you selected or entered is not valid")).ShowDialog(); _senderBox.Focus(); _senderBox.SelectedIndex = 0; } } catch (Exception) { ErrorForm _tempErrorForm = new ErrorForm("Unknown Error", "There was an unexpected untraceable error caught in the application. If you can reproduce this error, please let me know at FusionControlCentre@gmail.com"); _tempErrorForm.ShowDialog(); } } #endregion private void BTN_LOADIMAGE_Click(object sender, EventArgs e) { AddImageForm _tempAIF = null; if (CurrentVariableLocationImage != null) { if (CurrentVariableLocationImage._image != null && CurrentVariableLocationImage._path != null) { _tempAIF = new AddImageForm(CurrentVariableLocationImage._image, CurrentVariableLocationImage._path); } else { _tempAIF = new AddImageForm(); } } else { _tempAIF = new AddImageForm(); } if (_tempAIF.ShowDialog() == DialogResult.OK) { this.DialogResult = DialogResult.None; if (CurrentVariableLocationImage == null) { CurrentVariableLocationImage = new GlobalStaticClass.FusionImage_VariableLocationImage(); } CurrentVariableLocationImage._image = _tempAIF.IMAGE_BITMAP; CurrentVariableLocationImage._path = _tempAIF.IMAGE_PATH; UpdateFormImageOnly(); } this.DialogResult = DialogResult.None; } private void UpdateFormImageOnly() { if (CurrentVariableLocationImage._image.Size.Width > PICT_PREVIEW.Size.Width || CurrentVariableLocationImage._image.Size.Height > PICT_PREVIEW.Size.Height) { PICT_PREVIEW.BackgroundImageLayout = ImageLayout.Zoom; } else { PICT_PREVIEW.BackgroundImageLayout = ImageLayout.Center; } PICT_PREVIEW.BackgroundImage = CurrentVariableLocationImage._image; TXT_ORIGINALSIZE_WIDTH.Text = CurrentVariableLocationImage._image.Width.ToString(); TXT_ORIGINALSIZE_HEIGHT.Text = CurrentVariableLocationImage._image.Height.ToString(); int show_x, show_y; TXT_SHOWSIZE_WIDTH.Text = CurrentVariableLocationImage._width.ToString(); TXT_SHOWSIZE_HEIGHT.Text = CurrentVariableLocationImage._height.ToString(); if (int.TryParse(TXT_SHOWSIZE_WIDTH.Text, out show_x) && int.TryParse(TXT_SHOWSIZE_HEIGHT.Text, out show_y)) { if (show_x == 0 && show_y == 0) { TXT_SHOWSIZE_WIDTH.Text = CurrentVariableLocationImage._image.Width.ToString(); TXT_SHOWSIZE_HEIGHT.Text = CurrentVariableLocationImage._image.Height.ToString(); } } else { TXT_SHOWSIZE_WIDTH.Text = CurrentVariableLocationImage._image.Width.ToString(); TXT_SHOWSIZE_HEIGHT.Text = CurrentVariableLocationImage._image.Height.ToString(); } } private void UpdateForm() { TXT_ORIGINALSIZE_WIDTH.Text = "???"; TXT_ORIGINALSIZE_HEIGHT.Text = "???"; if (CurrentVariableLocationImage == null) { return; } if (CurrentVariableLocationImage._image == null) { return; } UpdateFormImageOnly(); TXT_LOCATION_X.Text = CurrentVariableLocationImage._x.ToString(); TXT_LOCATION_Y.Text = CurrentVariableLocationImage._y.ToString(); TXT_ZORDER.Text = CurrentVariableLocationImage._z_order.ToString(); TXT_VALUE_MIN.Text = CurrentVariableLocationImage._value_minimum.ToString(); TXT_VALUE_MAX.Text = CurrentVariableLocationImage._value_maximum.ToString(); TXT_LOCATION_MIN.Text = CurrentVariableLocationImage._location_minimum.ToString(); TXT_LOCATION_MAX.Text = CurrentVariableLocationImage._location_maximum.ToString(); BTN_SAVE.Enabled = true; } } }