Last active
July 19, 2018 18:48
-
-
Save lolp1/0ca360ef38af65bb3d80fff161564e96 to your computer and use it in GitHub Desktop.
Debug helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FormConsole : IDisposable | |
{ | |
private readonly Form _form; | |
public FormConsole() | |
{ | |
_form = new Form(); | |
var textBox = new RichTextBox {Name = "DebugFormTextBox"}; | |
_form.Controls.Add(textBox); | |
textBox.Dock = DockStyle.Fill; | |
_form.Show(); | |
} | |
public void Dispose() | |
{ | |
_form?.Dispose(); | |
} | |
public Form GetForm() | |
{ | |
return _form; | |
} | |
public void WriteLine(string line, params object[] args) | |
{ | |
var box = _form.Controls["DebugFormTextBox"] as RichTextBox; | |
box?.AppendMessage(line, box.ForeColor, args); | |
} | |
public void WriteLine(string line, Color color) | |
{ | |
var box = _form.Controls["DebugFormTextBox"] as RichTextBox; | |
box?.AppendMessage(line, color); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MessageBoxEx | |
{ | |
public static void ShowException(Exception exception, string caption) | |
{ | |
ShowError(exception.Message, caption); | |
} | |
public static void ShowInfo(string info) | |
{ | |
ShowInfo(info, "Information"); | |
} | |
public static void ShowInfo(string info, string caption) | |
{ | |
MessageBox.Show(info, caption, MessageBoxButtons.OK, MessageBoxIcon.Information); | |
} | |
public static void ShowError(string error) | |
{ | |
ShowError(error, "Error"); | |
} | |
public static void ShowError(string error, string caption) | |
{ | |
MessageBox.Show(error, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); | |
} | |
public static DialogResult Confirm(string question) | |
{ | |
return MessageBox.Show(question, @"Confirm", | |
MessageBoxButtons.YesNo, MessageBoxIcon.Question, | |
MessageBoxDefaultButton.Button2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// A class providing extension methods for Windows Forms rich text box objects. | |
/// </summary> | |
public static class RichTextBoxExtensions | |
{ | |
/// <summary> | |
/// Appends the message and ends with a new line (invoking if required) to the rich text box. | |
/// </summary> | |
/// <param name="box">The box to append the message to.</param> | |
/// <param name="text">The text to append.</param> | |
/// <param name="color">The color of the text to be appended.</param> | |
/// <param name="args">The arguments, if any for string format..</param> | |
public static void AppendMessage(this RichTextBox box, string text, Color color, params object[] args) | |
{ | |
if (box.InvokeRequired) | |
box.InvokeAppendLine(text, color, args); | |
else | |
box.AppendLine(text, color, args); | |
} | |
/// <summary> | |
/// Appends the message (and does not end with a new line) invoking if required to the rich text box. | |
/// </summary> | |
/// <param name="box">The box to append the message to.</param> | |
/// <param name="text">The text to append.</param> | |
/// <param name="color">The color of the text to be appended.</param> | |
/// <param name="args">The arguments, if any for string format..</param> | |
public static void AppendTextEx(this RichTextBox box, string text, Color color, params object[] args) | |
{ | |
if (box.InvokeRequired) | |
box.InvokeAppendText(text, color, args); | |
else | |
box.AppendText(text, color, args); | |
} | |
private static void AppendText(this RichTextBox box, string text, Color color, params object[] args) | |
{ | |
text = string.Format(text, args); | |
if (color == Color.Empty) | |
{ | |
box.AppendText(text); | |
return; | |
} | |
box.SelectionStart = box.TextLength; | |
box.SelectionLength = 0; | |
box.SelectionColor = color; | |
box.AppendText(text); | |
box.SelectionColor = box.ForeColor; | |
box.SelectionStart = box.TextLength; | |
box.ScrollToCaret(); | |
} | |
private static void AppendLine(this RichTextBox box, string text, Color color, params object[] args) | |
{ | |
box.AppendText("\n" + text, color == Color.Empty ? box.ForeColor : color, args); | |
} | |
private static void InvokeAppendText(this RichTextBox box, string text, Color color, params object[] args) | |
{ | |
box.Invoke((MethodInvoker)delegate { box.AppendText(text, color, args); }); | |
} | |
private static void InvokeAppendLine(this RichTextBox box, string text, Color color, params object[] args) | |
{ | |
box.Invoke((MethodInvoker)delegate { box.AppendLine(text, color, args); }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment