Visual Studio 2005 Code Snippet Editor

by Damiaan Peeters 21. March 2008 11:44

Ever wondered how to change or add code snippets to Visual Studio 2005?  As you probably found out, you have to change or create some XML.  On techRepublic you can find an article on "Creating custom Visual Studio 2005 Code Snippets".

But did you know codeplex is hosting a "Editor for creating and modifying Visual Studio 2005 Code Snippets".  Snippy - Visual Studio Code Snippet Editor

Http POST to webserver using C#

by Damiaan Peeters 6. March 2008 11:38

I 've written some time ago a HTML scraper to search the VIES VAT number Validation web site of the European Union.  The service offered no web service at that time, so I needed to use the HTTP POST method.

The code is not too difficult.

[more]

    private readonly string Url = "http://ec.europa.eu/taxation_customs/vies/cgi-bin/viesquer";
private readonly string Referer = "http://ec.europa.eu/taxation_customs/vies/en/vieshome.htm";
List<Exception> Exceptions = null;
     ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "Lang=EN";
postData += ("&VAT=" + VatNumber);
postData += ("&MS=" + CountryCode);
postData += ("&ISO=" + CountryCode);
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(this.Url);
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.Referer = Referer;
myRequest.ContentLength = data.Length;
myRequest.ProtocolVersion = HttpVersion.Version10;
myRequest.Method = "POST";
Stream newStream = myRequest.GetRequestStream();
// Send the data.
     newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse resp = null;
try
     {
resp = (HttpWebResponse)myRequest.GetResponse();
}
catch (Exception ex)
{
Exceptions.Add(ex);
}
     StreamWriter myWriter = null;
try
     {
myWriter = new StreamWriter(resp.GetResponseStream());
myWriter.Write(data);
}
catch (Exception e)
{
Exceptions.Add(e);
return VatCheckOk.NotChecked;
}
finally
     {
myWriter.Close();
}
HttpWebResponse objResponse = (HttpWebResponse)myRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
string result = sr.ReadToEnd();
// Close and clean up the StreamReader
         sr.Close();
}
If i remember well, i used the result string to check for the string: "Yes, Valid number".

How to extend the TabControl in Windows Forms

by Damiaan Peeters 19. February 2008 15:35

Short post, much code.

In this post I want to show how the TabControl can be extended.  How you can Hide and show a TabPage.

You can use the control like this:

 

if (currentProduct.Product_Compound)
{
tabProduct.ShowTabPage(tabProductCompound);
}
else
{
tabProduct.HideTabPage(tabProductCompound);
}

 

 

[more]

    /// <summary>
    /// A new tabControl
    /// </summary>
    public class TabControl : System.Windows.Forms.TabControl
    {
        /// <summary>
        /// Hides the tab page.
        /// </summary>
        /// <param name="tp">The TabPage</param>
        public void HideTabPage(TabPage tp)
        {
            if (this.TabPages.Contains(tp))
                this.TabPages.Remove(tp);
        }
        /// <summary>
        /// Shows the tab page.
        /// </summary>
        /// <param name="tp">The TabPage</param>
        public void ShowTabPage(TabPage tp)
        {
            ShowTabPage(tp, this.TabPages.Count);
        }
        /// <summary>
        /// Shows the tab page.
        /// </summary>
        /// <param name="tp">The TabPage</param>
        /// <param name="index">The index.</param>
        public void ShowTabPage(TabPage tp, int index)
        {
            if (this.TabPages.Contains(tp)) return;
            InsertTabPage(tp, index);
        }
        /// <summary>
        /// Inserts the tab page.
        /// </summary>
        /// <param name="tabpage">The tabpage.</param>
        /// <param name="index">The index.</param>
        public void InsertTabPage(TabPage tabpage, int index)
        {
            if (index < 0 || index > this.TabCount)
                throw new ArgumentException("Index out of Range.");
            this.TabPages.Add(tabpage);
            if (index < this.TabCount - 1)
                do
                {
                    SwapTabPages(tabpage, (this.TabPages[this.TabPages.IndexOf(tabpage) - 1]));
                }
                while (this.TabPages.IndexOf(tabpage) != index);
            this.SelectedTab = tabpage;
        }
        /// <summary>
        /// Swaps the tab pages.
        /// </summary>
        /// <param name="tp1">The TabPage1.</param>
        /// <param name="tp2">The TabPage2.</param>
        public void SwapTabPages(TabPage tp1, TabPage tp2)
        {
            if (this.TabPages.Contains(tp1) == false || this.TabPages.Contains(tp2) == false)
                throw new ArgumentException("TabPages must be in the TabControls TabPageCollection.");

            int Index1 = this.TabPages.IndexOf(tp1);
            int Index2 = this.TabPages.IndexOf(tp2);
            this.TabPages[Index1] = tp2;
            this.TabPages[Index2] = tp1;

            //Uncomment the following section to overcome bugs in the Compact Framework
            //this.SelectedIndex = this.SelectedIndex;
            //string tp1Text, tp2Text;
            //tp1Text = tp1.Text;
            //tp2Text = tp2.Text;
            //tp1.Text=tp2Text;
            //tp2.Text=tp1Text;

        }
    }

