Filtering a Dictionary List

One important component I needed for my Systems Directorate application is the ability to filter rows from a dictionary list and get the matching and non-matching rows. This article will describe the set of extension methods I created to perform this filtering.

Read more for details!

Continue reading

Posted in Dennis-IT Tools | Tagged , , | Comments Off on Filtering a Dictionary List

Some useful string extensions

I have created a useful set of string extensions in C# for doing many string operations like conversions to and from numbers, formatting, and escaping. Most of these were required by the application that I’m working on but are very useful for any purpose.

Continue reading

Posted in Dennis-IT Tools | Tagged , , , , , , | Comments Off on Some useful string extensions

Macro Resolution via Dictionaries

In my Systems Directorate application, I needed a way of handling macro resolution so that the user could easily substitute values when configuring parts of the application.

For most of the application, I have standardized on a Dictionary<string,object> structure for storing information. I retrieve records from the database this way, I store message events in structures like it, etc. I have systems that convert the dictionaries to tab delimited tables when I store them in files or read them back in. Even type information is retained.

The macro format I wish to support is the same way it is done in UNIX shell scripts and other script languages. Eg. ${NAME}.

Continue reading

Posted in Dennis-IT Tools | Tagged , , , | Comments Off on Macro Resolution via Dictionaries

Handling Highly Structured Data in ASP.NET Pages

Hi Everyone! I thought I’d talk about how I handle working with highly structured data in an ASP.NET web page. For my DMF application, I’m using ASP.NET for the management console for the system and I have a lot of highly structured classes of information.

I had some requirements to meet.

  • All the data had to stay within the page until the user clicked SAVE.
  • Had to support numerous user controls on the page that displayed tables and forms.
  • Can not use Session or other memory storage to keep the data between posts.

Continue reading

Posted in ASP.NET | Tagged , , | Comments Off on Handling Highly Structured Data in ASP.NET Pages

My Excellent Database Library

I was going to talk about a new feature that I had just added to my database library, but then I realized that I haven’t talked about the library at all yet.  I guess that I planned to for so long, I just thought I had!

For my Systems Directorate project, I needed a good database library that would let me support different database servers on the back end.  I came up with a pretty extensive library which used resource like strings for each database, much as you would for spoken languages.  It was fairly nice, but very time consuming to use because you had to build this large library of SQL commands for every little thing you did.

For another project though, I wanted  a much simpler and faster database class so I started over.  That one became so easy to use though that I dropped the larger one and went with the new one.

So, what makes my database library so nice to use?  I wanted a very easy way to execute a SQL statement with full SQL parameter support and yet, only have a single statement in my code.  I achieved this by simply stealing an idea from String.Format().  I figured, that my SQL statement could simply use a params array of parameters much like a formatting strings does.

Continue reading

Posted in Dennis-IT Tools | Tagged , , , , | Comments Off on My Excellent Database Library

Updating WPF windows from background threads

One of the most confusing things to do in a Windows application is to update your window or its controls from a background thread. Window controls will only allow updates from the main application thread. This problem existed back in the WinForms days and also within WPF.

I’ve found a real easy way though to handle this situation though which is extremely flexible to use.

Like in WinForms, you need to check if you are on the main thread or not and if not, you can use a delegate to pass information to the main thread and have it update the display. I used to create dedicated delegates and functions for updating specific controls. But I realized that you don’t need to do that if you use lambda expressions.

Here is the method that does all the work. It checks if you have access to the main thread and if so, it just executes your delegate expression. If not, it invokes the main dispatcher and passes your expression to it unaltered.

[ccle_csharp lines=”-1″ width=”600″] public void BackgroundUpdate(Action somethingToDo)
{
if (Dispatcher.CheckAccess()) {
somethingToDo();
} else {
Dispatcher.Invoke(DispatcherPriority.Normal, somethingToDo);
}
}
[/ccle_csharp]

Now, for an example, say you have a TextBlock called myTextBlock somewhere on screen and you want to update a number on a regular basis.

[ccle_csharp lines=”-1″ width=”600″] public void Run()
{
int count = 0;

while (count < 1000) { int localCount = count; BackgroundUpdate(() => {
statusBlock2.Text = string.Format(“{0}”, localCount);
});

count++;

Thread.Sleep(100);
}
}
[/ccle_csharp]

The Dispatcher being used belongs to the Window itself, so it has access to all controls within it or that it can find through other means.

One thing that is important in the example above is that tools like Resharper will complain about the variable “count” saying “Access to modified enclosure” if you tried to increase the count directly within the delegate expression and didn’t use the localCount variable. In this case, it would have still worked, but this condition can really cause headaches if you don’t understand what is happening.

Posted in WPF | Tagged , , | Comments Off on Updating WPF windows from background threads

Building a ListView with Checkboxes in WPF

So, in my work to build the GUI for my DMF project, I came across a need for a list view with check boxes in the first column.

I was creating a dialog window for editing a user account, and needed a simple way of defining what roles that user would have assigned to them.  I know some traditional methods are to display two lists with back and forth buttons between them, but that’s so 1990’s.

I wanted to display the checkbox, its title, category name, and description of what the role provides.  This is best done in a list view, as opposed to just plain check boxes in a list box.

Naturally, I started with some google searches because that’s how you always start these little adventures.  I found out that putting a check box in the list view column was actually very easy!  But none of the articles really went past that point.  The check box is there, but the whole list view still looks like crap and doesn’t operate very well at all.

I’ll try and list all the missing pieces here to get to that finished list view that works great.

Continue reading

Posted in WPF | Tagged , , , | Comments Off on Building a ListView with Checkboxes in WPF

Serializing data quickly and easily

Have you ever had a class with a bunch of data in it that you needed to store easily in a file or database field? Serialization is usually the easiest answer. But, what should it be serialized to?

The best answers are usually XML or JSON. I prefer XML in most cases since is natively supported by the .NET framework since the early 1.1 days. But serializing data back and forth is an annoying task in most cases so I found an easier way of doing it. By deriving your classes from a special templated base type, you can add serialization to almost any class you create.
Continue reading

Posted in Dennis-IT Tools | Tagged , , | Comments Off on Serializing data quickly and easily

Welcome to my new programming blog!

I’ve had this domain now for about three years and have been using it for email mostly.  Once in a while, I’d setup a blog, post nothing, and shut it down again.

But, I’ve finally found a hosting provider where I can let this blog live instead of on a Linux VM in the basement.  I’m hoping to post some articles about the software that I’m working on in my spare time.

I do almost all my programing these days in the one true language: C#.  That’s right, C#!  I’ve done lots of work in the past in C, C++, Perl, PHP, Java, and even that horrible VB thing.  Python freaks me out so I’m pretty sure that I’ll never try that one.  As for platform, I pretty much target Windows only.  It’s got the lion’s share of the market, so why target anything less?

Continue reading

Posted in General | Comments Off on Welcome to my new programming blog!