/// <summary> /// Initializes a new instance of the <see cref="FrmEditEnvVar"/> class. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="variableType">Type of the variable.</param> public FrmEditEnvVar( string variableName, EnvironmentVariableTarget variableType) { this.InitializeComponent(); this.MinimumSize = new Size(327, 439); dgvValuesList.TabIndex = 0; this.LoadSettings(); txtVariableName.CausesValidation = false; this.dgvHandler = new DgvHandler(ref dgvValuesList); // set default icon this.DgvValuesList_UserAddedRow(null, null); this.txtVariableName.Text = variableName; // remember current name this.variableName = variableName; this.variableType = variableType; this.validator = new EnvVarValueValidator(); if (txtVariableName.Text.Length != 0) { // Check if we are editing variable this.LoadEnvironmentVariableValues(); } // Set form title this.Text = (txtVariableName.Text.Length != 0 ? "Edit" : "New") + " " + (this.variableType == EnvironmentVariableTarget.Machine ? "System" : "User") + " Variable"; this.commandsList = new UndoRedoCommandList(); this.dgvHandler.SetCurrentCell(0); this.editVarNameCommand = new EditVarNameCommand(txtVariableName); // disable buttons this.SetBtnState(); txtVariableName.CausesValidation = true; this.isVarNameChanged = false; // Open/Save File dialogs openFileDialog.Filter = FileFilter; openFileDialog.DefaultExt = DefaultFilterExtension; saveFileDialog.Filter = FileFilter; saveFileDialog.DefaultExt = DefaultFilterExtension; }
/// <summary> /// Loads the environment variables. /// </summary> /// <param name="dgv">The Data Grid View.</param> /// <param name="target">The target.</param> private void LoadEnvironmentVariables( DataGridView dgv, EnvironmentVariableTarget target) { EnvVarValueValidator validator = new EnvVarValueValidator(); int currentRowIndex = dgv.CurrentRow != null ? dgv.CurrentRow.Index : 0; dgv.Rows.Clear(); int rowIndex = 0; IDictionary environmentVariables = this.variableManger.GetEnvVariables(target); foreach (DictionaryEntry de in environmentVariables) { string[] row = { de.Key.ToString(), de.Value.ToString() }; rowIndex = dgv.Rows.Add(row); // validate variable value and show row in red if invalid if (!validator.Validate(de.Value.ToString())) { dgv.Rows[rowIndex].Cells[0].Style.ForeColor = Color.Red; dgv.Rows[rowIndex].Cells[1].Style.ForeColor = Color.Red; } } dgv.Sort(dgv.Columns[0], ListSortDirection.Ascending); try { dgv.CurrentCell = dgv[0, currentRowIndex]; dgv.FirstDisplayedScrollingRowIndex = currentRowIndex; } catch { // if row was deleted this will set it to first one // TODO: Implement this by searching for var name in the grid. // Catching Exceptions makes program slow if (dgv.Rows.Count != 0) { dgv.CurrentCell = dgv[0, 0]; dgv.FirstDisplayedScrollingRowIndex = 0; } } }
/// <summary> /// Locates the in windows explorer. /// </summary> private void LocateInWindowsExplorer() { EnvVarValueValidator validator = new EnvVarValueValidator(); string varValue = string.Empty; foreach (DataGridViewRow row in dgvValuesList.SelectedRows) { if (row.Index != dgvValuesList.Rows.Count - 1) { DataGridViewCell cell = row.Cells[1]; varValue = cell.Value.ToString().Contains("%") ? cell.ToolTipText : cell.Value.ToString(); switch (validator.ValueType(varValue)) { case EnvironmentValueType.Folder: { // Open Folder in Windows Explorer this.LocateInWindowsExplorer(varValue); } break; case EnvironmentValueType.File: { // Select File in Windows Explorer this.LocateInWindowsExplorer( "/select," + varValue); } break; case EnvironmentValueType.Error: { // Select existing folder in the path string parentDir = Path.GetDirectoryName(varValue); while (!Directory.Exists(parentDir)) { parentDir = Path.GetDirectoryName(parentDir); } // Open Folder in Explorer this.LocateInWindowsExplorer(parentDir); } break; default: { // TODO: Remove for multiple rows MessageBox.Show( "Nothing to locate in Windows Explorer", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } break; } } } }
public void SetUp() { this.validator = new EnvVarValueValidator(); }