Skip navigation

Tag Archives: c#

Here are 10 helpful hints if you are considering using AJAX in an upcoming project:

  1. You don’t need an AJAX library
  2. Download Aptana. Make it your default .js editor inside Visual Studio.
  3. Place all Javascript in external files (like codebehind)
  4. Instead of making invoking requests directly from your pages, build a class library that does the dirty work.
    hint: once you have these base libraries, add the files the code assist window to get auto-complete inside Aptana. Aptana lets you add documentation comments as well as parameter and return type information. Use it!
  5. You don’t need an AJAX library.
  6. You can make classes in Javascript too.
  7. You don’t have to define seperate functions for async callbacks from webservices. Use anonymous methods to inline your code in the calling method.
  8. Consider providing ways to extend standard HTML elements rather that trying to make up all sorts of funky controls. Controls have a place, but consider the common example of a grid. A grid is really just a table. Don’t create a grid class, provide a way extend the table and add column sorts, row hover handling, row selection, etc. to it. Then, you can use standard HTML layouts and design and simply call a function or two to inject the features you want.
  9. Make sure to secure your AJAX server side.
  10. You don’t need an AJAX library.

Why do I point out over and over that you don’t need an AJAX library? Because all the AJAX hype out there makes the problems out to be a lot more difficult than they are. I would recommend one little script to abstract away SOAP or JSON requests, since you probably don’t want to build that manually, but you don’t really need much else to get started building AJAX into your applications. The most important thing to do is get a solid understanding of Javascript and CSS and make sure that you use standard DOM properties as much as possible. A great editor like Aptana can also be an extremely valuable tool. This isn’t to say that using an AJAX library is a bad thing, just that you can get by fine without one if you would rather not add the extra dependancies and complexity to your app. The browser differences are not nearly as bad as the mobs might have you believe.

Also keep in mind that you don’t have to make every element clickable and every node draggable. Web apps are not windows apps. You don’t need one base page that everything is dynamically added to and removed from. On the contrary, it is nice to have back and forward buttons that actually do something. A lot of improvement can be made by simply adding a few AJAX callbacks to eliminate those annoying pages you used to have to make with 2 lines of text and a button.

Windows Mobile has had a run dialog similiar to the one found in desktop versions of Windows since Windows 95 for as long as I can remember. This feature is not well known however.

On the main Today screeen of a Windows Mobile powered Pocket PC (Windows Mobile Professional or Classic for those up with the current lingo) you can tap and hold down the ‘Action’ key while tapping and holding the stylus on the clock shown on the nav bar. The ‘Action’ key is usually the center of the D-Pad or Ctrl if you are using the emulator.

When you release the ‘Action’ key a small popup menu will appear, and you can select the ‘Run’ option which will display a small dialog.

Within this dialog you can enter the path to any executable, as well as any required command line parameters.

Examples you may like to try out are as follows:

  • calc.exe
  • iexplore.exe
  • ctlpnl.exe cplmain.cpl,4,0

For more details on accessing control panel applets from the command line, like the last example demonstrates refer to the Using Control Panel article on the Pocket PC Developer Network website.

This feature can be handy during development of applications for tasks such as re-registering COM dlls, however it is rather impractical for day to day use due to the typical slowness of entering the command line and the fact that unlike on the desktop there is no history of previous commands.

As an additional tip, did you know that if you tap and hold without the ‘Action’ key being pressed a different popup menu will appear? This one allowing you to select between an analogue and digital clock being displayed on the navbar.

Christopher Fairbairn

Another finishing touch that I like to see in applications that use ListViews is the ability for the end user to re-order the columns to suit their own preferences. This blog entry discusses one approach for adding this functionality to the ListView control present within the .NET Compact Framework.
Although it is difficult to convey in a static screenshot, the screenshot above shows a user dragging the stylus over the header of the listview control to move the position of the “First Name” column.

Obtaining Draggable Columns

The System.Windows.Forms.ListView control is a wrapper over top of the native ListView control. The native ListView control supports the notion of extended styles, which allow various optional features to be enabled or disabled as desired. One of the extended styles is called LVS_EX_HEADERDRAGDROP. If this extended style is enabled the user can re-order the columns by dragging and dropping the headers shown at the top of the listview while it is in report mode.

Although the .NET Compact Framework ListView control does not expose a mechanism to enable extended styles, we can use a technique discussed in a previous blog entry of mine to add or remove the LVS_EX_HEADERDRAGDROP extended style as desired.

private const int LVM_SETEXTENDEDLISTVIEWSTYLE = 0x1000 + 54;private const int LVS_EX_HEADERDRAGDROP = 0x00000010;

