In many enterprise applications there is the need that, regardless on which control you have the focus, that you can hit the 'Enter' and/or 'Esc' key to perform a default action. This behaviour is also common to web applications. On the Form control you find properties like AcceptButton and CancelButton, whereas the UserControl doesn't have these properties. The code below contains an AcceptButton and CancelButton that allows you to define a default action when the Enter or Esc key is pressed respesctively. public class UserControlEx : System.Windows.Forms.UserControl
{
private Button _acceptButton;
private Button _cancelButton;
public event EventHandler<EventArgs> AcceptEvent;
public event EventHandler<EventArgs> CancelEvent;
[Browsable(true)]
public Button AcceptButton
{
get { return _acceptButton; }
set { _acceptButton = value; }
}
[Browsable(true)]
public Button CancelButton
{
get { return _cancelButton; }
set { _cancelButton = value; }
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (msg.WParam.ToInt32() == (int)Keys.Enter)
{
OnAcceptEvent(EventArgs.Empty);
if (_acceptButton != null)
_acceptButton.PerformClick();
}
if (msg.WParam.ToInt32() == (int)Keys.Escape)
{
OnCancelEvent(EventArgs.Empty);
if (_cancelButton != null)
_cancelButton.PerformClick();
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected virtual void OnAcceptEvent(EventArgs args)
{
if (AcceptEvent != null)
AcceptEvent(this, args);
}
protected virtual void OnCancelEvent(EventArgs args)
{
if (CancelEvent != null)
CancelEvent(this, args);
}
}
In some enterprise applications you need to show geographical data such as countries and postcodes. Most of the time you need it for a registration page, where the user need to fill in the country and postcode/area. Geonames is a free geographical database that contains over 8 million geographical names and it can be accessed through a number of webservices. For example the url http://ws.geonames.org/countryInfo? gives an xml with all countries, whereas the following request http://ws.geonames.org/postalCodeSearch?placename=be gives us all postcodes for a particular country (e.g. Belgium). Most likely you need two dropdown lists, one for countries and one for postcodes, where the postcode dropdown is dependent from the country dropdown list. This is a very good example to introduce AJAX by using the CascadingDropdown that is included in ASP.NET AJAX. To implement this functionality we need to implement two methods on a webservice, namely GetCountries and GetPostalCodesByCountry. The GetCountries simply returns all countries sorted by name and looks like this: [WebMethod]
public CascadingDropDownNameValue[] GetCountries()
{
List<CascadingDropDownNameValue> list = new List<CascadingDropDownNameValue>();
CountryItemCollection countries = IStaySharp.Geonames.GeonamesService.GetAllCountries();
for (int i = 0; i < countries.Countries.Length; i++)
{
list.Add(new CascadingDropDownNameValue(
countries.Countries[i].CountryName,
countries.Countries[i].CountryCode));
}
list.Sort(CompareCascadingDropDownNameValueByName);
return list.ToArray();
}
Note that the list need to be converted to an array of CascadingDropDownNameValue objects. Note that we also sort the list by implementing a delegate named CompareCascadingDropDownNameValueByName. private static int CompareCascadingDropDownNameValueByName(CascadingDropDownNameValue x, CascadingDropDownNameValue y)
{
if (x == null && y == null)
return 0;
else if (x == null && y != null)
return -1;
else if (x != null && y == null)
return 1;
else
return x.name.CompareTo(y.name);
}
The other webservice method, called GetPostalCodesByCountry, need to retrieve all postcodes for a particular country. The signature of the method is very strict. The parameter names must be named 'knownCategoryValues' and 'category', otherwise it will fail! [WebMethod]
public CascadingDropDownNameValue[] GetPostalCodesByCountry(string knownCategoryValues, string category)
{
List<CascadingDropDownNameValue> list = new List<CascadingDropDownNameValue>();
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
if (kv.ContainsKey("Country"))
{
string countryName = kv["Country"];
PostalCodeItemCollection postalCodes = IStaySharp.Geonames.GeonamesService.GetPostalCodes(countryName);
for (int i = 0; i < postalCodes.PostalCodes.Length; i++)
{
list.Add(new CascadingDropDownNameValue(
string.Format("{0} ({1})", postalCodes.PostalCodes[i].PostalCode, postalCodes.PostalCodes[i].Name),
postalCodes.PostalCodes[i].PostalCode));
}
}
list.Sort(CompareCascadingDropDownNameValueByName);
return list.ToArray();
}
In order to complete the webservice, the attribute ScriptService (line 3) need to be included so that a client javascript proxy can be generated. You can test this by calling your webservice like this http://localhost:9999/GeonamesService.asmx/js. [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class GeonamesService : System.Web.Services.WebService
{
...
}
Finally we only need to add two CascadingDropDown controls on our aspx page with the following settings: <asp:ScriptManager ID="scriptManager" runat="server" />
<asp:DropDownList ID="countriesDropDown" runat="server"/>
<ajaxToolkit:CascadingDropDown
ID="countriesCascadingDropDown"
TargetControlID="countriesDropDown"
Category="Country"
PromptText="Please select a country"
LoadingText="[Loading countries...]"
ServicePath="/GeonamesService.asmx"
ServiceMethod="GetCountries"
runat="server"/>
<br/><br/>
<asp:DropDownList ID="postalCodesDropDown" runat="server"/>
<ajaxToolkit:CascadingDropDown
ID="postalCodesCascadingDropDown"
TargetControlID="postalCodesDropDown"
Category="PostalCode"
PromptText="Please select postalcode"
LoadingText="[Loading postalcodes...]"
ServicePath="GeonamesService.asmx"
ServiceMethod="GetPostalCodesByCountry"
ParentControlID="countriesDropDown"
runat="server"/>
The source code can be downloaded here: IStaySharp.AJAXSample.rar (332,56 KB)
Scott Guthrie is coming for a presentation on February 1st. The session will take place at Utopolis Mechelen. Please subscribe to the event through the VISUG site. Two presentations will be given, namely: - First Look at Visual Studio and ASP.NET 'Orcas'
- ASP.NET 2.0 and ASP.NET AJAX Tips and Tricks
See you there!
Virtual PC 2007 RC1 has been released, and can be downloaded on the Microsoft Connect site, if you participated in the beta tests. The main new features are:
- Support for Windows Vista™ as a host operating system
- Support for Windows Vista™ as a guest operating system
- Support for 64-bit host operating systems
- Support for hardware-assisted virtualization
- Built-in support for network installations
More details about the release notes can also be downloaded on the Microsoft Connect site.
The Validation Application Block (VAB) of the upcoming Enterprise Library v3, uses attributes to describe validations. This gives us for example the opportunity to generate ASP.NET validators based on the attributes decorated on the properties. Take for example the NotNullValidator of VAB, this can be translated to a RequiredFieldValidator, whereas the RegexValidator can be translated to RegularExpressionValidator. You can go further with the NotNullValidator and mark required fields with a different backcolor and adding an asterix (*) to the end of the control. I am big fan of the DetailsView control, you can simply bind a DataSource control to it, and it will automatically provide you with a caption to each control and two-way binding. Below you find an example how you can extend the BoundField control, that investigates the NotNullValidator attribute of VAB. Note that I am currently extending it for the other set of validators and in a more OO way. More info will follow later. public class BoundFieldEx : System.Web.UI.WebControls.BoundField
{
public override void InitializeCell(
DataControlFieldCell cell, DataControlCellType cellType,
DataControlRowState rowState, int rowIndex)
{
base.InitializeCell(cell, cellType, rowState, rowIndex);
if ((((rowState & DataControlRowState.Edit) != DataControlRowState.Normal) && !this.ReadOnly) ||
((rowState & DataControlRowState.Insert) != DataControlRowState.Normal))
{
TextBox textBox = null;
if (cell != null && cell.Controls.Count > 0)
textBox = cell.Controls[0] as TextBox;
if (textBox != null)
{
Type dataItemType = null;
if (DataBinder.GetDataItem(base.Control) != null)
dataItemType = DataBinder.GetDataItem(base.Control).GetType();
if (dataItemType != null)
{
ValidatorAttribute attribute = IsRequired(dataItemType, base.DataField);
if (attribute != null)
{
string textBoxID = this.DataField;
textBox.ID = textBoxID;
RequiredFieldValidator validator = new RequiredFieldValidator();
validator.ControlToValidate = textBoxID;
validator.ID = string.Concat("RequiredValidatorOf", textBoxID);
validator.Display = ValidatorDisplay.Dynamic;
validator.ErrorMessage = attribute.MessageTemplate;
cell.Controls.Add(validator);
}
}
}
}
}
private ValidatorAttribute IsRequired(Type dataType, string property)
{
PropertyInfo propertyInfo = dataType.GetProperty(property);
if (propertyInfo != null)
{
foreach (Attribute attribute in propertyInfo.GetCustomAttributes(true))
{
if (attribute is NotNullValidatorAttribute)
return attribute as ValidatorAttribute;
}
}
return null;
}
}
Now you create a custom business object, called Customer, and bind it to the DetailsView through an ObjectDataSource. public class Customer
{
private string _firstName;
private string _lastName;
[NotNullValidator(MessageTemplate="Firstname cannot be empty")]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
[NotNullValidator(MessageTemplate="Lastname cannot be empty")]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public Customer Fill()
{
Customer customer = new Customer();
customer.FirstName = "Christoph";
customer.LastName = "De Baene";
return customer;
}
}
Inside your aspx page you have something like <asp:ObjectDataSource id="customerDataSource"
TypeName="IStaySharp.Business.Customer, IStaySharp.Business"
DataObjectTypeName="IStaySharp.Business.Customer, IStaySharp.Business"
SelectMethod="Fill"
runat="server">
</asp:ObjectDataSource>
<asp:ValidationSummary runat="server"/>
<asp:DetailsView DataSourceID="customerDataSource" DefaultMode="Edit" AutoGenerateRows="false" runat="server">
<Fields>
<rfx:BoundField HeaderText="Firstname" DataField="FirstName"/>
<rfx:BoundField HeaderText="Lastname" DataField="LastName"/>
</Fields>
</asp:DetailsView>
Here is the result if you leave the properties empty:

Tom Hollander revealed some features/scenarios about the upcoming Validation Application Block that will be included in Enterprise Library v3. There are already some validation libraries available, for example The fact that you can define rules through configuration is really cool. I can't wait for the CTP!
VISUG has a brand new website. All the necessary features are already available and more functionality is coming soon. The two last VISUG events for year 2006 are announced. One of the events is a panel discussion about exception handling and logging, and it will take place at Real Software (my home). The other event is a geek dinner where we can discuss about... you know what I mean ;). Topic: Discussion on Enterprise Exception Handling and Logging
Description: This is a session where you (yes, you!) can discuss exception handling and logging in enterprise applications. Bring your tips and tricks, problems and exception handling frustrations with you, and we'll all learn about best practices together! No slides or preparation required! Don't want to discuss? No problem, you're not required to say anything, but we hope you will. To help with a smooth discussion, it will be lead by a panel of 3 to 4 people. If you're interested to sit on this panel, you're invited (First-come, First-served basis).
When: Monday 11 December, 18:30 - 20:30
Where: Real Software (Prins Boudewijnlaan, 2550 Kontich, Route) Register: VISUG website
See you on both events!
This macro enables you to nest project items inside Visual Studio .NET. Until now, there is no easy way to nest project items through the Visual Studio IDE, you can only do it by manipulating the project (.csproj or .vbproj) file and adding the DependentUpon element.

Inside the IStaySharp.vsmacros file there is a macro called 'Create Dependency' which allows you to nest two selected items. I have even created a video (672,18 KB) to illustrate how to configure and use the macro.
PowerShell 1.0 has been released for Windows XP SP2 and Windows Server 2003. The bits can be downloaded here. If you already have installed a previous release of PowerShell, you need to uninstall by selecting 'Show Updates' in the 'Change or Remove Programs'. 
One of the oldest and most basic programs is certainly the command prompt (cmd.exe). One thing is certain, you still can't do without the command prompt, and certainly being a developer. I came across a nice tool, called Console, which enhances the command prompt. I have been using it for a couple of months now, and I really like it. One of the things you should really customize is your PROMPT. More information about changing the PROMPT can be found here. Having, for example, your current directory path on a separate line is really useful. You can customize it through the environment variable called PROMPT, like Scott is doing, but I prefer passing it as a parameter to the cmd.exe. This way I can easily copy the application and no reboot is required. This can be done by the /k argument of the cmd.exe. Here is a snippet of my XML configuration file for Console. <tab title="Console">
<console shell="cmd /k PROMPT $p$_$+$g" init_dir=""/>
<cursor style="11" r="255" g="255" b="255"/>
<background type="2" r="0" g="0" b="0">
<image file="" relative="0" extend="0" position="0">
<tint opacity="190" r="0" g="0" b="0"/>
</image>
</background>
</tab>
<tab title="cmd">
<console shell="cmd.exe /k PROMPT $p$_$+$g" init_dir=""/>
<cursor style="11" r="255" g="255" b="255"/>
<background type="0" r="0" g="0" b="0">
<image file="" relative="0" extend="0" position="0">
<tint opacity="0" r="0" g="0" b="0"/>
</image>
</background>
</tab>
<tab title="VS.NET 2005">
<console shell="cmd /k PROMPT $p$_$+$g && C:\PROGRA~1\MID05A~1\VC\vcvarsall.bat" init_dir=""/>
<cursor style="0" r="255" g="255" b="255"/>
<background type="2" r="0" g="0" b="0">
<image file="" relative="0" extend="0" position="0">
<tint opacity="188" r="0" g="0" b="0"/>
</image>
</background>
</tab>
<tab title="PowerShell">
<console shell="C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" init_dir=""/>
<cursor style="0" r="255" g="255" b="255"/>
<background type="2" r="0" g="0" b="0">
<image file="" relative="0" extend="0" position="0">
<tint opacity="189" r="0" g="0" b="0"/>
</image>
</background>
</tab>
This is the result in Console

I am currently building software factories and I needed a way to have a list of running Visual Studio instances and the corresponding EnvDTE._DTE object to manipulate the solution. Windows internally keeps a list of COM objects that are currently running, called the Running Object Table (ROT). VS .NET 2005 for example, register itself in the ROT as "!VisualStudio.DTE.8.0:pid" where 'pid' is the process id of the corresponding VS 2005 instance. In .NET 1.1 you would use the the following UCOMIRunningObjectTable, UCOMIBindCtx for enumerating the ROT. In .NET 2.0 these interfaces are obsolete and are replaced by IRunningObjectTable and BIND_OPTS respectively. Note that the same code can be used for getting other instances like MS Word, IE, etc. using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using EnvDTE;
public static class DTEHelper
{
const uint S_OK = 0;
[DllImport("ole32.dll")]
public static extern uint GetRunningObjectTable(uint reserved, out IRunningObjectTable ROT);
[DllImport("ole32.dll")]
public static extern uint CreateBindCtx(uint reserved, out IBindCtx ctx);
static IDictionary<string, object> GetRunningObjectTable()
{
IDictionary<string, object> rotTable = new Dictionary<string, object>();
IRunningObjectTable runningObjectTable;
IEnumMoniker monikerEnumerator;
IMoniker[] monikers = new IMoniker[1];
GetRunningObjectTable(0, out runningObjectTable);
runningObjectTable.EnumRunning(out monikerEnumerator);
monikerEnumerator.Reset();
IntPtr numberFetched = IntPtr.Zero;
while (monikerEnumerator.Next(1, monikers, numberFetched) == 0)
{
IBindCtx ctx;
CreateBindCtx(0, out ctx);
string runningObjectName;
monikers[0].GetDisplayName(ctx, null, out runningObjectName);
Marshal.ReleaseComObject(ctx);
object runningObjectValue;
runningObjectTable.GetObject(monikers[0], out runningObjectValue);
if (!rotTable.ContainsKey(runningObjectName))
rotTable.Add(runningObjectName, runningObjectValue);
}
return rotTable;
}
public static IDictionary<string, _DTE> GetRunningVSIDETable()
{
IDictionary<string, object> runningObjects = GetRunningObjectTable();
IDictionary<string, _DTE> runningDTEObjects = new Dictionary<string, _DTE>();
foreach (string objectName in runningObjects.Keys)
{
if (!objectName.StartsWith("!VisualStudio.DTE"))
continue;
_DTE ide = runningObjects[objectName] as _DTE;
if (ide == null)
continue;
runningDTEObjects.Add(objectName, ide);
}
return runningDTEObjects;
}
}
The Microsoft Patterns & Practicess Team recently released a new version of the Web Client Software Factory that can be found on CodePlex. One of the things I noticed directly, is that they implemented a web version of the Composite UI based on the ObjectBuilder. The goals of Web Client Software Factory are: - CAB for Web
- Hiding complexity
- Separation of infrastructure & biz logic
- Biz logic reuse amongst different UI Technologies
- Promoting consistent development practices
- Navigation
- UI Richness
- UI Layout management
- State management
- Best use of technology available (Ajax, WinForms controls, ...)
- Security
- SaaS implications on application design
More information about the vision & scope can be found here.
On IStaySharp.NET I created an article that shows you, how you can load a DSL file. This is necessary if you need to access your model from GAT, a Visual Studio AddIn, custom library, etc. The last couple of weeks I am digging into DSL and GAT for creating a set of software factories at Real Software that will be used as a baseline for building applications. Learning two (DSL & GAT) simultaneously and combining them was not so trivial and there is some learning curve. For GAT there are some interesting resources: The MSDN forums is a good resource when you have some issues and/or questions. DSL has also a specific forum. The capabilities of DSL are really impressive. Now you have finally the tools to write for example your own dataset designer with code generation!!!
Last week I bought an external harddisk, Vantec Nexstar 3 (NST-360SU-BK), that can be connected through USB 2.0 and eSata. eSata is simply a new standard for connecting external SATA drives at full speed! I use the external harddisk to share data between my laptop (through USB 2.0) and my desktop PC (through eSata). I use extensively Virtual PC for development, and it's a good thing when developing on my desktop PC I can use the full speed of my HD.
Microsoft released the beta program for Virtual PC 2007. The new features are - Hardware-assisted virtualization
- Support for Windows Vista as a guest and host operating system
- Support for 64-bit host operating system
You can participate on the beta program through the Microsoft Connect web site.
|