Andrej Tozon's blog

In the Attic

NAVIGATION - SEARCH

Adventures in Calculus, pt. 2: A Letter to Santa

In previous adventure, we built a simple calculator with some help of DataTables and data binding. Today, with holidays right behind the corner, we'll write a letter to Santa, using the same tools.

DataTable's expressions can include other columns' values and they are not limited to numeric values - they have much power in text operations too. For this example, we'll build a simple tool to transform raw input data to a nice letter to Santa.

Again, we'll create our base calculation class and call it SantaTemplateCalculator. In it's constructor, we'll create a DataTable like in a previous example, with some extra columns, which will represent the data we're going to input:

table.Columns.Add("Name", typeof(string));
table.Columns.Add("Gender", typeof(string));
table.Columns.Add("Behavior", typeof(string));
table.Columns.Add("List", typeof(string));

We'll also expose this fields as properties:

[Bindable(true)]
public string Name
{
    get { return (string)row["Name"]; }
    set { row["Name"] = value; }
}

...

Then we'll build a three-part form:

The left-hand side part contains input fields, which we'll bind to our SantaTemplateCalculator class' properties. The middle part serves as a template and is bound to Expression property of our output column. And the value of this output column is shown as a finished letter in the right-hand side label.

Here's a few lines of code to databind our input fields to the SantaTemplateCalculator class:

