Showing posts from 2008

MFC using an ImageList in a TreeView control

This is a bit of a departure for my regular blogging but I've been working in MFC lately so I'm posting some of the things I've learned. So I have a tree view control that I want to add some images to. In .net this is easy just drop in an ImageList and add the images in the designer. In MFC this takes a wee bit more work. Here is the actual code: /* create the image list with 16x16 sized icons, 32 bit color, 0 images to start, and resize the array 1 at a time when adding new images*/ m_ImageList.Create(16,16,ILC_COLOR32,0,1); m_ImageList.Add( AfxGetApp()->LoadIcon(IDI_ICON_ORG) ); m_ImageList.Add( AfxGetApp()->LoadIcon(IDI_ICON_POSITION) ); // set theimage list to be used in the tree view control with normal sized icons m_treeGFM.SetImageList( &m_ImageList, LVSIL_NORMAL ); m_ImageList a CImageList member variable. m_treeGFM is a member variable mapped to my tree view control. The icons are resources I have added to my application. For mo

Validating an email address with a regular expression

Below is a quick and dirty post on how to use the C# regular expression library to validate an email address. The regular expression stuff is located in the System.Text.RegularExpression namespace. So somewhere in my class I use the code below. Now you don't have to do class member variables. You could just do all this code in a function. I however will be doing multiple validations so I don't want to be destroying/creating multiple objects during the life of my class. You have to create your regular expression and then use that expression when creating the RegEx object. private string _expression = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; private Regex _validEmailRegEx = new RegEx( _expression ); Then in my class I define a function that uses the RegEx object to validate an email address

Interesting article on famous programmers

In a bit of a break from my usual posts I read an interesting article on the breakdown of 200 famous programmers . It would appear most have only worked on 1 project and are male. I was surprised to see .45% are transsexual and only 2.7% are women. I know IT/Programming is heavily male but that blew my mind. Also only 1 project? I think a lot of these guys get into a specialty and then don't do anything else for a while. Anyway a bit of an interesting read.

C# tricky ternary operator

C# has a way of letting you shorten up those short little if else statements into one line of code. It's called the ternary operator . Take the following code if something is meets our matching criteria set a value, else set another value. I've written a lot of these in my time as a coder. So let's assume we have some integer we're using, we want to set a string based on that value. We know it needs to be in one of two ranges. We could write an if then else like below: String result = String.Empty; if( someInterger < 100 ) { result = "Less than a hundred"; } else { result = "Over a hundred"; } With the ternary operator you can shorten it down to one line: String result = someInteger < 100 ? "Less than a hundred" : "More than a hundred"; Now some might argue it's less readable and I agree to an extent but if you try it a few times you will find yourself writing a lot less small if/else blocks. You

Updated getting data from an Excel file in C#

