« April 2006 | Main | June 2006 »

Web browser Widget: Integrating Internet Explorer into C# Windows Application

Last night was a really interesting play around for me with C Sharp (C#). I decided a while back that it is a huge opportunity to be able to place widgets on users' desktops in Windows but quite simply I did not know how. Off the back of that idea I bought Visual Studio .NET and decided to teach myself how to program in C#. The book I used to teach myself is called Microsoft Visual C#.NET Step by Step and is written by John Sharp and Jon Jagger. The two Jonnies have done a fantastic job in this book of helping you step by step work your way into C# and I am very grateful.

NewprojectwindowsapplicationThere are a few simple steps for getting a browser integrated into a windows application using Visual Studio and Windows C# .NET. Initially you need to set yourself up a new project of the type "Windows Application" as shown in the linked thumbnail to the left.

CustomizetoolboxOnce you have created this new application you need to be able to insert the webbrowser into the C# form under the design option in the Visual Studio .net interface. The way you add the webbrowser is again really really simple but I could find almost nowhere online which shows you how. If you head to the Tools > Add/Remove Toolbox items > menu you will see the dialog box to the right. Within this box you have the opportunity to select the Microsoft Web Browser COM component. Check this box and then click "OK"

Toolbox Now within the toolbox you will see the Microsoft Web Browser icon shown to the right hand side, you can drag and drop this to the windows form and you will have integrated the web browser into your form. The image to the right shows the toolbox with the new Web Browser option.

Now you have your web browser in your form you are pretty much done but of course you will see nothing in this form unless you set a base referring URL within the code. The following code is directly from my initial test file of this little integration. The browser in my integration has been given the name "axWebBrowser1" and on the form loading I set it's location to my website www.paperairplanes.co.uk.

private void Form1_Load(object

sender, System.EventArgs e)

{

axWebBrowser1.Navigate(http://www.paperairplanes.co.uk/);

}

I have Zipped up the source code for anyone to use and included it at the following link but this is totally offered up as is and I am not a good programmer at all so I insist you check the code thoroughly before you use it. Download WindowsApplication2.zip

Good luck and I will be sure to post anything fun I do with this technology. I have a history of creating fun little hacks that are useful for me so I hope to post something soon.

Working Around SELECT INTO for mySQL

mySQL does not support the "SELECT * FROM ... INTO ..." sybase SQL extension. That seemed a real bummer for me since I use that a lot in my work and wanted to use it while I was trying to repair my cocktail recipe database which was full of rubbish because I don't check the input to the table.

After a bit of searching around I found that you could replicate this in two simple ways outlined below:

Using the CREATE ... SELECT Method

This is a really simple way to fix the problem, if you have experience with the existing "SELECT" method you can use that to create a new table with exactly the same properties as the table you are selecting from. An example statement below would copy exactly the table tbl_Ingredients from a cocktail database across to the placement tbl_Ingredients_temp.

CREATE tbl_Ingredients_temp SELECT * FROM tbl_Ingredients

In fact this method is really flexible since you can insert any column you wish into the new table. For example if you wanted to add a new column which allowed you to announce if an ingredient had been added after the new version you could (the values in that column will all be NULL for exising ingredients).

CREATE tbl_Ingredients_temp (A_INT_newversion INT) SELECT * FROM tbl_Ingredients

This made my life a lot easier and I hope it makes your life easier too.

Zeus mod rewrite Redirects

In my previous post I talked about how I was reading into what can be done with mod rewrite files. I was quite amazed at what it is possible to acheive with rewrite files. The output of the work I have done means that the URL http://www.cocktailmaking.co.uk/433-a-strawberry-with-milk.html is just redirecting to http://www.cocktailmaking.co.uk/showcocktail.php?CocktailID=433 on the server but any crawler or visitor can't tell that.

The methodology for doing this is really very simple with a .htaccess file but to truly do justice to the power of .htaccess will take a lot more than this short blog post. In effect however to mask the following URL http://www.anydomain.co.uk/someprog.php?variable=value by mapping http://www.anydomain.co.uk/variable/value.html to that URLwould require the following script.

#start .htaccess code
RewriteEngine On
RewriteBase /
# use regexp matching to identify which part is the value for this variable
RewriteCond %{QUERY_STRING} ^variable\=([^&]+)$
RewriteRule ^$ /variable/%1.html [R=301,L]
The above is an extremely efficient way of doing the mapping and should be search engine friendly. Unfortunately my host decided not to allow me to do this with .htaccess and doesn't allow the RewriteEngine to run. I therefore had to use the mod rewrite functionality in Zeus
There was very little documentation for this on the web as I searched but over a couple of hours it became clear that the methodology was really similar and the following script is what I required to get the job done on my website.
match URL into $ with ^/[0-9]*-(.*)\.html$
end if
Unfortunately I didn't nail it completely as you see. I had to add a bit of script in the php file to recognize the URL used to call the file and then pull out the variable number (a bit of a hack I know) but it works really well. I am now planning to change the URL structure linked from the site and do some 301 redirects so that I can get the yahoo value from having the better URL structure without losing the existing Google benefits.

301 Redirects

I have been working towards making my sites fully active served. This allows me the flexibility to do lots of interesting things I have been wanting to do for some time. For example in paper airplanes I would like to serve people adverts from different advertisers depending on the number of pages they have seen on my site. In cocktail recipes I want to make amazon recommends style recommendations when someone visits an individual cocktail (since when they visit an individual cocktail they tend to look at only one page then leave).

A major limiting factor was the fact that although I had replaced all the links on my sites to point at the new active pages the old html pages were still drawing a lot of traffic from people linking to my site and legacy search engine results. This is clearly not an unusual problem for a webmaster.

For some time now I have been wondering how to do 301 redirects since I understood they could overcome just this kind of problem in a way meta redirects cannot and I decided to dig a little deeper into this. It turns out that when you have a linux server you can place a ".htaccess" file in your directory which will permenantly redirect traffic arriving at an old URL to the new URL you want accessed. A screen dump of my .htaccess file is below.

  Htaccess_1

I worried of course that the new URLs would underperform in the search engines and I would lose all my traffic but today (less than a week after making this 301 redirect change) the entirely new URL www.paperairplanes.co.uk/planes.php is listed in Google Sitemaps as being the highest page rank page on my website when previously it was www.paperairplanes.co.uk/ and I have seen no reduction in search engine traffic to the site from any engine following the changes.

Good encouraging news for anyone who wants to change the URL structure of their site but not lose existing traffic in doing so.

My Photo
Blog powered by TypePad

my flickr tag cloud