name.DataBindings.Add("Text", calculator, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
gender.DataBindings.Add("Text", calculator, "Gender", true, DataSourceUpdateMode.OnPropertyChanged);
behavior.DataBindings.Add("Text", calculator, "Behavior", true, DataSourceUpdateMode.OnPropertyChanged);
list.DataBindings.Add("Text", calculator, "List", true, DataSourceUpdateMode.OnPropertyChanged);

If you take a closer look at the template, you'll see nothing but basic string concatenation and references to some variables - these are the names of DataColumns we added to the DataTable.

DataTable's expressions also support some other functions and operators, like the Iif operator, which returns a value based on some simple expression. In the above example the expressions "short list" or "long list" are generated based on how many letters we enter in the List text box.

Our Santa letter generator is now finished. Again - DataTable's expressions, combined with Data Binding options offer a lot of options for developer to automate some simple number or text processing, without writing a lot of code. Expressions are not to be mistaken for SQL Server's computed column - they offer similar (and more limited) functionality, but they are performed on the client, which is a big plus.

Download the source code

To write your letter to Santa right now (it's not too late, you know), launch this tool through this ClickOnce Page and select "Letter to Santa".

VS2005 SP1 - uninstalling the Beta

 

If you're going to install Visual Studio 2005 SP1 on a machine, running the Beta version of the same service pack (VS2005 SP1 Beta), you'll have to uninstall that first, otherwise this nasty message will prevent you from installing the release version. To uninstall the SP1 Beta, go to Control Panel and review installed updates:

  • [Win XP] - Go to Add or Remove programs and check "Show Updates"
  • [Win Vista] - Go to Programs, Programs and Features and choose "View Installed Updates"

The VS2005 Service Pack 1 should appear right under the Microsoft Visual Studio 2005. Select "Service Pack 1 Beta" and choose to uninstall it.

It will take a while for SP1 Beta to uninstall and perhaps you'll need your copy of Visual Studio 2005 installation CDs/DVD handy, just in case some old files are needed to reinstall... After setup program is done uninstalling, you can continue installing the release version of VS2005 SP1.

VS2005 SP1 is ready

Yes, it's finally out. Visit the official SP1 web page and download your updates. If you're working on Windows Vista, there's another update in the making - "Visual Studio 2005 SP1 Update for Windows Vista" - it's currently still in Beta and looks like download is not available yet.

[Update: Visual Studio 2005 SP1 Update for Windows Vista Beta is now available for download]

Slovenia has Euro in Windows Vista already

Haven't noticed this until today, but it seems like the default currency for Slovenia has been set to Euro in Windows Vista RTM. Now, we're about to change our Slovenian Tolar (SIT) to Euro in about two weeks (1.1.2007) and we're still using Tolars at this moment, so this is kind of early to set such defaults.

Interestingly, this default has only been set with the fresh install of Windows Vista RTM. When upgrading to RTM from previous versions (RC1, 2, ...), the regional currency remained SIT. However, resetting to defaults will set the default currency to Euro:

If you've already installed Vista on your working computer and your regular daily work includes financial applications, watch out for these settings to avoid any surprises...

OpenSearch Vista Sidebar Gadget

My first Windows Sidebar Gadget started as a tool to support searching through Anthology ICE. About a month later, this tool developed into a general OpenSearch gadget, capable of querying any search engine that supports OpenSearch specification.

So, here's... the OpenSearch Windows Vista Sidebar Gadget:

 

Search terms are entered into the search box, pressing enter or clicking the search button request the results from the selected OpenSearch provider. Clicking on an item in the list displays the item's contents in a flyout window.

Currently, in v1.0, this gadget supports searching through the following sites:

The active search provider can be selected through the settings page. More search engines will be added in future versions, but if you know your way around Windows gadgets and javascript, you can add your favorite search engine for yourself [look at my previous post about this].

Some other features, planned for future releases, include:

  • The ability to add custom search engines
  • Support for async results retrieval
  • True OpenSearch support
  • Better UI
  • ...

Download OpenSearch this gadget [After downloading, unzip and install]

More gadgets coming soon...

[Update: you can also download this gadget from Windows Live Gallery]

Adventures in Calculus, pt. 1

Evaluating simple mathematical expressions in .NET may not appear as an easy task. I mean, how would you code something like Eval("1+1")? While there are more complex solutions to such problem, like using JScript's Eval.JScriptEvaluate(), you can also hook up the DataTable class and its Expression columns to perform some simple calculations.

For this post's example, I created a simple Calculator C# project, which allows a user to enter a simple math expression and watch the result being calculated while typing. There were very few lines of code required to code this sample, since most of the hard work is done by DataTable and Data Binding functionality.

Calculator

The expression can be entered by typing directly into the box or by pressing desired buttons. Each button's Click event is wired to the same button_Click handler, which additionally reduces the code line count:

private void button_Pressed(object sender, EventArgs e)
{
    display.AppendText(((Button)sender).Text);
}

The core of this project is the Calculator class, which holds the DataTable and actually performs the calculations. A DataTable with single column and single row, holding the default value (0), is generated in its constructor:

public Calculator()
{
    DataTable table = new DataTable();
    column = table.Columns.Add("Result", typeof(decimal));
    row = table.Rows.Add(new object[] { 0m });
}

Calculator class exposes two properties; one for expression, and one for the result. Expression property controls the Expression property of DataTable's only column, and Result only returns the value, being calculated by the DataTable:

[Bindable(true, BindingDirection.OneWay)]
public decimal Result
{
    get { return row.IsNull(0) ? 0 : (decimal)row[0]; }
}

[Bindable(true)]
public string Expression
{
    get { return column.Expression; }
    set { column.Expression = value; }
}

There you go, this is your math calculator engine. And when is the actual calculation performed, you ask? Well, that's the magic DataTable provides for you. By setting the DataColumn's Expression property to a (valid) expression, the value in this column will be automatically calculated by the Framework. 

To finalize this example, let's connect our input and result fields with the Calculator class. Two lines of Data Binding code are required:

display.DataBindings.Add("Text", calculator, "Expression", true, DataSourceUpdateMode.OnPropertyChanged);
result.DataBindings.Add("Text", calculator, "Result", true, DataSourceUpdateMode.Never);

display is a TextBox, used by user to enter the expression, which is passed to the Calculator class through Data Binding. Similarly, result TextBox will display whatever comes from the same class.

Well, that was easy, and you've now got yourself a fully functional calculator. In further posts, we'll explore additional calculation options DataTable provides.

Download this sample

[Update: you can try the compiled Calculator sample without downloading and compiling the code. Just run it through this ClickOnce page]

Search this blog

In one of my previous posts I wrote about OpenSearch and adding OpenSearch providers to your browser's Search toolbar. If you're using Community Server for your blog/forums engine, you can easily enable your visitors to take advantage of such providers to search through your site.

For example, when you visit my blog, you'll see a dropdown button next to browser's search icon turn orange. This means that browser [should be IE7, Firefox 2 or some other browser, supporting search providers] found a search provider on this page and has made it ready for you to use. Just enter the desired keywords, open the dropdown and select Andrej Tozon's Blog Search - your browser will take you to blog's search results page. To make this provider stay in your browser's search providers dropdown for future use, select Add Search Providers | Andrej Tozon's Blog Search from the same menu.

To implement this kind of search on your own site, create an OpenSearch description file, called opensearch-description.xml. This file describes your provider and should look something like:

<?xml version="1.0" encoding="UTF-8" ?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
  <ShortName>My Search</ShortName>
  <Description>Search my site</Description>
  <LongName>My Search Provider</LongName>
  <Tags>Blog, Community</Tags>
  <Url type="text/html" template="http://www.mysite.com/blogs/user/search.aspx?q={searchTerms}&o=Relevance" />
</OpenSearchDescription>

[Note: the above bold text represents a link, used by Community Server to display the search results page. If you're using a different kind of site engine and it supports searching, replace that link with the one your engine uses.]

Put the OpenSearch description file, you've just created, somewhere on your site, then edit one of your web pages, where you want your provider to be discovered [best place for this is usually the master template file]. Here's the contents that should go into this file's <head> section:

<link title="My Search" type="application/opensearchdescription+xml" rel="search" href="http://www.mysite.com/blogs/user/opensearch-description.xml" mce_href="http://www.mysite.com/blogs/user/opensearch-description.xml" lang="en" xml:lang="en" />

Make sure the link in the href attribute points to a valid OpenSearch description file you've created in previous step.

That's it, you're done. Navigate to your site with your browser and watch that icon go orange...

On a side note: I've also searchbox enabled our Developer users group portal [SLODUG/CodeZone-SI]; if you read Slovenian language, you're kindly invited to search through our posts...

Latest hotfixes for Visual Studio developers

Microsoft launched a "DevDiv Hotfix Public Availability Pilot Program", which will allow developers to access and download some of the latest hotfixes of their developer tools. Not many hotfixes are available at this time [the one that caught my eye was "FIX: You may receive an error message when you rebuild a solution and try to view a Windows Form in Design view in Visual Studio 2005."], but hopefully the number will increase in time. Of course, this hotfixes will be included in the forthcoming Visual Studio 2005 Service Pack 1.

This program is available through Microsoft Connect. And make sure you read the red text before downloading and applying any of the hotfixes...

Got .docx?

With 2007 Microsoft Office System out in the wild, there is a fair chance your next e-mail message will contain some Word 2007 formatted documents. Get ready and download Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats, which will help you open, edit and save those documents in your Word 2000, XP or 2003. The pack includes filters for Word, Excel and PowerPoint 2007 File Formats and can also be applied to equivalent Office 2003 Viewers.