/// <summary> /// A method that loops and processes keystrokes until the form is complete, /// either by <see cref="FormComplete">FormComplete</see> or /// <see cref="FormCancelled">FormCancelled</see> events. /// </summary> private void LoopForKeypress() { // Loop for keypresses. Since we're doing all the work of processing, we have // to trap special keypresses and respond appropriately while (true) { // Blocks on the next function call. ConsoleKeyInfo cki = Console.ReadKey(true); ConsoleKey nKey = cki.Key; // A key's been pressed. Figure out what to do. // All actions will be against the current field, stored in _field. char cChar = cki.KeyChar; if (cChar != 0) { // Guard against unprintable chars. KeyPressEventArgs kpea = new KeyPressEventArgs(_field, cChar); if (_keyPressEvent != null) _keyPressEvent(this, kpea); if (!kpea.Cancel) { // Process the keystroke. It wasn't cancelled. switch (nKey) { case ConsoleKey.Backspace: // Backspace pressed // Is there a character to backspace over? if (_field.Text.Length > 0) { _field.Text = _field.Text.Substring(0, _field.Text.Length - 1); Refresh(_field); } break; case ConsoleKey.Tab: // Tab -> Move to the next field. if (cki.Modifiers == ConsoleModifiers.Shift) { // Go backwards. _currentField--; // If we're at the first field, move to the last. if (_currentField == -1) _currentField = _textboxes.Count - 1; } else { // Go forwards _currentField++; // If we're in the last field already, move back to the first. if (_currentField == _textboxes.Count) _currentField = 0; } // Set the current field to the next one in the collection. _field = _textboxes[_currentField]; _textboxes.FocusField = _field; // Move the cursor to the location of the next field, accomodating // any text that may already be there.. Console.SetCursorPosition(_field.Location.X + _field.Text.Length, _field.Location.Y); break; case ConsoleKey.Enter: // Enter -> Fire the complete event if it's wired. if (_formCompleteEvent != null) { FormCompleteEventArgs fcea = new FormCompleteEventArgs(); _formCompleteEvent(this, fcea); // The listener of this event will set the Cancel field if they // want to re-use the form. If not cancelled, the form will // be destroyed. if (!fcea.Cancel) { return; } // else the current form will be reused. Go back for more keys. } break; case ConsoleKey.Escape: // Esc -> Fire the cancelled event if it's wired. if (this.FormCancelled != null) { this.FormCancelled(this, System.EventArgs.Empty); return; } break; default: // Any other keystroke if (_field != null) { // May not be an active textbox. if (_field.Text.Length < _field.Length) { // The field is not yet full. It can be appended to. _field.NonEventingText += cChar; Console.ForegroundColor = _field.Foreground; Console.BackgroundColor = _field.Background; if (_field.PasswordChar != char.MinValue) // It's a password field. Display the password character. Console.Write(_field.PasswordChar); else // Not a password type field. Show the actual character. Console.Write(cChar); } // Field already full => no keystrokes accepted. } break; } // Keystroke was not cancelled } // Character was printable } // End of switch statement } // End loop for keypresses }
/// <summary> /// A method that loops and processes keystrokes until the form is complete, /// either by <see cref="FormComplete">FormComplete</see> or /// <see cref="FormCancelled">FormCancelled</see> events. /// </summary> private void LoopForKeypress() { // Loop for keypresses. Since we're doing all the work of processing, we have // to trap special keypresses and respond appropriately while (true) { // Blocks on the next function call. ConsoleKeyInfo cki = Console.ReadKey(true); ConsoleKey nKey = cki.Key; // A key's been pressed. Figure out what to do. // All actions will be against the current field, stored in _field. char cChar = cki.KeyChar; if (cChar != 0) { // Guard against unprintable chars. KeyPressEventArgs kpea = new KeyPressEventArgs(_field, cChar); if (_keyPressEvent != null) { _keyPressEvent(this, kpea); } if (!kpea.Cancel) { // Process the keystroke. It wasn't cancelled. switch (nKey) { case ConsoleKey.Backspace: // Backspace pressed // Is there a character to backspace over? if (_field.Text.Length > 0) { _field.Text = _field.Text.Substring(0, _field.Text.Length - 1); Refresh(_field); } break; case ConsoleKey.Tab: // Tab -> Move to the next field. if (cki.Modifiers == ConsoleModifiers.Shift) { // Go backwards. _currentField--; // If we're at the first field, move to the last. if (_currentField == -1) { _currentField = _textboxes.Count - 1; } } else { // Go forwards _currentField++; // If we're in the last field already, move back to the first. if (_currentField == _textboxes.Count) { _currentField = 0; } } // Set the current field to the next one in the collection. _field = _textboxes[_currentField]; _textboxes.FocusField = _field; // Move the cursor to the location of the next field, accomodating // any text that may already be there.. Console.SetCursorPosition(_field.Location.X + _field.Text.Length, _field.Location.Y); break; case ConsoleKey.Enter: // Enter -> Fire the complete event if it's wired. if (_formCompleteEvent != null) { FormCompleteEventArgs fcea = new FormCompleteEventArgs(); _formCompleteEvent(this, fcea); // The listener of this event will set the Cancel field if they // want to re-use the form. If not cancelled, the form will // be destroyed. if (!fcea.Cancel) { return; } // else the current form will be reused. Go back for more keys. } break; case ConsoleKey.Escape: // Esc -> Fire the cancelled event if it's wired. if (this.FormCancelled != null) { this.FormCancelled(this, System.EventArgs.Empty); return; } break; default: // Any other keystroke if (_field != null) { // May not be an active textbox. if (_field.Text.Length < _field.Length) { // The field is not yet full. It can be appended to. _field.NonEventingText += cChar; Console.ForegroundColor = _field.Foreground; Console.BackgroundColor = _field.Background; if (_field.PasswordChar != char.MinValue) { // It's a password field. Display the password character. Console.Write(_field.PasswordChar); } else { // Not a password type field. Show the actual character. Console.Write(cChar); } } // Field already full => no keystrokes accepted. } break; } // Keystroke was not cancelled } // Character was printable } // End of switch statement } // End loop for keypresses }
private static void LoginForm_FormComplete(ConsoleForm sender, FormCompleteEventArgs e) { if (e.Cancel || sender.Textboxes["txtServer"].Text == null) { ConsoleUtils.UOut(ConsoleColor.Yellow, "K, bye!"); return; } LoginResult loginResult = HandleLogin(sender.Textboxes["txtServer"].Text, sender.Textboxes["txtUser"].Text, sender.Textboxes["txtPass"].Text); if (loginResult.Success == false) { DisplayLoginForm(); } else { Console.ResetColor(); Console.Clear(); Console.Out.Flush(); EnterMainLoop(); } }