Sunday, February 1, 2015

Introducing PowerForms for creating windows form quickly

PowerForms is a little nuget package which allows you to create windows forms quickly either in a LinqPad query or in a console application for rapid data collection.

Before going into details of what PowerForms does let me tell you how it came into existence and what problem I was facing that PowerForms solves. Every once in a while I had to write certain queries either for debugging purposes or testing different scenarios that require different data inputs. After I wrote them I had to use them again and again with different input data which at first I had hardcoded.  For example, in a where clause WHERE lastname = ‘John’.  When you write a sql query in SQL Developer, LinqPad or SQL management studio you have to replace the text and run. This is a sample inside LinqPad using AdventureWorks data.

var person = Persons.Where(x => x.FirstName == "Mike" && x.LastName == "Choi").FirstOrDefault().Dump("Mike");
person.EmailAddresses.Dump("Email Addresses");
person.PersonPhones.Dump("Phones");
person.PersonCreditCards.Dump("Credit Cards");

I want to find a person by firstname of Mike and lastname of Choi. I am interested into other details of this person which I can quickly display once I get person object. This is why LinqPad is a very powerful software. Now some other time you might be interested into another person and I don’t want to replace the text. I don’t to put anything in the query.  I want a form into which I can quickly supply details and I can see details into LinqPad using our favorite Dump method.  With SQL Developer and SQL Management Studio you can customize. Hence I experimented with displaying a windows form from LinqPad. 


“Oh wait. You said Windows Form!! Really?” Yes my friend. A windows form from within LinqPad.


“Wow”. 


LinqPad is very powerful and it has become my daily companion for quick debugging, snippet evaluation, rapid prototyping and more. Sorry LinqPad some other day. This post is for PowerForms.


With PowerForms you can quickly create a form that will allow you to collect data as shown in the image below. I want to collect FirstName and LastName and the above query uses that FirstName and LastName.


image


So how to use it and where to get it. You can get PowerForms as a nuget package and add it to either LinqPad or even your console application. Primarily I intended it to be used inside of LinqPad but after I realized that it could be used inside console application just fine. And there are advantages to that too which I have mentioned below. Here is a quick way of creating a form shown in the above image using a fluent syntax.

var form = new PowerForm();
var fd = form.TextBoxFor("FirstName").TextBoxFor("LastName").Display();
var firstName = fd["FirstName"].ToString().Dump("Firstname");
var lastName = fd["LastName"].ToString().Dump("LastName");

image


Let’s dive into the details of the above example. First we create a new PowerForm() and on the form object we can specify what we want in our form. In this case, we specify we want TextBox for “FirstName” and then another one for “LastName” and then display the form using .Display() method. The .Display() method returns a dictionary of type string and object. The values you provide in the form are stored inside Dictionary<string,object>. To retrieve FirstName we would do fd[“FirstName”].ToString();.


Now some examples and fun.


1. Create a single TextBox using TextBoxFor

 var dt = form.TextBoxFor("FirstName").Display();

2. Create multiple TextBoxes using different syntaxes

 var dt = form.TextBoxFor(new string[]{"FirstName","LastName"}).Display();
//or

var fd = form.TextBoxFor("FirstName")
.TextBoxFor("LastName")
.Display();

3. Display a Calendar Control for selecting Date

  var dt = form.TextBoxFor("StartDate").Display();
image

4. Display a Calendar Control & TextBox for selecting Date

  var dt = form.TextBoxFor(new string[] { "Birthdate", "LastName" }).Display();

image


5. Lets do something even more. Create a ComboBox using the string array.



var dt = form.ComboBoxFor("City")
.Using(new List<string>() { "Chicago", "Canton", "Cupcake" })
.Display();



image

6. I know you are still not impressed or convinced in this silly utility. Let do auto suggestion for a textbox. In the using method you can even specify from a list or another query. Very powerful.



var dt = form.AutoSuggest("FirstName")
.Using(new string[]{"Mike","Molly","Manny","Maya"})
.Display();

image


7. You can do multiple of these Auto Suggest textboxes



var dt = form.AutoSuggest("FirstName").Using(new string[] { "Mike", "Molly", "Manny", "Maya" })
.AutoSuggest("LastName").Using(new string[] { "Suthar", "SutherLand", "So", "Sue", "Summer" })
.Display();

image


8. Here is full combination of options you can try it out.

	var dt = form.AutoSuggest("FirstName").Using(new string[] { "Mitul", "Mike", "Michael", "Manny" })
.AutoSuggest("LastName").Using(new string[] { "Suthar", "SutherLand", "So", "Sue", "Summer" })
.AutoSuggest("MiddleName").Using(new string[] { "Parry", "Pari", "Pom", "Pommy" })
.ComboBoxFor("Country").Using(new string[] { "USA", "CHINA", "INDIA", "INDONESIA" })
.ComboBoxFor("Cities").Using(new List<string>() { "Chicago", "Canton", "Cupcake" })
.TextBoxFor(new string[] { "BirthDate", "Hello", "World" })
.Display();

image


9. Bonus. Sometimes you might want to show confirmation before doing something

            var dt = form.ComboBoxFor("City").Using(new List<string>() { "Chicago", "Canton", "Cupcake" }).Display();
var shouldContinue = form.DisplayConfirmation();
if (shouldContinue)
{
"You selected to continue..".Dump();
dt["City"].Dump();
}

image


In the above example, after you display the first time then it displays a confirmation dialog. I have found PowerForms very useful in doing some daily work. 


“So what kind of things you do with PowerForms Mitul?”


Like every enterprise, we have big ERP system with hundreds of tables and I have to write queries to make sense of that data. I have created a query powered by PowerForms where I can find a user with multiple attributes. Once the PowerForm is displayed I can just type in either FirstName or userid or anything and hit submit and bingo I get my results. 


Another scenario where I use is testing web services against different uris and I have created a PowerForm with ComboBox for different Uris and related endpoints with parameters as textboxes. I save time by not having to change anything in these queries once I write them. In essence whenever you need to collect data.


“Can I run PowerForms in a console application?”


Yes.


In order to use PowerForms in a console application you will have to decorate the Main method with [STAThread] and then it will work just fine. It will open a console and PowerForm. If you change the output type of console application  to Windows Application then it will not show you console window and only a PowerForm. This way you can create a quick utility application and put it on your network share drive for others to use. The users don’t have to install the application.


PowerForms has made my LinqPad instance a Power ERP system. PowerForms is not intended as a replacement for Windows Forms. That would be silly. However for scenarios I just mentioned it serves its purpose pretty well.


What do you guys think of PowerForms? If you have any suggestions, questions, comments please let me know in the comments section.