Skip to main content

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
}

}

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

Popular posts from this blog

How to Create and Run Tableau Bridge on Linux Containers

Tableau Bridge is now availble on Linux Containers. Yay! Now what does this mean and how do I build and run Linux Containers? We will discuss the advantages of running Bridge on Linux Containers the steps to build them, and finally, we will provide some automation script ideas for monitoring and scaling Linux Bridge agents. Tableau Bridge Today Until recently, Tableau Bridge was only available as a Windows application running on a Windows VM. It supported only one bridge agent per Virtual or Physical Machine. Advantages of Bridge in Containers Better Hardware Utilization: Linux containers are more efficient than Windows VMs, requiring only about 1/50th of the disk space. Ability to Spin Up Multiple Bridge Agents: With Linux Containers, it becomes easier to spin up multiple bridge agents on a single machine, improving scalability and resource utilization. Infrastructure Automation: Linux Containers enable easier automation of provisioning bridge agents and upgrading Tableau Bridge, the...

RAM Disks do not speed up Visual Studio

  The limiting factor for Visual Studio is disk IO. I got a tip to speed up Visual Studio from Channel 9 by creating a RAM disk which sounded like a great idea. However, when I ran a thorough set of tests, I found that the performance difference between the Ram disk and the hard disk were not appreciably different. This was a big surprise since RAM is 240,000 times faster than disk (see my previous blog post). But the reason is because Visual Studio and Vista do a lot of caching. So compile times for the same project in RAM disk and on hard disk were pretty similar. I also tested the time it took to search the entire solution for a word, and times to open a solution. There was no discernable difference!   If you still want to try it out and create your own RAM disk, you can download a simple RAMDISK.EXE utility to create a RAM disk in just a few minutes. What is a RAM Disk ?   Ramdisk is a virtual drive created in RAM.   Performance Analysis Creating f...

SpreadsheetGear vs. SyncFusion vs. ComponentOne

I conducted a three month analysis comparing three top spreadsheet controls for .NET application developers: SpreadsheetGear , SyncFusion Spreadsheet , and ComponentOne Spread.NET . The definite winner was SpreadsheetGear, with ComponentOne Spread.net in second. SpreadsheetGear provides the most Excel-like experience, is the most performant, and provides the most responsive and capable product support. I build a reporting engine for my company and SpreadsheetGear made it awesome. Our customers have been very pleased. I wanted to share my extensive analysis with any other developers out there trying to find the right spreadsheet control for their .NET application.   Comparison Spreadsheet gear grid goes to 1 million! Wow. (SyncFusion goes to 500 by default, ComponentOne goes to 60k) SpreadsheetGear is an older control which has been around for a while. It is built by the same guys who built Formula1, so they have been doing spreadsheets for many years. Sp...