When I get some spare time here and there, I am developing a template driven code generator which uses a database schema as a data source. In doing this, I want the templates to specify what type their output is. To support this, I have defined an enum TemplateTypes with the values of TSQL, VisualBasic, CSharp and Unknown. I want to be able to convert the names of the enum values to more friendly values so I can use them in things like the tooltip text of nodes in a treeview. I remembered that Geoff had previously posted about using type converters with enums to achieve this.
To avoid reinventing the wheel, I took his code and ran. What I found though, is that when the enum value name is converted to a nice string, it adds a space each time that it encountered an uppercase character that wasn't the first character in the name. This means that TSQL was converted to T S Q L. I made minor changes to his EnumToString function so that it only adds a space when it finds that the previous character is lowercase. I also added a conversion of _ to spaces as well.
The function now looks like this:
'given the passed in enum, convert to a string,
'but inject spaces in font of all capital letters.
'a regex might work best here, but for now, let's do it the long way
Private Function EnumToString(ByVal poSource As Object) As String
'convert the value to a string
Dim sVal As String = System.Enum.GetName(moEnumType, poSource)
Dim sNew As New System.Text.StringBuilder
' Replace _ with spaces
sVal = sVal.Replace("_"c, " "c)
'loop through each char, looking for spaces.
For i As Int32 = 0 To sVal.Length - 1
If i <> 0 _
AndAlso System.Char.IsUpper(sVal.Chars(i)) _
AndAlso System.Char.IsUpper(sVal.Chars(i - 1)) = False Then
sNew.Append(" "c)
End If
sNew.Append(sVal.Chars(i))
Next
Return sNew.ToString
End Function
I ran the following test cases:
- T (no change)
- TSQL (no change)
- CSharp becomes CSharp (no change)
- VisualBasic becomes Visual Basic
- TWhyAmIDoingThisT becomes TWhy Am IDoing This T. This one is kinda close, but I can't really make a decision about whether multiple upper-case characters should be split.
- T_WhyAmI_DoingThisT becomes T Why Am I Doing This T
Updated: Corrected results of the CSharp test case - Thanks Geoff.
The
announcement has been made. Community Server 2.0 is out in the wild. I will hopefully convert this site over to the new version sometime in the next month.
I have been developing some new software which uses the WebBrowser control. Depending on what is selected in a TreeView control, the browser will be instructed to load some HTML that has been created and manipulated in memory.
The problem I have encountered is that once the HTML has been determined, setting the DocumentText property hasn't been updating what is displayed in the browser. It only displays the HTML that was orginally assigned to the control. The reason for this not working is hinted at in the help documentation for the Document Text property. The documentation says this:
Use this property when you want to manipulate the contents of an HTML page displayed in the WebBrowser control using string processing tools. You can use this property, for example, to load pages from a database or to analyze pages using regular expressions. When you set this property, the WebBrowser control automatically navigates to the about:blank URL before loading the specified text. This means that the Navigating, Navigated, and DocumentCompleted events occur when you set this property, and the value of the Url property is no longer meaningful.
For my application, I have set up the WebBrowser control to be as restricted as possible because I don't want my application to look like a browser. For example, I don't want the browsers context menu to be displayed when the user right-clicks on it. One of the properties I changed was to set AllowNavigation = false. The help description for the AllowNavigation property says this:
Gets or sets a value indicating whether the control can navigate to another page after its initial page has been loaded.
If you fire up reflector and find the WebBrowser.DocumentText property you will see that internally, setting the WebBrowser.DocumentText property will create a MemoryStream with the new text value and then the stream is assigned to the WebBrowser.DocumentStream property. Reflector shows the DocumentStream property code to be this:
public Stream DocumentStream
{
get
{
HtmlDocument document1 = this.Document;
if (document1 == null)
{
return null;
}
UnsafeNativeMethods.IPersistStreamInit init1 = document1.DomDocument as UnsafeNativeMethods.IPersistStreamInit;
if (init1 == null)
{
return null;
}
MemoryStream stream1 = new MemoryStream();
UnsafeNativeMethods.IStream stream2 = new UnsafeNativeMethods.ComStreamFromDataStream(stream1);
init1.Save(stream2, false);
return new MemoryStream(stream1.GetBuffer(), 0, (int) stream1.Length, false);
}
set
{
this.documentStreamToSetOnLoad = value;
try
{
this.webBrowserState[2] = true;
this.Url = new Uri("about:blank");
}
finally
{
this.webBrowserState[2] = false;
}
}
}
When a new stream is assigned to DocumentStream, it will attempt to navigate to about:blank as the documentation says. The code here indicates that the stream data will be populated into the browser after it has navigated to about:blank. The hitch here is that if the navigation fails, you are none the wiser that the process has failed and the new document text won't be loaded. My problem with the code here is that the try block around the navigate swallows the exception, hence the document content doesn't change and you won't know why.
Basically, setting AllowNavigation = false cripples the DocumentText and DocumentStream properties as well as the navigation methods.
I have been thinking about this question ever since I started my first 'real' job in the industry when I was half way through university. Each time I think about UI design and what it means to software, I come to the same conclusion.
Market acceptance and widespread use of software is now becoming proportionate to the quality of the UI design of that software.
Traditionally, UI design wasn't seen as important as functionality, regardless of what the user liked to see. UI design was seen as an unnecessary project expense. There didn't seem to be a link made between UI design and usability. I believe that design and usability go hand in hand and usability will always impact on the functionality of software or at least the users willingness and ability to use the functionality provided. Unfortunately, this still seems to be the case in many software projects today.
Robert Scoble recently had an interview with Jenny Lam, a designer on the Microsoft User Experience team. It was very interesting to hear about the progress Microsoft and the rest of the industry has made with regard to UI design. As it should, UI design is now starting to become more important in the overall software design process. Although I am a developer, my current role is in a UI design team and I also see many of the struggles in trying to get UI design recognised as important to a product.
Bobinho made some very interesting comments in a response to the Channel 9 video. I really agree with him when he says:
But Windows developers, on the whole have no sense of style. No taste. No feel for design. And their products suffer greatly as a result.
Developers tend to be people who can appreciate great looking UI, but can't make it themselves. Software projects worked on by small companies and individuals with limited or no budgets are the common victims of bad UI design. This is because they don't have the skills to create good looking UI and can't afford to have skillful people employed to do the work. Development with larger budgets often give a little bit to UI design, but I think there is still not enough focus on it in larger projects.
In my previous jobs, I have seen many projects that have not had any professional UI design work done. Several times I have mentioned to management that if they want their great product to be successful, they need to make it look good as well. Unfortunately this advice has always fallen on deaf ears. Of course this means that the UI design is done by developers with the result being what Bobinho calls 'no sense of style'. My colleagues in the UI design team have often taken a stab at developers because of poor designs that they come up with. Even though I am a developer, I whole-heartedly agree with them.
Many times I have heard about projects that have been rejected by a management or client review because it didn't look good, even though the 'naked' product itself was fantastic. I have also often heard of poor solutions getting approved because the UI looked great. This is partly about first impressions, but also about whether the people you are selling the project to (the managers) or the people buying the product (customers keeping you employed) are able to see through a bad looking product and can appreciate the product for what it does.
This comes down to demographics. Who are you trying to please when you are trying to sell your project or final product? If they want to see a good UI as well as have a good product, then use some design talent in your project. How successful would the Xbox be if the dashboard was an ancient text based console style design?
Have a read of some interesting quotes regarding design that Joe Stagner posted a while back.
Good UI design isn't just important to software, it is critical. Users are starting to expect more from software. This is probably because of progress made in the gaming industry, but also the now common use of advanced CG in movies.
I love good looking software and I think companies are starting to realise the importance of having good UI design along with a great product under the covers. I hope an increase in the use of smart/rich clients compared to web interfaces and the use of the coming Windows Presentation Foundation will propel companies towards putting more effort into UI design.
I am very much looking forward to a future of great looking software. I know it is going to be a bumpy ride, but sweet looking software is just too good to ignore.