It Seems I Enjoy Peril

For the most part, the last post had described how far I’d gotten in that ASP.NET project. I’m not done by any means, but had to move on because I’d gotten an opportunity to write a desktop app. What this new application would need to do is be able to accept text that comprises of only numbers, would always be either 8 or 9 characters long, would have two columns to view the data that was inserted, and would associate all 9 character items with the last 8 character item that was inserted. This is because this program will be used to scan boxes (8 character barcode on the front) and files within those boxes (9 character barcode on them). Also, the PC this would be used on would be remote and not have easy access to an internal database, so it would need the ability to store this data locally. With that in mind, we decided to export the data into a CSV file in the exact same format as it is in my app.

Considering I’d not done a desktop app yet, I immediately opened Visual C# 2010 Express that I’d already installed on my machine and looked at what project types were available. I saw WPF and Windows Forms, and since I had no idea the difference between the two, I looked them up. I was on a tight schedule, so I didn’t read through all the pages on either (had about a weeks to get solid progress). Instead, I searched for a comparison of the two, and came up with this. Considering some of the people on this post insisted that Windows Forms was easy to get started and well documented, I decided to go with it. However, if I would’ve noticed that this post was 5 years old, I would’ve rethought it. Many of my buddies at Code and Coffee have suggested that this may have been easier if WPF was used, but since I’ve already done most of the project in Windows Forms, I’ll still write about it regardless.

Moving on, I started my Windows Forms project. Right at the very start, you’re given an empty window. One very interesting part of Windows Forms as opposed to Web Forms is the Properties pane down in the bottom right. In this area, you can add and remove a bunch of different things to your Windows Form (you can do this in Web Forms, but it seems to be a bit less useful in my opinion). Also, just like in other projects that have a UI, you can add controls using the toolbox. I generally don’t use this just so I know how to do it manually and understand it more, but considering my limited amount of time, I decided to use it. I added a TreeView, since I figured that would show a good parent/child relationship between the boxes and files. So I started in the same way my other project went, where I created a list of strings and started trying to find a way to add it to the TreeView. TreeView uses Nodes as it’s data containers, so I was looking for ways to add items to them.

This is simple enough though: I did a foreach loop that selected each item in my list and added them using treeview1.Nodes.Add() (where treeview1 is the name of your TreeView). However, when you add an item, it adds it as a root node (as if it’s the top of the tree). I wanted to differentiate between 8 and 9 character items and make 9 character items the child item. So then I looked into child nodes and ran into a brick wall in the form of a lack of time. If you observe this post as a noob, you’ll notice that it has a bit of complexity to it. I had no time for complexity, so I bailed on the TreeView idea and thought a bit further. I then thought it may be a good idea to use a CheckedListBox and change the font style or color when the item is 8 characters long to easily show the difference between the two different kind of items. However, after searching for quite some time and only finding things like this, I decided to try another route.

I went and talked to my boss about what’s really necessary for this project to be sucessful. After clarifying, all I needed was for 8 character items to go into a left column and for 9 character items to go into a right column. Since they’ll be scanned sequentially, I don’t really need any checking; just a raw data feed in a simple list. If there’s any issues, they can easily be sorted afterwards in the CSV output file. Sounding decently easy, I added a ListView. Then, using the Properties box in the bottom right, I added two columns (calling them Boxes and Files) and set GridLines to true (just to make it look better than a blank box). I then used a for loop to run through my list and tried to use an if statement that would add the items to their respective column based on length. The columns have an index number, so I hoped by just trying to add the items to the index, they would keep adding one after another in the column. However, they just kept adding to the first column and was ignoring my if statements. Below is the code, where BoxNumberRepository is the class that holds my list, _boxAndFileList.

for (int i = 0; i < BoxNumberRepository._boxAndFileList.Count; i++)
            var item = BoxNumberRepository._boxAndFileList.Item[i];
            if (item.Length == 8)
                BoxAndFileList.Items.Insert(0, item);
                BoxAndFileList.Items.Insert(1, item);

Next week, I’ll continue to talk about this project and what I did to work around this problem. Until then, have a good week, and thanks for reading!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s