Ok I posted how to get data from an excel file a bit ago. I have some update code that reads all files into a dataset, or reads from a specific file. You could easily modify this code to read from a specific sheet as well. public static DataSet GetAllSheetsFromExcelFile( string filename ) { DataSet ds; try { ds = new DataSet(); DataTable dtSheets = new DataTable(); // get a datatable with the worksheet name(s) OleDbConnection con = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0" ); con.Open(); // get a datatable of the sheetnames in this file DataTable dtNames = con.GetOleDbSchemaTable( System.Data.OleDb.OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" } ); // add each sheet as a new table to our dataset foreach( DataRow row in dtNames.Rows ) { DataTable dtTemp = new DataTab

Reading from an excel file with C#

Ok so I have found a need to read data from an excel spreadsheet in a C# program. After looking over using COM and other things I found actually OleDB is the best way. This ensures you don't have to have excel/office installed on the target system. It may mean a bit more work but for most purposes I think it is good enough. All of this code is contained in the System.Data.OleDb namespace. This code reads from a known file with a known sheet name. If you want to be more flexible and don't know the sheet name(s) you have to do a bit more magic. That will be covered in another post. From the posts below I have decided a bit more information is necessary for this post to be more informative. When dealing with any office component you have to deal with the different versions (2000,2003,2007) etc. This example is connecting to an excel 2003 file. You will need to use the right excel version number based on what you have installed or what your customers will have installed.

Reading/Writing zip files with C#

Ok so today I found myself having to read/write from a zip file in C#. Apparently in the 2.0 framework this is built in. You need to use the GZipStream class found in the System.IO.Compression namespace. You can read more here on MSDN After a bit more research I have found the GZipStream class is pretty limited. You would be better served to look at the Sharp Zip Library. It's a GPL project so if you have issues with that read their disclaimer/legal pages. I have used it and have been able to read/write zip files made with windows xp and win zip. You can read more here UPDATE: I suggest using DotNetZip from CodePlex you can get it here. It's a snap to use and generates zip files for .net very easily and quickly. Get it from CodePlex Some sample code for DotNetZip using (ZipFile zip = new ZipFile()) { // add this map file into the "images" directory in the zip archive zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images"

Favicon generator

Cool site to convert many different graphics types to a favicon . Clickity Click!

Ghost Doc Visual Studio XML documentation tool

There is a nifty little tool called ghost doc for VS that can auto generate the XML documentation used in VS. It rocks and I use it all the time. If you don't have it you should download it here . It works in VS 03, 05, and 08. After installing it you can simply right click a methods and select document this or for the keyboard power users use a keyboard shortcut you can define to auto document.

The best programming cartoon ever

This little cartoon explains software engineering in the best way I've ever seen.


Now I'm not one to usually pontificate on pure CS topics I'm usually very specific in applications of .net related things. You know getting a grid/control/etc to do a certain thing. However in my programming duties lately I've found myself applying old fashioned hardcore CS concepts a lot. Which to my mind is a good thing. The most exciting or boring as you see it is recursion. For some reason I've been doing a lot of it lately. So it would be a good think for you programmers out there or would be programmers to brush up on it a bit. So click the following link for some good examples in C# Click the google link

Upgrades Upgrades Upgrades

So I attended the VS 2008 launch yesterday in downtown SLC. Not a bad event. It gave me the excuse to ride the new frontrunner train down from Farmington. They showed off some pretty cool stuff. I was impressed with the WCF and LINQ stuff. He showed a small demo that got data from the database and using AJAX was able to dynamically send/store data in about 10 minutes. I think the best thing was they gave out Windows Vista Ultimate and VS 2008 for free. I finally installed Vista on my home machine last night. It was god awful slow, it took 3 hours for the copy to take over on an XP upgrade. I'm still not entirely used to it but it seems to be working well. The new UI interface is slick but performance might be an issue. I haven't tested any of my dev tools but we'll see how they go. And speaking of upgrades FF 3.0 RC1 is out. I'm posting from it right now. We'll see how the memory whoring goes. Overall it's improved quite a bit from 2.0 and even e

Batch File for Windows to start/stop Oracle Services

In a bit of a departure from my regular coding I had to write a batch file yesterday that would start/stop our Oracle 11g instance on our server. Since this was old old old school stuff it took me some tweaking but i got it done. So now I've decided to share it with the rest of the world. Link to oracle.bat It's pretty simple you just call oracle.bat start/stop from the command line to start or stop your oracle services. Now the service names in there are specific to our instance so you will need to rename them to the values you have installed. Also this is for one database instance with one listener running. If you have different services running you just to need to add their service name to the appropriate start/stop section. It's a good base for extending if you need it.

Enabling Paging on a GridView

So I have been recently playing with GridViews in Normally I use repeaters but the auto styles and paging have been appealing to me a bit lately. But there is some trickery to enable the paging on a GridView. You have to write a wee bit of code to get it to work correctly. The documentation doesn't make it abundantly clear on how to do this. What you need to do is implement the PageIndexChaning event and rebind your datasource to the grid. So for my project the code looks like this: protected void GridViewProducts_PageIndexChanging( object sender, GridViewPageEventArgs e ) { /* Reset your datasource, I am using a custom collection here */ this.GridViewProducts.DataSource = this.ProductsList; /* Set the new page index this comes from the GridViewPageEventArgs arguement passed into the method */ this.GridViewProducts.PageIndex = e.NewPageIndex; /* Rebind the grid */ this.GridViewProducts.DataBind(); }

windows powershell

So for those of you not in the know, microsoft has realesed a pretty good set of command line tools called windows powershell. It pretty much offers unix power commands to the windows command line. It's pretty cool and allows developers to add functionality via snappins. The microsoft documentation is here . A pretty good tutorial can be found here . I've followed the tutorial and found it fairly easy to create the cmdlet and snappin. Installation isn't as easy as I woule like but it's doable.

Internet Explorer 8 beta 1 is out

IE 8 from MS is now out in beta 1. I haven't had a chance to download and play with it yet but it's here. Download here!

JSP/Servlets vs ASP.NET

So if anyone reads this blog they know I write a lot of C# code. My work is currently looking at grabbing a new project using JSP/Servlets on Oracle rather then ASP.NET on SQL Server. I have no issue with the oracle, I hate SQL Server and think it's a kids toy compared to oracle. However I haven't written much java but from what I've read it seams pretty easy to write JSP/servlets. There seems to be a speed debate over the two so here are my two cents. ASP.NET I feel is not scalable. The project I'm working on takes 5000 users concurrently on a regular basis and it gets hammered on performance. We've looked and looked and the way microsoft suggests you do things works great for small projects with low users. But for enterprise level stuff has to be done in a radically different manner. So while you can get a web page up and running pronto it can't scale worth shit. We are currently struggling to implement these kinds of changes. Java on the ot

Bad programmer, Bad!

Ok so I posted some javascript for a numeric only textbox and it wasn't working. Someone posted a comment about it so I took a look at it. Sure enough the whole thing was boned. Hopefully I didn't lead anyone astray or anything. So I fixed the script and tested it and found it works now. So if anyone wants to read the new script it's over here: JavaScript post

Saving Changes to a DataTable bound to a DataGridView

Ok, So I'm working on app that binds a datatable to a data grid view. Normally you would just edit right in the grid and be done. However our table has too much data to edit right in the grid so instead users have to click a row on the grid to load a details panel below the grid. This works great except I noticed changes made to the last row weren't being saved. This is because the grids don't commit changes to their datasource unless a new row is clicked. So after a few hours of digging I found this bit of code worked very well. // commit all our changes to the datasource of our grid if( dataGridViewVariables.BindingContext[_tableEditVariables] != null ) {   dataGridViewVariables.BindingContext[_tableEditVariables].EndCurrentEdit(); }//end if Check that the object in this case a data table being used to bind exists. And if it does call the end current edit on that object. However if you're binding to a custom collection of objects I believe you need to

Firefox 3 Beta 3 is in the wild

Ok so Firefox v3 has release a new beta version 3. I have downloaded it and will see if the memory issues are still improving. Download here

Using the ItemDataBound event with a DataReader vs DataTable/DataView

A little while back I was blogging about how we were switching over to datareaders for getting data back from our database. Well it was working fine until we noticed on some of our pages data wasn't being calculated correctly. This was traced to the ItemDataBound event used in our grids/repeaters not working correctly. With a datatable/view we would cast the e.Item.DataItem as a DataRowView; which works just fine. However datareaders when being databound can't be cast as DataRowViews. You have to use the DbDataRecord class. Afte switching from the DataRowView to DbDataRecord in the ItemDataBound event everything works just like it should. DbDataRecord is in the System.Data.Common namespace. Make sure you are importing that namespace if you haven't already. So to summarize it: Binding Object What to Cast to in ItemDataBound event DataReader DbDataRecord DataTable DataRowView DataView DataRowView

Notify Icon Text vs BalloonTipText

Ok so I have a small application that shows a users ip address via a tool tip when it sits in their system tray. Everything is going fine until someone sends me a bug that there was a problem. Investigating this I find that the method I was using to display tool tips in the system menu is somewhat flawed. I was using a notify icon control to show the tool tips while in the system tray. This works but I was setting the Text property of the control like this: // old way of setting text, with 64 character limit this.notifyIcon1.Text = "Show me in the toolbar"; This works but only for up to 64 characters. When I was designing the application I was thinking about ip addresses using the v4 format not the v6 format. Sure enough v6 ip addresses kick out more then 64 characters a lot of the time. So the search was on and I found using the balloon text can show more data but is a wee bit harder to use. Balloon tool tips don't show automatically like the text does. You h

Some interesting links

So I found some interesting links on software development programming: How to recognize a good programmer Confessions of a terrible programmer The first one is fairly interesting. I agree with most of his points and I think drive is a good indicator but maligning people who don't code in their spare time and people who learned it in school is not accurate. Some folks just code during the day and don't want to code when they get home. It doesn't mean they're not good programmers. However people who don't want to learn new stuff while not bad coders are doomed to work on the same ol thing forever. Maybe some folks like that but it's not for me. The second one should be required reading for all coders. It's very zen but you have to realize you know nothing before you can learn anything. It offers a lot of very good advice on how to become a better coder. The first step is to realize you're a bad coder to begin with.

Use DataReaders instead of DataSets

Ok so I'm working on a fairly large application. When we first started we were using DataSets for ease of use. However as the app has gotten large we're finding DataSets are very heavy in memory and filling them from the database takes a lot of time. So after some research I found DataReaders are the best thing to use. We use the data access blocks (3.1) from microsoft and swithing from ExecuteDataSet to ExecuteDataReader saved almost 2 seconds. It was a huge savings. Switching to datareaders isn't without some problems. In some instances we need hierarchical presentation and datasets work very well for that. Also some of our classes expose dataviews as properties and that creates some problems. However most of this can be overcome with some better OOD. And when using datareaders always always always close them! So use the using statement in c# which closes the datareader when done. So using the data acccess blocks with a stored procedure here is some sample code of

New software Directory Sweeper is unleased!!

Ok so I finally finished work on my latest software program Directory Sweeper. This one lets you clean specified directories at a given interval or simply when you want to. It can sit in the system tray or you can run it full screen. You also get a report of what files/directories it was unable to remove after the last sweep. It's version 1.0 and I can think of lots of improvements already but it's pretty good. I think it's actually good enough to charge a small fee for. It's only 9.95 to buy, you can download it over at my website under software. Download it, try it, and if you think it rocks your world just send me the paypal. If you think it stinks let me know why! The screenshots below show the app in action.