Bart De Smet gone mad about Extension methods

by Damiaan Peeters 7. January 2008 16:56

Our beloved Bart De Smet has gone mad.  Complete insane.  He started to add some Extension Methods to do Exception handling. 

He writes the following on his blog:

What a joke you must think when reading this post's title. Isn't the functional paradigm all about side-effect free programming and the such? Well, it turns out you're absolutely right. So, why this post? I have to admit I had yet another crazy Sunday afternoon idea that I found worthwhile to open VS 2008 for and give it a short.  Exception Handling in functional style - B# .NET Blog

You read it well.  "Another crazy Sunday".  It appears that working at Microsoft has some side effects.

Apart from the crazy Sundays Bart, created to a few 5 star Webcasts about C# 3.0.

Launch Visual Studio 2008 Belgium

by Damiaan Peeters 6. January 2008 15:24

Heroes Happen Here it is called. Go to http://www.heroeshappenhere.be

Heroes Happen Here is the main theme of an impressive launch.
With it, Microsoft honours the talent of all of our users who transform our products into powerful products for solving real work IT problems.

On Launch day March 11, we'll officially present new versions of Windows Server 2008, Microsoft SQL Server 2008 and Microsoft Visual Studio 2008.

On March 12 and March 13 we stage the TechDays. During workshops IT professionals and developers alike get a chance to go into every single asset of each pack.

The program for the technical sessions on March 12 and 13 are still not available, but i guess this will be soon the case.

Let's meet in Gent.

Connectionstring in another Project (DLL)

by Damiaan Peeters 1. January 2008 16:49

I always put my datalogic into a different project.  Like this i can reuse my DL in other projects with the same purpose or customer.  [more]

To change or get a connectionstring is normally impossible because the Settings class is declared as internal.  This means that the complete project is able to access this property, but other projects (dll's) are not allowed to access this property.

If you try to access this, you will see get an error like this:

'DamPee.MyProject.DL.Properties.Settings' is inaccessible due to its protection level   

If you open the Settings, you will see the Access Modifier.  Change this to Public.

image

 

Now you will be able to access the Settings (including the connectionstrings) in your other projects.

connection.ConnectionString = global::DamPee.MyProject.DL.Properties.Settings.Default.MyProjectConnectionString;
connection.Open();

Generic BindingList in the .NET Framework 2.0

by Damiaan Peeters 7. December 2007 17:10

Learn about the generic BindingList and how to extend this generic collection type to add sorting and searching functionality.

Behind the Scenes: Improvements to Windows Forms Data Binding in the .NET Framework 2.0, Part 2

Windows Applications (How Do I in C#)

by Damiaan Peeters 6. December 2007 17:04

Today I found the  How Do I in C# series on MSDN.  The How Do I in series are a great way to get started with task-based topics for any C# programmer and application developer.

At this moment the Windows Applications (How Do I in C#)  page. This page links to help on widely used Windows applications tasks.

Windows Applications (How Do I in C#)

My Font BindingList

by Damiaan Peeters 2. December 2007 14:13

When you search a lot on MSDN, you might encounter from time to time a nice example.  Yesterday I found such a nice example on the BindingSource Class page.

They created a new new Generic BindingList of the Font Class

public class MyFontList : BindingList<Font>
{
    protected override bool SupportsSearchingCore
    {
        get { return true; }
    }
    protected override int FindCore(PropertyDescriptor prop, object key)
    {
        // Ignore the prop value and search by family name.         for (int i = 0; i < Count; ++i)
        {
            if (Items[i].FontFamily.Name.ToLower() == ((string)key).ToLower())
                return i;
        }
        return -1;
    }
}        

To use this class, just fill the MyFontList and attach it to a bindingSource.

MyFontList fonts = new MyFontList();
for (int i = 0; i < FontFamily.Families.Length; i++)
{
    if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Regular))
        fonts.Add(new Font(FontFamily.Families[i], 11.0F, FontStyle.Regular));
}
binding1.DataSource = fonts;
listBox1.DataSource = binding1;
listBox1.DisplayMember = "Name";

Who.I.am

Certified Umbraco, .net and Azure developer, seo lover. Magician in my spare time.

Month List