Archive for December 2009

Control arrays in VB.NET

One of the things that I really liked (and used quite a bit) in the VB6 IDE was the ability to use the design surface to create a form with a bunch of controls with the same name as a control array. You would create the controls on the design page, give it the same name as another control, and the Index property would automatically be incremented. This would then let me use a loop to manipulate and examine these controls.

This functionality is missing in VB.NET, as I discovered when I tried to do my first .NET Compact Framework application way back when Visual Studio 2003 was shiny and new, and has continued to be missing from the feature set in VS2005 and VS2008.

Microsoft is not much help on this front. Their solution is to create the forms in code, as described in this article:

Not so helpful link (link redacted)

But I like using the design surface to create forms. A co-worker suggested we try to do a test in C# and tamper with the designer.cs file to create an array of controls in there, which worked OK. The big problems there were that the controls showed up on the design surface, but they could not be clicked and modified. Also, when we added a control to the form and saved it, all of the customizations we made to the designer file disappeared. (Oops.)

So instead, what I am now doing is creating my forms in the designer as before, with each control of the set having a different name with a number after it (such as cboName0, cboName1, etc.), referring to the index of the control in the array. At the top of the form’s class I have the arrays defined:

Dim cboName(10) as ComboBox
Dim lblNumber(10) as Label

Then, in the form load event, I am calling this subroutine:

Sub SetUpControlArrays()
    For Each cb In Me.Controls.OfType(Of ComboBox)()
        If cb.Name.Contains("cboName") Then
            cboName(CInt(cb.Name.Replace("cboName", ""))) = cb
        End If
    For Each lbl In Me.Controls.OfType(Of Label)()
        If lbl.Name.Contains("lblNumber") Then
            lblNumber(CInt(lbl.Name.Replace("lblNumber", ""))) = lbl
        End If
End Sub

Now I have the ability to address the controls in the same way that I used to do in VB6. The fact that I lived with this missing feature for about 6 years and just now figuring out a decent way around the problem pretty much guarantees that VS2010 will put the control arrays back in.

EDIT: At the request of a Strong Bad fan who shall remain nameless, I changed up the code above to be a bit more friendly. The previous code only extracted the rightmost 1 character from the name of the control on the design surface, which would not work if you had a control name such as cboName10.

Can we all get along?

I finally got tired of living with the inability of WordPress to automatically update on this very blog site, which is hosted on 1&1, so I did some digging into the WordPress forums. In addition to the automatic update just hanging up when trying to download the updated zip file, I also would never see anything appear under the WordPress Development Blog and Other WordPress News sections on the right side of the main dashboard.

One of the solutions mentioned was to set up your web site to use PHP5 instead of PHP4. So, I logged into the 1&1 administration panel, but could find no setting for the version of PHP used by my web sites. (My company has sites hosted on Rackspace Cloud, formerly Mosso, which has a nice little choice list on their admin panel to set the PHP version.)

A quick phpinfo() page on my blog site indicated that version 4.x of PHP is used. An equally quick search of the 1&1 FAQs yielded the answer.

You have to put the following line into the .htaccess file in the root of your web site:

AddType x-mapp-php5 .php

Apparently, this makes it appear to 1&1 hosting that all of your PHP files are meant to be run using PHP version 5 instead of version 4, without having to rename all of your files to have a php5 extension instead of php.

Oh, and I hope everyone is having a nice Boxing Day.

The keys to the Kingdom

The biggest problem that I have had over the past 2 years or so of working with the iPhone SDK is the seemingly insurmountable learning curve to have you applications do something reasonably simple. I think this is partly due to my lack of any free time to dive into the whole iPhone SDK/Cocoa Touch/Objective-C world, and partly due to my straight ahead, VB6 development mentality.

For example, in my VB6 applications, if I need to display a dialog to the user to collect some bit of information, the parent code creates the form and shows it, and the child code closes down the child form when the user is done. This reasonably simple task has proven a bit elusive, as it seems like at times, I can’t get this to work for one reason or another.

Alas, I think I have finally figured out a good way to do this very task. Now, all I do in my parent view controller is to run the following code when I need to:

ChildViewController *controller = [[ChildViewController alloc]
					initWithNibName:@"ChildViewController" bundle:nil];
[self presentModalViewController:controller animated:YES];
[controller release];

In the new view that comes up by calling the code above, I use this code to get rid of the new view:

[[self parentViewController] dismissModalViewControllerAnimated:YES];

(Now of course, there is more going on here, as you need a way to pass values back and forth from one view to another. I think I might have a blog post from a few months ago to talk about this process.)

The end result is that I now have a simple way to show and dismiss forms in much the same way that I did with VB6. Does discovering this mean that I will continue to bumble around in the dark when it comes to the mysteries of inheriting from the correct classes in the iPhone SDK? Probably. Am I worried about it? Nope.

Oh, and I hope everyone has a nice Christmas (or insert your own holiday here, Kwanzaa, Festivus, Hanukkah, etc.), and drive safely out there. Grandma still hasn’t recovered after being run over by a reindeer.

Demo days (CONDG meeting, December 14, 2009)

The Central Ohio .NET Developers Group held their final meeting of the year last night at the Microsoft office in Polaris. Four people gave short demos of Xbox, Zune HD, Windows Mobile, and iPhone, and talked about developing for each platform.

The highlight of the evening was my name being drawn as winning the year’s final and perhaps most craptastic door prize, which I then gave to my coworker who was attending the meeting with me. He is into free t-shirts.

2009-12-15 09.13.18

Netbook resolution in Windows Virtual PC

A customer lets me know that they are having a problem with one of our software products. It was something specific to the 1024 by 576 pixel resolution of their Dell netbook, which was a problem since we did do not have a computer with that resolution.

So I decided to try to use the Windows XP Mode of Windows Virtual PC on my Windows 7 Ultimate dev box to simulate the netbook resolution and try the software in that virtual machine. Unfortunately, I fire up the Windows XP mode only to find that it is fixed to 1024 by 768 resolution in the display settings, with no way to change since the Advanced button is disabled and there are no other choices for the resolution slider.

After I investigated a bit, I found that I could edit the Windows XP Mode.vmc file (located in the folder C:\Users\My Name\AppData\Local\Microsoft\Windows Virtual PC\Virtual Machines, substitute your name for My Name in the path), which is an XML file. There is a ui_options section in the file, my file now contains the following 2 items:

<resolution_height type="integer">576</resolution_height>
<resolution_width type="integer">1024</resolution_width>

Tap toon

I saw this cartoon come across my screen today:

Spinal Tap amps

Spinal Tap amps

Nice work, xkcd. Click the image above to visit their web site.