AJAX AutoSuggest Textbox

I wrote an article on CodeProject describing this nice webcontrol I’ve done using the Anthem library. I find this webcontrol very useful in many situations.

Here is the link to the article with the source code and some samples: http://www.codeproject.com/Ajax/AJAXAutoSuggest.asp

Change ListBox background color

The screenshot below is from an interesting software I wrote for my college final project. It is an assembly and microinstruction emulator that gives you the chance to debug and monitor all the registers, controllers, buses and memory values. I’ll try to talk more about that project in future posts.

On the form we have a RichTextBox on the left, where we can write macroinstructions (i.e., assembly) and on the top left we have a ListBox that contains the default microinstruction program.  So as we debug the instructions we have to keep track of which command line is being executed, both on the macro and micro-program.

To give it a Visual Studio feeling I decided to highlight the current command line.

The RichTextBox already has a built-in SelectionBackColor property, so the only thing I had to do was to select the current command and set the property to yellow.

On the other hand, the ListBox doesn’t have any built-in property like that. But I found a pretty easy way to do it.

The ListBox control exposes a DrawItem event that we can use to override the default drawing for each item.
First we need to set the DrawMode property of the ListBox to DrawMode.OwnerDrawFixed. This can be done on the form’s constructor, right after the InitializeComponent method is called.
Then, on the DrawItem event handler we need to write the following code to draw the ListBox item.  

private void lbMIC_DrawItem(object sender, DrawItemEventArgs e)
    // if we call the DrawBackground method, 
    // it will use the default gray color to paint the background
    Brush backGroundBrush;

    // Determines if the current item is the selected one
    if ((e.State & DrawItemState.Selected) > 0)
        //It is the selected one, so we use yellow
        backGroundBrush = Brushes.Yellow;                
        //It isn't the selected one, 
        //so we use the default white color
        backGroundBrush = Brushes.White;                

    // Draw the background as a rectangle using 
    //the color defined above
    e.Graphics.FillRectangle(backGroundBrush, e.Bounds);

    // Draw the text of the item
    // Here you can also change the text fore color
    if (e.Index > -1)
        	e.Font, Brushes.Black, e.Bounds);


With this in mind, we could easily create an inherited ListBox control with a built-in SelectedItemBackColor property.

Hope it helps.


Select Multiple Objects in SQL Manager Studio

This is a little tip for developers who are using SQL Server 2005 and, like me, are missing some of the functionality Enterprise Manager used to offer.

It’s often necessary to select multiple objects (tables or stored procedures) to script or drop them. But in the Object Explorer in SQL Management Studio you can only select one item at a time. That’s a pain when you need to publish your new stored procedures on the production database at the end of the day.

After a couple of days I realised that you can use the summary page to multiselect objects. Just use the F7 key to open it and now you can script or delete multiple objects. You can even filter the objects you want to see on the page by using the filter option on the top of the page. That’s very helpful when your database has more than one thousand stored procedures.

T-SQL Proper Case Function

Today one of our clients asked us to fix the letter casing in a particular field of the products table. We had just imported their products table from their old system into ours and the products descriptions were all messed up.

Since there is no buit-in function to do that, I had to do it my own.
I’m posting it here in case someone find it useful. 

CREATE FUNCTION [dbo].[ProperCase] 
    @str VARCHAR(500) 
    IF @str IS NULL
        RETURN NULL  

    DECLARE @counter INT
    DECLARE @ret VARCHAR(255) 
    DECLARE @currentChar CHAR 
    SET @counter = 1 
    SET @ret = ''         
    SET @goUp = 1 
    WHILE @counter <= LEN(@str) 
        SET @currentChar = SUBSTRING(@str, @counter, 1) 
        IF @currentChar IN (' ', '-')                    
            SET @goUp  = 1 
            IF @goUp  = 1 
                SET @currentChar = UPPER(@currentChar) 
                SET @currentChar = LOWER(@currentChar)  
            SET @goUp  = 0 

        SET @ret = @ret + @currentChar
        SET @counter = @counter + 1 
    RETURN @ret 

After creating the user-defined function in the database, you can use the following line to test it:

SELECT dbo.ProperCase('GOLD 802.11B WIRELESS CARD')

Source Code Formatting

When I decided to set up this blog, the first thing I thought was: “how am I going to format my c# code blocks on the web?”. After some googling, I found this really nice site that, apparently, did just what I needed. It formats c#, vb, html and even t-sql. The only drawback for me is that it uses CSS classes. WordPress and other free blogs do not support that.

Thankfully, the author had posted the source code, so I tweaked it a little bit and created a simple windows app to format the code without CSS classes.

Here’s a screenshot:

And here’s a proof it works:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Web.UI; 
using System.Web.UI.WebControls;           

namespace Common 
    /// <summary> 
    /// Comments here 
    /// </summary> 
    public class FieldManager 
        private Dictionary<string, Field> _controlList; 
        public FieldManager() 
            _controlList = new Dictionary<string, Field>(); 

If you like it, get the source files here.

Hope this helps,