Saturday, October 29, 2011

SmartStepsUtil - A Utility Class for WinForms

I’ve been using a utility class I wrote called “SmartStepsUtil” since 2007.  I’ve decided to share it with the world. It turns your WinForms app into a “Console” simply helping you to write colored text to a RichTextBox.

image

I’ve used SmartStepsUtil on a few applications including SharePoint SUSHI. It makes it easy to quickly give feedback to the user about what is going on. It is thread safe, so you can call it from a background thread. Feel free to change it as needed.

It just amazes me how many applications don’t give feedback to the user. Or they have complex UIs with textboxes all over the place. SmartSteps util brings the best of a Console application (simple UI, live feedback) into a Winforms app. To use SmartStepsUtil, copy into your application

Methods:

- AddtoRichTextBox()
- ScrollToBottom()
- ClearRichTextBox()

Preset Styles to choose from to write to console:

public enum StyleType
{
    bodyBlack,
    bodyBlackBold,
    bodyBlue,
    bodyBlueBold,
    bodyChocolate,
    bodyChocolateBold,
    bodyDarkGray,

     …

Applications I’ve written using SmartStepsUtil:

SharePoint SUSHI

SNAGHTML55ade1a

BearWarePro Load Tester (affectionately called “The Punisher” Smile)

 

Source for SmartStepsUtil:

/*
Written by Joseph Fluckiger, 2007
Home page: http://josephfluckiger.blogspot.com/
*/
using System;
using System.Drawing;
using System.Windows.Forms;

namespace SmartStepsUtility
{
public static class SmartStepUtil
{
/// <summary>
/// Singleton Reference to RichTextBoxInstance. Set this Instance on application startup.
/// </summary>
public static RichTextBox RichTextBoxInstance { get; set; }

/// <summary>
/// Add to RichTextBox with Style
/// </summary>
public static void AddToRichTextBox(string strText, StyleType style)
{
AddToRichTextBox(strText, style, EnumIcon.no_icon);
}

private static void AddToRichTextBox(string strText, StyleType style, EnumIcon icon)
{
switch (style)
{
case StyleType.bodyBlack:
AddToRichTextBox(strText, Color.Black, 8, false, icon);
break;
case StyleType.bodyBlackBold:
AddToRichTextBox(strText, Color.Black, 8, true, icon);
break;
case StyleType.bodyBlue:
AddToRichTextBox(strText, Color.Blue, 8, false, icon);
break;
case StyleType.bodyBlueBold:
AddToRichTextBox(strText, Color.Blue, 8, true, icon);
break;
case StyleType.bodyChocolate:
AddToRichTextBox(strText, Color.Chocolate, 8, false, icon);
break;
case StyleType.bodyChocolateBold:
AddToRichTextBox(strText, Color.Chocolate, 8, true, icon);
break;
case StyleType.bodyDarkGray:
AddToRichTextBox(strText, Color.DarkGray, 8, false, icon);
break;
case StyleType.bodyOrange:
AddToRichTextBox(strText, Color.Orange, 8, false, icon);
break;
case StyleType.bodyRed:
AddToRichTextBox(strText, Color.Red, 8, false, icon);
break;
case StyleType.bodySeaGreen:
AddToRichTextBox(strText, Color.SeaGreen, 8, false, icon);
break;
case StyleType.bodySeaGreenBold:
AddToRichTextBox(strText, Color.SeaGreen, 8, true, icon);
break;
case StyleType.titleBlack:
AddToRichTextBox(strText, Color.Black, 14, true, icon);
break;
case StyleType.titleBlue:
AddToRichTextBox(strText, Color.Blue, 14, true, icon);
break;
case StyleType.titleChocolate:
AddToRichTextBox(strText, Color.Chocolate, 14, true, icon);
break;
case StyleType.titleSeagreen:
AddToRichTextBox(strText, Color.SeaGreen, 14, true, icon);
break;
default:
AddToRichTextBox(strText, Color.Black, 8, false, icon);
break;
}
}

public static void AddToRichTextBox(string strText)
{
AddToRichTextBox(strText, Color.Black, 8, false, EnumIcon.no_icon);
}

public static void AddToRTBnl(string strText)
{
AddToRichTextBox(strText + "\r\n", Color.Black, 8, false, EnumIcon.no_icon);
}

public static void AddToRichTextBox(string strText, Color textColor, float fontSize, bool bold)
{
AddToRichTextBox(strText, textColor, fontSize, bold, EnumIcon.no_icon);
}

public static void AddToRichTextBox(Exception ex)
{
AddToRichTextBox(ex + "\r\n", StyleType.bodyRed, EnumIcon.no_icon);
}

/// <summary>
/// Delagate for thread safely adding to Richtextbox
/// </summary>
private delegate void delAddToRTB(string strText, Color color, float fontSize, bool bold, EnumIcon icon);

/// <summary>
/// Threadsafe call to add to Richtexbox
/// </summary>
public static void AddToRichTextBox(string strText, Color textColor, float fontSize, bool bold, EnumIcon icon)
{
if (RichTextBoxInstance.InvokeRequired)
{
RichTextBoxInstance.BeginInvoke(new delAddToRTB(AddToRichTextBox), new object[] { strText, textColor, fontSize, bold, icon });
return;
}
FontStyle style1 = bold ? FontStyle.Bold : FontStyle.Regular;
if (fontSize <= 0)
{
fontSize = 8;
}
var font1 = new Font("Courier New", fontSize, style1, GraphicsUnit.Point, 0);
RichTextBoxInstance.SelectionStart = RichTextBoxInstance.TextLength;
RichTextBoxInstance.SelectionFont = font1;
RichTextBoxInstance.SelectionColor = textColor;
RichTextBoxInstance.SelectedText = strText;
AddIcon(icon);
}

public static void ScrollToBottom()
{
if (RichTextBoxInstance.InvokeRequired)
{
RichTextBoxInstance.BeginInvoke(new Action(ScrollToBottom));
return;
}
RichTextBoxInstance.Focus();
RichTextBoxInstance.Select(RichTextBoxInstance.Text.Length, 0);
RichTextBoxInstance.ScrollToCaret();
}

public static void ClearRichTextBox()
{
if (RichTextBoxInstance.InvokeRequired)
{
RichTextBoxInstance.BeginInvoke(new Action(ClearRichTextBox));
return;
}
RichTextBoxInstance.Clear();
}

private static void AddIcon(EnumIcon icon)
{
if (icon != EnumIcon.no_icon)
{
var font1 = new Font("Wingdings", 10f, FontStyle.Regular, GraphicsUnit.Point, 0);
RichTextBoxInstance.SelectionStart = RichTextBoxInstance.TextLength;
RichTextBoxInstance.SelectionFont = font1;
switch (icon)
{
case EnumIcon.brown_arrow:
RichTextBoxInstance.SelectionColor = Color.Brown;
RichTextBoxInstance.SelectedText = AsciiToStringUtil(182);
break;
case EnumIcon.green_check:
RichTextBoxInstance.SelectionColor = Color.Green;
RichTextBoxInstance.SelectedText = AsciiToStringUtil(252);
break;
case EnumIcon.orange_dotdotdot:
RichTextBoxInstance.SelectionColor = Color.Orange;
RichTextBoxInstance.SelectedText = AsciiToStringUtil(160) + AsciiToStringUtil(160) + AsciiToStringUtil(160);
break;
case EnumIcon.red_x:
RichTextBoxInstance.SelectionColor = Color.Red;
RichTextBoxInstance.SelectedText = AsciiToStringUtil(251);
break;
}
}
}

private static string AsciiToStringUtil(int asciiCode)
{
return ((char)asciiCode).ToString();
}

public enum EnumIcon
{
brown_arrow,
green_check,
no_icon,
orange_dotdotdot,
red_x
}

}

public enum StyleType
{
bodyBlack,
bodyBlackBold,
bodyBlue,
bodyBlueBold,
bodyChocolate,
bodyChocolateBold,
bodyDarkGray,
bodyOrange,
bodyRed,
bodySeaGreen,
bodySeaGreenBold,
titleChocolate,
titleSeagreen,
titleBlue,
titleBlack
}

}

2 comments:

Bob C said...

Joseph

I use your Sushi tool extensively for its abilities to change the photo path in Sharepoint 2010. Thank you so much for this - I've tried several other ways of doing it but none work like this tool.
(You can probably anticipate what is coming next...). I am now converting to Sharepoint 2013 as our college likes to keep up to date. However, Sushi looks at the new site and says it's not a Sharepoint site. Maybe Sushi looks in 14 folder rather than 15? If you have any plans to upgrade I would be very happy to act as your tester!!!

Regards
Bob Cummings

vincentadell said...

Windows Forms labels class