Disabling the Browser Context Menu

Date: Monday, 18 Dec 2006 14:14
By Bret Taylor, December 3, 2006

You can disable the browser context menu for an element in your DOM with the following JavaScript recipe:

function disableContextMenu(element)
element.oncontextmenu = function()
{ return false; }

For example, to disable the context menu over a photo on your page, you could use:


Browsers like Firefox do not always allow web pages to disable the context menu (this option is a setting in Firefox), so this recipe will not work 100% of the time in all browsers.

See the example

JavaScript Debug Log

Date: Monday, 18 Dec 2006 14:08
By Bret Taylor, December 3, 2006

Even with the most advanced debugging tools, every program requires a little “printf debugging”: run your program, print out state as it executes, and visually verify that everything is working properly. Unfortunately, there is no built in console to print debug messages to in JavaScript, forcing many JavaScript programmers to revert to calling alert() in their code. alert is completely infeasible for a function that gets called a lot, as it halts execution until you click an OK button. This recipe creates a simple debug log window that does not halt execution:

function log(message) {
if (!log.window_ log.window_.closed) {
var win = window.open("", null, "width=400,height=200," +
"scrollbars=yes,resizable=yes,status=no," +
if (!win) return;
var doc = win.document;
doc.write("<html><head><title>Debug Log</title></head>" +
log.window_ = win;
var logLine = log.window_.document.createElement("div");

To print log messages, you just call log() within your code:

for (var i = 0; i < 10; i++) {
log("This is log message #" + i);

When you push your code to production, you can just replace the definition of log with an empty function so your users will not see your debug messages.

See the example

Only well documented software will survive!

I remember when I started my previous job at 1999 as a software developer I’ve asked
my manager about the standards and policies of the company over the documenting. It wasn’t part of the requirements to produce any technical document that would help to see what is going on in the software. Some people started design documents and in beginning days it has been seen as a nice luxury to have if time allow us. Soon when the first phases of software delivery were over and maintenance became an issue every developer was asking the management to free some time for documentation. Unfortunately the need for documentation was not so crucial for the management as it was for the software developers that were responsible for maintenance of application that have been designed years behind.

In the meantime something else was going on which was very much related to this. First a new requirement was raised by the users. The Analysts brought them to developers to talk about the possibilities. Those architects being sharper than the rest took the opportunity and came up with a solution which was promised to rock. Some raised possible issues but very soon they were rejected as the solution was so “flexible” that those changes could be captured. In the beginning phase, just before users get a version to go live, when the concept is not proven to the users as it is for the architects, the new system can capture every requirement users want.. that seems a success, a big applause!

The architect gets promoted and the software is now in the hands of software developers. The product goes live and users are buying it. From time to time small change requests come to the development department and the responsible developer tries to figure out how it can be implemented. He realises that to get the software robust, he needs more time than expected and he learns not to underestimate the efforts. Next time a request comes he starts to tell the analyst that it is not that easy to implement this request and soon the requests make the developer frustrated. “It seems like they never make up their mind properly and keep changing their mind” he mumbles into his screen sometime while digging into the implementation. There comes a day that one of the requests is becoming too expensive to deliver and therefore the analyst finds a nice way of putting it to the request note to get it rejected. That developer may even leave the company and another one takes the place (they always do), but soon the same happens with the new developer and finally the management realises that “something should be done!” That is when management acknowledge the need for documentation.

One thing I have learned so far is that in IT business everyone is very busy and
nobody gets enough time.

I used to put the responsibility to the management’s shoulders to free up some time for me to get the documentation done. They couldn’t come up with nice and easy paste of project planning and it seemed always to be squeezed plan short on resources and the users want to get the product soon and some one made nice offer just to get the deal and so it is up to development to implement the new requirements. I had an impression that my manager tried to tell me (in a way) that I shouldn’t buy it if the requirements are not clear and detailed. Lost of time I happened to come across the situation where the current system isn’t really suitable to get the new requirements implemented. I should have seen it if I could get the requirements clear and detailed, and also if I know my system pretty well.

At the end of those days, I now realise that it was me whoI maintained that software. And it was me who suffered from the frustration and gave higher estimates for something that actually could have been done easier. And it was me who should have make the documentation happen. One could argue and say hey the time was not given, but I always believed that writing software is an act of art and I am the creator of it and at the end of the day the world is how we make it. So I should not have created any crap.

Fortunately, these days managers are more and more appreciating the documentation and it seems like they do their best to support the development
and free some time for documentation. That is very nice feeling when you have such a manager supporting you and that is how you can deliver maintainable software: Making good documentation at any costs.

In the past ten years I have seen tremendous changes in the way software is developed and maintained. Although there are plenty of methodologies I haven’t even thought about but it is remarkable that in this small world of mine companies have grown their IT departments into a dedicated service businesses. Companies are getting about 10 year service contract when buying the software. That is probably more than the service lifetime of a version of any big software like Oracle or even windows. Companies are investing huge money into the IT products and they want to get the best quality and longest support. It is not a shop-as-you-go business. It is rather a long-term relationship with support for change requests, as the market and the human needs are growing rapidly in these days. And one thing we all know; peoples demands never stops. So we need to make our software ready for that. We can’t assume that the system will cater all kind of situations and we need to document all aspects of it to make the changes coming in the near future possible.

So, who knows what I am writing right know? And who would want
to know that in 10 years over and over? It is me. And I can not remember it right if I wouldn’t right it down.

Asynchronous Invocation Using BackgroundWorker / Threading / C#

Sample Image


When you have a function that takes a long time and it freezes the screen, the user gets nervous. They simply don’t like to lose the control. It would give a friendly impression when they can still click on the form while the application is busy with getting the information, for example. When you start implementing asynchronous methods, you will wonder which technique is best suitable for your application. Would you use a Timer class or a Thread class or a BackgroundWorker? This article uses a BackgroundWorker class to show how easy it is, and where you need to pay attention when using it.


I have tried to compare different methods of implementing Asynchronous Invocation in my weblog. With Microsoft Visual Studio 2005 , I discovered this class which is new to me.

Using the code

I have put extra comments everywhere possible, to make the sample a snippet of code you can use to start your own implementation. The code implements a very simple action, which is finding files in a directory to simulate the time consuming action. This is the place where you need to change. For the rest, the skeleton should be kept the same.

The background worker is able to send an event to the called thread to change the screen appearance. That is very easy, and I thought it would be easy for everyone to discover. But what if that is not enough and you would like to send other messages back to the caller? So I used BeginInvoke to invoke a call on the caller thread.

I have two delegates used from the sub thread, to call the method implemented in the main thread.

private delegate  void PaintMessage(TreeNode root,                                     string childText); private PaintMessage myAddNode = null;

The OnPaontMessage implements the adding of a newly found file into an existing (given) root. This is called when the sub-thread finds a new item.

private  delegate void EnableCancel(bool enabled); private EnableCancel myEnableCancel = null;

The second delegate is to indicate that the thread has been started and that it could be stopped. It is implemented by the main thread to enable the Cancel button.

Starting the action will be done by calling the RunWorkerAsync method on the worker.


When a sub-folder is found, I send a notification to the main thread to create a node for the sub-folder. Then, I use that node to create a file node in it. But the dilemma is that I can not proceed with filling the file, if the main thread didn’t get the chance to create the sub folder. Thus, I need to wait and let the main application take the chance to create it. One simple way to do this is the following loop:

// Create a folder node before proceeding IAsyncResult ar = BeginInvoke(myAddNode, new object[]                   { root, di.Name }); // Update the screen  // Wait until the folder has been created before // proceeding with the content of the folde while (!ar.IsCompleted) {     Application.DoEvents();     ar.AsyncWaitHandle.WaitOne(-1, false); }

First, I invoke the delegate and I keep the reference to it. I use the reference to see if it has been completed. And in the loop, I let the application take the chance and do its events. Meanwhile, I tell my sub-thread to wait a bit.

Searching tables and columns

I am working with some big databases having similar tables for different group.
It seems to be poluted by different parties and have no administrator. Some developers
put their application initials as a prefix of the table names and as a result we
got several Contact tables and several Items each with diffeent layouts.

When I am dealing with these tables I need to find everything to do with lets say
Code. I need to find out all the tables having a field like Tax_Code or TaxCode
or ItemCode etc. It might be even good to know if there are tables called like Meta_Code
or Item_Code etc.

To find these two things I have written two stored procedures to use as follows

sp_FindColumns ‘Code’
sp_FindTables ‘Code’

When I was using these two I realised that also some stored procedures
are dealing with changing a field internally. So I had to also write something to
search the definitions of the stored rocedures to find al those mentioning the field
in the code and I came up with the following procedure:

sp_FindInProcedure ‘Tax_Code’

.And below are the defenitions for them.


if exists (select * from sysobjects
where id = object_id(N‘[dbo].[sp_FindColumns]’) and OBJECTPROPERTY(id,
N‘IsProcedure’) = 1)
drop procedure [dbo].[sp_FindColumns]

Create Procedure sp_FindColumns
@sToSearch varchar(255)
/************************************************************************************* *
* sp_FindColumns
* This stored procedure will look into all table collumns
* search the string which is given as the only parameter.
************************************************************************************* * By Asghar Panahy
* 12-jun-2006

SELECT SO.name + ‘.’ + Sc.name
FROM SysColumns Sc
Inner Join SysObjects SO
on SO.ID = SC.ID
Where SO.xtype=‘U’
And Sc.Name like ‘%’ + @sToSearch + ’%’



if exists (select * from sysobjects
where id = object_id(N‘[dbo].[sp_FindTables]’) and OBJECTPROPERTY(id,
N‘IsProcedure’) = 1)
drop procedure [dbo].[sp_FindTables]

Create Procedure sp_FindTables
@sToSearch varchar(255)
/************************************************************************************* *
* sp_FindTables
* This stored procedure will look into all table names
* search the string which is given as the only parameter.
************************************************************************************* * By Asghar Panahy
* 12-jun-2006
SELECT Distinct So.Name
FROM SysColumns Sc
Inner Join SysObjects SO on SO.ID = SC.ID
Where So.xtype= ’U’
And SO.Name like ‘%’ + @sToSearch + ‘%’


if exists(select * from sysobjects where id = object_id (N‘[dbo].[sp_FindInProcedure]’) and
OBJECTPROPERTY(id, N‘IsProcedure’) = 1) color=”#0000ff”
drop procedure [dbo].[sp_FindInProcedure]

Create Procedure sp_FindInProcedure

/************************************************************************************* *
* sp_FindInProcedure
* This stored procedure will look into all stored procedures defenitions to
* search the string which is given as the only parameter.
************************************************************************************* * By Asghar Panahy
* 12-jun-2006

SELECT so.name, sc.text
FROM syscomments sc
JOIN sysobjects so ON sc.id = so.id
WHERE so.xtype = ‘P’
And sc.Text like ’%’ + @sToSearch + ‘%’
Order By so.Name Asc


Column Description in SQL Server

The biggest challenge for me to learn de the business is to figure out how it hasbeen translated into a database. The more information I get out of the databasethe more familiar I feel myself with the business.
For applications living or quite some times and evolved by several developers, thisbecomes difficault if you can’t find out what those columns meant to keep. Speciallywhen they are keeping codes and flags which has not direct meaning for the businessbut rather they are stored as a meta data.
One of the handy implementation for such an information is the database itself.Lots of database administrators (and also developers) choose to keep the descriptionof a column somewhere reserved by the database. SQL-Server is not so convinientin this matter. Or maybe the system is while the user interface is not so open forit.
There is a so called undocumented stored procedure in the SQL-Server called sp_columns_rowset which shows you al you need and even more about atable. I am not going to talk about my experience with this undocumented storedprocedure which you can search the net and find some examples about it. Just mentioningit that it givs you something it calls DESCRIPTION of the field.
The question I have is where is this DESCRIPTION maintained? or even better, Howcan I fill this attribute? What should I do to get the descriptions of the columnsupdated with meaningfull sentence?

How to merge two Visual SourceSafe databases

This is a piece of cake when you know VSS has already provided tools for that. Irealised that some companies have different teams keeping their sources in differentVSS repositories. Maybe not all of them willing to merge these but certainly somedo. I was thinking of creating such a tool to bring all the history of the filesusing VSS programming API. I have investigated the VSS Admin and yes it doesit for free.

  • Open the VSS Admin screen (obviously you need to have administrative privilages)
  • From the menu’s select Archive->ArchiveProjects.. and create archive files foreach database.
  • Repeat this for all the vss databases you want to merg together
  • Create the destination VSS databased from the admin screen.
  • In that database create projects on the root. This is making it conviniet to importeach database into a seporate project. Although this step is not necessary and youcan do that during the next step too.
  • From the menu’s select Archive->RestoreProjects.. and select the archived file.Follow the wizard to select a location to restore the old database into the newlacation.

    Isn’t this just simple!

  • Importing source history from VSS to TFS

    I have managed to import a Visual SourceSafe database including the historyof it into the Microsoft Team Foundation System.
    It requires you to create a new project in the TFS as destination ream project,and by the way you can do this in Visual Studio using the menu File->New->TeamProject,doing the analyse and read the nice and handy report which gives you beautiful listof all warnings and errors. Then I had to map the users of one repository to theother one by editing the users.xml that comes out of the analyse step. You can mapdifferent folders from one to the other if you like to do so by editing the settings.xmlwhich is very convinient.
    During the exercise you will be asked to type in the VSS administrator password.

    Google Parser

    Download source – 25.2 Kb
    Google is definitely one of the most useful websites on the net. I am using it every day and like other things I use frequently, I intended to customize it to my personal changing needs. As a developer the first thing that came to my mind to automate my activities was that I needed a class representing this search engine. When I looked into their web site I saw that they provide a very nice API which is accessible through Web Services.

    For some strange reason, I couldn’t access the web service out of the company from behind the firewall… So I had to look into other options. Looking into CodeProject’s web site, I figured out that I am not the only one interested in this subject. There has been already some nice articles written about this matter. There is a nice code provided by Stuart Konen in C++ but I was looking for a .NET assembly. Also Kisilevich Slava is providing code with a much better interface and even a more powerful engine which gets its results from different search engines. But (s)he uses another HTML parser which makes it dependent on it and I wanted to have full control of the parsing of the page.
    So, I came up with this parser…
    Providing a Google interface has two challenges involved. First, you should write a code to establish a connection to send your HTTP request and get your response back. This is very easy when you work at home where the permissions are granted automatically. At the office there is a proxy which blocks the unattended internet access. So the issue here is to provide the DefaultCredentials as Internet Explorer would do. Fortunately Mr. Gates has provided an easy solution for this:HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Proxy = WebProxy.GetDefaultProxy();
    request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
    Once this is done, you can request anything IExplorer can. For more information on DefaultCredentials see the MSDN.
    The second challenge was to parse the response. This is the tricky part. Some do this by converting the HTML to XML and then use the XML parsers. Others use COM and use HtmlDocument to access the tags as DOM objects. Some others like me prefer parsing it using RegularExpressions. The last approach is surely the least readable code but surely fast and elegant.

    Describing Google’s Search Object
    Each result item has the following attributes:

    • link to the website
    • description of the found item
    • some fragment of text having one or more words from the query
    • a link to Cache
    • and a link to Similar pages
    from which the last two parts are not supported in my demo.
    This brings us to our first class Item which gets constructed as follows:
    public Item(string link, string description, string text)
    this._Link = link;
    this._Description = description;
    this._Text = text;
    The Searcher class provides the following properties:
    • Count
    • From
    • To
    • ItemsPerPage
    • Url and
    • Results
    Of properties, only the ItemsPerPage is read/write, the rest are read-only.
    It has three public methods which all help to initiate the search. The first one which should also be called first is the Search method which is to be given a query parameter.public void Search(string query)
    Once you call this method, the class will get the response and parse the page to get Count and Results. The you can call subsequent search methods exactly like you would do on the Google page.
    public void Search(long from)
    public void SearchNext()

    As I was working with this project, I realized that Google developers are changing the HTML layout more often than they change the logo on the front page. So I had to make sure my code was still working. But then they never promised anyone to keep the same HTML layout. Which makes this project very unstable. In other words nothing guarantees that it will work after a while. That is why I decided to have a project with full control over the whole code and not using any third party library. That way, I can modify the code easily to accommodate any changes in the Google HTML format. The whole parsing process has been split into three sections. First I have to get the counts and I m doing it in the GetCounts method. Then I get the division of results and parse the items out of it in a loop implemented in GetResults. For each item, I parse the HTML to get its properties and that happens within ParseItem.
    Using the Code
    In my demo I have a form to query Google and show the results in a list box. I have also provided a WebBrowser control to see how it looks within IExplorer. Also there is a link on the title to initiate IExplorer outside of the application. The code using this class is pretty simple.
    private Searcher google = new Searcher();

    google.ItemsPerPage = nudItemsPerPage.Value ;
    google.Search( txtSearch.Text);
    if (google.Results==null) return;
    btnNext.Enabled= true;
    catch (Exception ex)
    lstLinks.DataSource = google.Results ;
    lstLinks.DisplayMember = “Description”;

    Last Words
    There are plenty of ways to parse your HTML code. Depending on the problem you will need to choose one. Working with regular expressions is the most fascinating one and there is undoubtedly a lot to explore in that area.

    Static Events


    I just had a conversation with one of my colleagues and he mentioned the subject of using Static Events which was new to me and I want to investigate it in this article.

    Background (Why Static?)

    Basically the idea is to have something shared among all the loaded instances of a class and ensure that changing the static property will cause all instances to update their content right away without the changer having to iterate through the existing objects and figure out which ones need to be updated. Kind of building the intelligence into the class so that all instances know what to do when the static property has changed.

    This reminds me of the example of exchange rate, which is very well known to those implementing in banking systems: all transactions respect the current exchange rate. But I don’t recall using Static Events for that. We saw this property as a separate object and we made sure that there is only one instance of it at a time. And all instances of transactions knew where to find it when needed. There is a fine difference though. The transactions will not need to know about the changes happening on the exchange rate, rather they will use the last changed value at the time that they use it by requesting the current value. This is not enough when, for example, we want to implement an application where the user interface reacts immediately on changes in the UI characteristics like font, as if it has to happen at real-time. It would be very easy if we could have a static property in the Font class called currentFont and a static method to change that value and a static event to all instances to let them know when they need to update their appearance.

    Using the code

    It is clear that we need a static field that all instances respect. Let’s say we need a static field called Font that all the labels will use to refresh when this base field has been changed.

    public class MyLabel : System.Windows.Forms.Label
    // The static field all class instance will respect
    private static Font font = new Font("Verdana",11 );

    This field requires a static property setter to allow us to make changes to the base field.

    public static Font Font
    set {
    font = value;
    OnMyFontChange(new FontChangedEventArgs(font));

    As you can see this is where we set the static variable but we also call the notification method to start the delegate.

        private static  void OnMyFontChange(FontChangedEventArgs e)     {         if (MyFontChanged != null)             MyFontChanged(null, e);     }

    Now almost everything is set. All we need to do is to make sure that every instance subscribes to this event. And that is what we do in the constructor of the class.

    public  MyLabel()
    // Every instance subscribes to this event
    MyLabel.MyFontChanged += new FontChangedEventHandler(this.ChangeBaseFont);

    The delegated method is where we use the changed value to refresh the UI.

    private void ChangeBaseFont(object  sender, FontChangedEventArgs e)
    base.Font = e.Font ;


    Of course we could access the static field without the need to pass it over and introduce a new EventArgs class but it just happens to be implemented so, and it certainly has nothing to do with this subject.

    In the demo, I have provided a test application using this label control and it demonstrates how it will update multiple screens by changing the base Font property.