public static void SetAllowDraggableColumns(this ListView lv, bool enabled){// Add or remove the LVS_EX_HEADERDRAGDROP extended// style based upon the state of the enabled parameter.Message msg = new Message();msg.HWnd = lv.Handle;msg.Msg = LVM_SETEXTENDEDLISTVIEWSTYLE;msg.WParam = (IntPtr)LVS_EX_HEADERDRAGDROP;msg.LParam = enabled ? (IntPtr)LVS_EX_HEADERDRAGDROP : IntPtr.Zero;

// Send the message to the listview controlMessageWindow.SendMessage(ref msg);}

This method allows the drag feature to be turned on and off for a given ListView control. Notice that this method makes use of a C# 3.0 feature called Extension Methods. The “this” keyword in front of the first parameter means that this method can be called as if it was part of the standard ListView control, meaning the following code snippet will work (assuming listView1 is an instance of the System.Windows.Forms.ListView control).

listView1.SetAllowDraggableColumns(true);

This is pure syntactic sugar, behind the scenes the C# compiler is simply passing in listView1 as the first parameter to the SetAllowDraggableColumns method.

Persisting Column Order Preferences

Once you have reorder-able columns it can be desirable to persist the user’s preferred layout across multiple executions of your application. It would be a pain if the columns always defaulted back to a standard order everytime the form was displayed.

The native ListView control provides two window messages, LVM_GETCOLUMNORDERARRAY and LVM_SETCOLUMNORDERARRAY that can be used to implement this feature. The code sample available for download wraps up these two window messages to allow you to query the current order of the columns by using a statement such as the following:

int[] columnOrder = listView1.GetColumnOrder();// TODO: save 'columnOrder' to the registry// or another persistent store

When columns are added to a ListView they are given an index. The first column is column 0 while the second is column 1 and so on. When columns are re-ordered they keep their index value but their position on screen changes. The array returned by the GetColumnOrder function contains the index for each column in the order that they are visible on screen. For example if the array contains the values 2, 0, and 1 it means that the last column (column 2) has been dragged from the right hand side of the listview to become the left most column.

Once we have obtained the order of the columns we can store the data in any persistent storage mechanism such as a file, a database table, or registry key. When the form is reloaded we can initialise the default order of the columns by calling the equivalent SetColumnOrder method with the value we previously saved:

// TODO: should read 'columnOrder' from the registry// or other persistent storeint[] columnOrder = new int[]{2, 0, 1};

listView1.SetColumnOrder(columnOrder);
Christopher Fairbairn

I’ve been looking these days to extend the GridView native class so i can have export to Excel. So i have tried and built another class, that will make the export. The class doesn’t relay on a specific control, but on a list of objects, every object also having a child list of objects (list meaning everything what IEnumerable means). In this way, for every property in a class a new column in XSL can be generated.

Here’s the code (alin.berce – RONUA):

using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public class ExportToXls
{
public static void Export(string fileName, GridView gv, string title)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(“content-disposition”, string.Format(“attachment; filename={0}”, fileName));
HttpContext.Current.Response.ContentType = “application/ms-excel”;

using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
try
{
// render the table into the htmlwriter
RenderGrid(gv).RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(“

” + title + “

“);
HttpContext.Current.Response.Write(“Generated in: ” + DateTime.Now.ToString());
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
finally
{
htw.Close();
}
}
}

private static Table RenderGrid(GridView grd)
{
// Create a form to contain the grid
Table table = new Table();
table.GridLines = grd.GridLines;

// add the header row to the table
if (grd.HeaderRow != null)
{
ExportToXls.PrepareControlForExport(grd.HeaderRow);
table.Rows.Add(grd.HeaderRow);
}

// add each of the data rows to the table
foreach (GridViewRow row in grd.Rows)
{
//to allign top
row.VerticalAlign = VerticalAlign.Top;

ExportToXls.PrepareControlForExport(row);
table.Rows.Add(row);
}

// add the footer row to the table
if (grd.FooterRow != null)
{
ExportToXls.PrepareControlForExport(grd.FooterRow);
table.Rows.Add(grd.FooterRow);
}
return table;
}

private static void PrepareControlForExport(Control control)
{
for (int i = 0; i

Few week ago i was playing with Google API trying to make an online tracker and offline data viewer. I mess it up lamentably. Cause of many thing, but I’m concentrate in here only over the most important ones.

1. Lack of support and integration for FAI recommended formats. As a sample, .IGC files are not supported by GE. Well, i said i can handle that. Using a 3′rd party freeware application and a script for auto processing the .IGC files, i was converting all files while i was starting the application. Task done !

2. Lack of Google Earth options available in GE API. Hell, I’ve found like maximum(probably less though) 15 functions/extends that i can use in my code. I found out in the end, it’s lost time. Everyday i worked on this project means lost time.

3. Problems integrating GE in your application. HA ! Was a mess trying this. You loose focus, need to PInvoke to hide screens, windows. Even more, it’s not only implementing (at the beginning the map only), but the controls too.

Final conclusions. They have a lot to work so make this API developer related, for all kinds of developers, for novice ones till guru’s. Thought it’s a start.

Hell, yesterday i got a new task. To implement for our company mobile software applications a IPhone gesture based user interface(UI). It’s really shitty, no matter what. In fact, the main problem is that i have to use a complex neural network algorithm combined with gesture management. Normally, it will take like 3 months to complete the this, except the 3D part using DirectX(preferably) or OpenGL.

Then the problems starts. Creating a 3D interface using DX implies a lot of things, and if we do a economic feasibility study we get nowhere. Technically it’s feasible, at the limit though.

Been founding this: .NET C# Application to Create and Recognize Mouse Gestures

Looking around over it, seems to me that it’s a point of start. I’ll try contact Daniele to talk with him about this. Till then, I’ll put an eye over the code provided.

More informations and links about this can be found on the following websites:

Artificial neural network
Project Doc-WMG
Mouse gesture recognition

PS: I have forgot, Liverpool won 4-2 against Arsenal. Great game ! Bring the Cup to the KOP !!

Follow

Get every new post delivered to your Inbox.