Archive for the ‘Development’ Category.

Detecting when you press "Enter" in a WPF TextBox

Hey all,

You have a TextBox and you want to detect when someone presses “Enter” and do something different than if they type text.

This one is simple, but you need to be careful when closing a window in multiple places.

The Event you want to use is KeyDown.

Imagine you have a TextBox called TextBoxCompanyName where you are asking the company name.  It is common to enter the name and press Enter and have “Enter” act just like the OK button.

You already have an OK button and it is setup to close the window or do other stuff first.


        private void ButtonOK_Click(object sender, RoutedEventArgs e)
        {
            // May do other stuff.
            this.Close();
        }

        private void TextBoxCompanyName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ButtonOK_Click(this, new RoutedEventArgs());
            }
        }

Notice I don’t just call this.Close() again in the new TextBoxCompanyName_KeyDown event; but instead I call the ButtonOK_Click function to simulate the OK button being clicked. This way:

  • I don’t have duplicate code
  • I prevent a potential bug where if I had simply called this.Close() again, then someone in the future might add code to ButtonOK_Click and they might not know that TextBoxCompanyName_KeyDown will close the window a different way, leaving you in a weird state where the button does something that pressing “Enter” doesn’t do.

You now know how to do something different when you press “Enter”. I hope that was easy for you.


Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.

C# – Email Regular Expression

I wrote a regex for email that is gets the best results of any I have found online. Along with getting better results, it is shorter too.

Download the C# project with unit tests here: EmailRegEx on GitHub
The pattern of an email is described as follows:

  1. It will always have a single @ sign
  2. 1 to 64 characters before the @ sign called the local-part. Can contain characters a–z, A–Z, 0-9, ! # $ % & ‘ * + – / = ? ^ _ ` { | } ~, and . if it is not at the first or end of the local-part.
  3. Some characters after the @ sign that have a pattern as follows called the domain.
    1. It will always have a period “.”.
    2. One or more character before the period.
    3. Two to four characters after the period.

So a simple patterns of an email address should be something like these:

  1. This one just makes sure there are characters before and after the @
    .+@.+
  2. This one makes sure the are characters before and after the @ as well as a character before and after the . in the domain.
    .+@.*+\..+
  3. This one makes sure that there is only one @ symbol.
    [^@]+@[^@]+\.

These are all quick an easy examples and will not work in every instance but are usually accurate enough for casual programs.

But a comprehensive example is much more complex.

  1. I wrote one myself that is the shortest and gets the best results of any I have found:
    ^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))\z
    
  2. Here is another complex one I found: [reference]
    ^(([^<>()[\]\\.,;:\s@\""]+(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$
    

So let me explain the first one that I wrote as it passes my unit tests below:

The start
[\w!#$%&’*+\-/=?\^_`{|}~]+ At least one valid local-part character not including a period.
(\.[\w!#$%&’*+\-/=?\^_`{|}~]+)* Any number (including zero) of a group that starts with a single period and has at least one valid local-part character after the period.
@ The @ character
( Start group 1
( Start group 2
([\-\w]+\.)+ At least one group of at least one valid word character or hyphen followed by a period. The attached project has a more complex hostname regex option too.
[\w]{2,4} Any two to four valid top level domain characters.
) End group 2
| an OR statement
( Start group 3
([0-9]{1,3}\.){3}[0-9]{1,3} A regular expression for an IP Address. The attached project has a more complex IP regex example too.
) End group 3
) End group 1
\z No end of line: \r or \n.

Code for the Email Regular Expression

Here is code for both examples. My email regular expression is enabled and the one I found on line is commented out. To see how they work differently, just comment out mine, and uncomment the one I found online.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace RegularExpressionsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            String theEmailPattern = @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*"
                                   + "@"
                                   + @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))\z";

            // The string pattern from here doesn't not work in all instances.
            // http://www.cambiaresearch.com/c4/bf974b23-484b-41c3-b331-0bd8121d5177/Parsing-Email-Addresses-with-Regular-Expressions.aspx
            //String theEmailPattern = @"^(([^<>()[\]\\.,;:\s@\""]+(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))"
            //                       + "@"
            //                       + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])"
            //                       + "|"
            //                       + @"(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$";

            Console.WriteLine("Bad emails");
            foreach (String email in GetBadEmails())
            {
                Log(Regex.IsMatch(email, theEmailPattern));
            }

            Console.WriteLine("Good emails");
            foreach (String email in GetGoodEmails())
            {
                Log(Regex.IsMatch(email, theEmailPattern));
            }
        }

        private static void Log(bool inValue)
        {
            if (inValue)
            {
                Console.WriteLine("It matches the pattern");
            }
            else
            {
                Console.WriteLine("It doesn't match the pattern");
            }
        }

        private static List<String> GetBadEmails()
        {
            List<String> emails = new List<String>();
            emails.Add("joe"); // should fail
            emails.Add("joe@home"); // should fail
            emails.Add("a@b.c"); // should fail because .c is only one character but must be 2-4 characters
            emails.Add("joe-bob[at]home.com"); // should fail because [at] is not valid
            emails.Add("joe@his.home.place"); // should fail because place is 5 characters but must be 2-4 characters
            emails.Add("joe.@bob.com"); // should fail because there is a dot at the end of the local-part
            emails.Add(".joe@bob.com"); // should fail because there is a dot at the beginning of the local-part
            emails.Add("john..doe@bob.com"); // should fail because there are two dots in the local-part
            emails.Add("john.doe@bob..com"); // should fail because there are two dots in the domain
            emails.Add("joe<>bob@bob.com"); // should fail because <> are not valid
            emails.Add("joe@his.home.com."); // should fail because it can't end with a period
            emails.Add("john.doe@bob-.com"); // should fail because there is a dash at the start of a domain part
            emails.Add("john.doe@-bob.com"); // should fail because there is a dash at the end of a domain part
            emails.Add("a@10.1.100.1a");  // Should fail because of the extra character
            emails.Add("joe<>bob@bob.com\n"); // should fail because it end with \n
            emails.Add("joe<>bob@bob.com\r"); // should fail because it ends with \r
            return emails;
        }

        private static List<String> GetGoodEmails()
        {
            List<String> emails = new List<String>();
            emails.Add("joe@home.org");
            emails.Add("joe@joebob.name");
            emails.Add("joe&bob@bob.com");
            emails.Add("~joe@bob.com");
            emails.Add("joe$@bob.com");
            emails.Add("joe+bob@bob.com");
            emails.Add("o'reilly@there.com");
            emails.Add("joe@home.com");
            emails.Add("joe.bob@home.com");
            emails.Add("joe@his.home.com");
            emails.Add("a@abc.org");
            emails.Add("a@abc-xyz.org");
            emails.Add("a@192.168.0.1");
            emails.Add("a@10.1.100.1");
            return emails;
        }
    }
}

Well, now you have the best C# Email Regular Expression out there.

Update: My attached project has an even better and more accurate one now too.

(Reference: wikipedia)

How to create a directory on an FTP server using C#?

Ok, so I already can upload a file to an FTP server: How to upload a file to an FTP server using C#?

However, now I need to create a directory first.

It follows some basic steps:

  1. Open a request using the full destination ftp path: Ftp://Ftp.Server.tld/ or Ftp://Ftp.Server.tld/Some/Path
  2. Configure the connection request
  3. Call GetResponse() method to actually attempt to create the directory
  4. Verify that it worked.

See the steps inside the source as comments:

using System;
using System.IO;
using System.Net;

namespace CreateDirectoryOnFtpServer
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateDirectoryOnFTP("ftp://ftp.server.tld", /*user*/"User1", /*pw*/"Passwd!", "NewDirectory");

        }

        static void CreateDirectoryOnFTP(String inFTPServerAndPath, String inUsername, String inPassword, String inNewDirectory)
        {
            // Step 1 - Open a request using the full URI, ftp://ftp.server.tld/path/file.ext
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(inFTPServerAndPath + "/" + inNewDirectory);

            // Step 2 - Configure the connection request
            request.Credentials = new NetworkCredential(inUsername, inPassword);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            request.Method = WebRequestMethods.Ftp.MakeDirectory;

            // Step 3 - Call GetResponse() method to actually attempt to create the directory
            FtpWebResponse makeDirectoryResponse = (FtpWebResponse)request.GetResponse();
        }
    }
}

All right, now you have created a directory on the FTP server.


Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.

Detecting if the install is occuring on a 64 bit or 32 bit machine

Ok, so NSIS has most the 64 bit functionality available for their installers.

I need to write a registry key to the 64 bit registry on 64 bit machines.

I may need to install to Program Files or Program Files (x86).

If on 64 bit workstation you need to install to Program Files (x86) that is done by default because the $PROGRAMFILES constant goes to the Program Files (x86) directory.

So a simple solution that should be obvious is to check for something that is on a 64 bit machine that is not on a 32 bit machine.  Lets make a list of such differences.

Files

  1. c:\Program Files (x86) – Since this is just a folder, it could exist on a 32 bit machine, though that would be unlikely.
  2. c:\windows\SysWow64

Registry key

  1. HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node – This is a 64 bit registry and does not exist in 32 bit operating systems.

So some of these are susceptible to be “faked”.  I could see some idiot writing an installer on Windows 7 and hard coding the Program Files (x86) directory so it is installed there even on 32 bit systems.  The SysWow64 directory seems more safe and it exists on Vista, Windows 7, and 2008.  I don’t have XP or 2003 64 bit machines so someone can check for me. It seems that the registry key is also likely to be a safe check.

You can check any of these you want. 

Here is how you would change which registry to work with.

	IfFileExists $WINDIR\SYSWOW64\*.* Is64bit Is32bit
	Is32bit:
		SetRegView 32
		GOTO End32Bitvs64BitCheck

	Is64bit:
		SetRegView 64

	End32Bitvs64BitCheck:

That fixes the registry keys. So if you only care about the registry you are fine. However, you software will still install to the Program Files (x86) directory. To fix both, you could place the following code in BOTH the .onInit and the un.onInit functions.

	; Install to the correct directory on 32 bit or 64 bit machines
	IfFileExists $WINDIR\SYSWOW64\*.* Is64bit Is32bit
	Is32bit:
		MessageBox MB_OK "32 bit"
		SetRegView 32
		StrCpy $INSTDIR "$PROGRAMFILES32\LANDesk\Health Check"
		GOTO End32Bitvs64BitCheck

	Is64bit:
		MessageBox MB_OK "64 bit"
		SetRegView 64
		StrCpy $INSTDIR "$PROGRAMFILES64\LANDesk\Health Check"

	End32Bitvs64BitCheck:

Don’t forget to comment out the usual place that INSTDIR is configured.

Anyway, that seems to work.


Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.

Notepad++ and the XML Tools plugin

Ok, so I do a lot with Xml files.  Between working with my company’s product LDMS and all the Xmls it uses, to my own code using Serialization, I seem to always have an Xml file open.

I hate it when I open and Xml File and it is all on one line.  Or every element is a separate line but not a single line is indented.

Visual Studio can fix these for me rather easily.  But I have a lot of different machines in my lab and often work in other’s labs, or on customer’s computers while remote controlling them for support reasons. I can’t just quickly download an install Visual Studio. While yes, there is a free Express Edition version, it is just an overkill for the simple job of making an Xml file look pretty in text.  I need a small simple, quick to download and install tool.

The solution was right in front of me the whole time: Notepad++ and the XML Tools plugin

I always use Notepad++ as my random text editor on windows.  I already use its Compare plugin constantly, so I am very familiar with it.  It finally donned on me to check if it had an plugin that could help me with Xmls and sure enough, it did.

The XML Tools plugin, has almost all the features I wanted.

Notepad++ is just over 3 megabytes and download rather quickly.  The XML Tools plugin is not installed by default but can quickly be installed using the Plugin Manager.  Don’t worry about the additional download, the XML Tools plugin is only just over 300 kilobytes.

So I can now prettify my Xml files with a small, quick to download and install, windows tool.

Here is the menu this plugin provides, so you can see a list of some of the features.

So the Pretty pring (XML only – with line breaks) will take an Xml that is all on one single line, and expand it to have line breaks and also give it the proper indents.

There is also a tool to make sure the Syntax is correct.

The only feature I want that it doesn’t have is a tool to sort child elements and attributes.


Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.

Options for serializing an Array or List when using Serializable?

Lets say you have to implement Xml Serialization on your objects. Many of your exiting Xml files have Lists. However, what if you are working with existing Xml files and you don’t get to reformat them? So you have to make the List in your object react the way it already looks in possible poorly designed Xml Files.

Ok, so off the top of my head, I can think of many ways that a List of objects could be displayed in XML (and there are probably more). For simplicity’s sake, I am going to use String objects for this, but this goes along with any object.

So how does Xml Serialization hold up when confronted with existing Xml files and existing lists?

Well, lets first show you some example Lists in Xml and then lets see what types are supported.

Type 1 – Grouped Lists

Status: Supported – This is the default way Xml Serialization stores lists in Xml files.
Xml Design: Excellent.

The object elements are contained in a parent element. As shown, each String element is inside the Strings (notice it is plural) element.

<RootElement>
  <Strings>
    <String>Some string 0.</String>
    <String>Some string 1.</String>
    <String>Some string 2.</String>
    <String>Some string 3.</String>
    <String>Some string 4.</String>
  </Strings>
</RootElement>

Type 2 – Grouped Lists numbered

Status: Unknown – I haven’t found a way to do this yet, but I am not ready to say it can’t be done.  If it can’t be done, I think it “should be” supported.
Xml Design: Average. I am not sure if you can serialize this. Someone designing an Xml without serialization in mind would not think this design is wrong.

Same as above only each element is numbered incrementally from 0. (Or it could start at 1, right?)

<RootElement>
  <Strings>
    <String0>Some string 0.</String0>
    <String1>Some string 1.</String1>
    <String2>Some string 2.</String2>
    <String3>Some string 3.</String3>
    <String4>Some string 4.</String4>
  </Strings>
</RootElement>

Type 3 – Flat List

Status: Supported – If you add [XmlElement] above the property, then this is the format you get.
Xml Design: Excellent. This is a standard supported format.

There is just a list, with no parent attribute containing the list items.

<RootElement>
    <String>Some string 0.</String>
    <String>Some string 1.</String>
    <String>Some string 2.</String>
    <String>Some string 3.</String>
    <String>Some string 4.</String>
</RootElement>

Type 4 – Flat List numbered

Status: Unknown – I haven’t found a way to do this yet, but I am not ready to say it can’t be done. If it can’t be done, I think it “should be” supported.
Xml Design: Average. I am not sure if you can serialize this. Someone designing an Xml without serialization in mind would not think this design is wrong.

Same as the Flat list above only each element is numbered incrementally from 0. (Or it could start at 1, right?)

<RootElement>
    <String0>Some string 0.</String0>
    <String1>Some string 1.</String1>
    <String2>Some string 2.</String2>
    <String3>Some string 3.</String3>
    <String4>Some string 4.</String4>
</RootElement>

Type 5 – Attribute Lists

Status: Broken but working – If you put [XmlAttribute] before and Array or List, beware. It seems it uses space as the delimiter. I can’t seem to find a way to change the delimiter.
Xml Design: Poor. You can serialize this, but it deserialize the same way as it serializes.

A single attribute that contains a list. Obviously the object can’t be complex to match this type.  String works, but a complex object has to be an element not an attribute.

I can already see that this method would be tough to use. Right away I am wondering seeing the problem of using white space as a delimiter. However, quotes are important too. Xml Serialization automatically replaces quotes with the following string:

"

I would have thought since it uses a space as a delimiter that it would have replaced white space with some similar character entity string, but it did not.

<RootElement Strings="Some string 0. Some string 1. Some string 2. Some string 3. Some string 4.">
</RootElement>

Type 6 – Attribute Lists numbered

Status: Unknown – This is what I expected when I used the [XmlAttribute] tag but instead I got Type 5.  I have seen this in Xml files, so supporting it would be nice.
Xml Design: Average.  If a List exists that is always expected to have only one to five elements, I see nothing wrong with this design.

A separate, numbered attribute for each element in the list.

<RootElement String0=Some string 0." String1="Some string 1." String2="Some string 2." String3="Some string 3." String4="Some string 4."
</RootElement>

Type 7 – Delimited text in an element

Status: No supported – This just is not how it is designed to work, nor should it every work this way.
Xml Design: Poor – This defeats the whole purpose of an Xml.

This assumes that the list is inside a single element and has some kind of delimiter. Below the delimiter is a new line character. (Let’s assume that we expect white space at the front and end of the string to be trimmed and blank lines to be ignored.)

<RootElement>
  <Strings>
    Some string 0.
    Some string 1.
    Some string 2.
    Some string 3.
    Some string 4.
  <Strings>
</RootElement>

Conclusion

Type 1 and Type 2 are the ideal methods and work perfectly for me.

Types 2, 4, 6 should be supported if they aren’t already.  Maybe there is a way to do these that I don’t know about.

Type 5 seems to work but doesn’t and it is really disappointing that the [XmlAttribute] tag works this way instead of like Type 6.

Type 7 is bad and you pretty much are left to fixing this with manual code and not using Xml Serialization.


Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.

How to create a custom class template for Xml Serializable classes?

Ok, so you don’t always want a default class template for every type of class.  I have to create a bunch of classes that implement Serializable and if the class template assumed this, that would be great.  However, I don’t want my default class template to assume this.

So here is what I did broken down into four simple steps.

  1. Open or create a c# project.
  2. Create a class file.
  3. Add the text and the variables to replaced.
  4. Export the item as a template.

Step 1 – Open or create a c# project.

Ok, so any project will do.  I used an existing project, but you can create a new one if you want.  Any C# project should allow this to happen.

Step 2 – Create a class file.

In one of my C# projects in Visual Studio, I created a new class called XmlClass.cs.

Step 3 – Add the text and the variables to replaced

I put the following text into my new class:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace $rootnamespace$
{
	[Serializable]
	public class $safeitemrootname$
	{
		#region Member Variables
		#endregion

		#region Constructors

		/*
		 * The default constructor
 		 */
		public $safeitemrootname$()
		{
		}

		#endregion

		#region Properties
		#endregion

		#region Functions
		#endregion

		#region Enums
		#endregion
	}
}

Step 4 – Export the item as a template

  1. In Visual Studio, chose File | Export Template.  This starts a wizard that is extremely easy to follow.Note: If you have unsaved files in your project, you will be prompted to save them.
  2. Chose Item template, select your project, and click Next.
  3. In the next screen there was a tree view of check boxes for all my objects.  I checked the box next to my XmlClass.cs.
  4. In the next screen, provide references.Note: I added only System and System.Xml.
  5. In the next screen, provide a Template name and a Template description.
  6. Click finish.

You should now have the option under My Templates when you add a new item to your project.

This class will be  useful and will save you and your team some typing when you are in the class creation phase of your project and you are creating all your Serializable classes.


Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.

Old Games…very old…1991…on 5.25" Floppies

Hey all,

Guess what gifts my brother brought me yesterday?  Yes, he cleaned out some old DOS games from his closet.  Some of the games I thoroughly loved:

  • Advanced Dungeons & Dragons’ Eye of the Beholder II – The Legend of Darkmoon
  • Nephi’s Quest – A mormon version of Kings Quest.

Unfortunately my Kings Quest set wasn’t found, though my brother has this if I want it, and Bard’s Tale II wasn’t found.

So I instantly thought of two problems I had to solve:

  1. How to read the 5.25″ floppies
  2. How to get a DOS system that isn’t so fast the games are unplayable.

Reading the 5.25 floppy drives

Yes, they are on 5.25″ floppies.  So after about 20 minutes of looking online for a USB 5.25″ floppy drive, I gave up.

So my options are:

  1. Get an old system with a floppy drive that still works.
  2. Send my floppies to a company that will migrate them to a CD at $5 a floppy.
  3. Find someone who has already moved the software from the floppies…

Yeah, one and two just weren’t for me, so the alternative was my only option.

Alternative to Reading the 5.25 floppy drives

Ok, so I almost gave up, when my decided that some one out that had a copy of the game downloadable right.  I mean, these games are 20 years old.  They are collectable. So somebody has to have them online.  Since I purchased a license in 1991, I thought I would not be breaking any laws downloading them.

Turns out I didn’t really have to worry about legalities and copyrights.

I found this site: http://www.abandonia.com/

Yes, Abandonia is the land of “abandonware”.  According to them, many of these games no longer have a copyright and are free to download, play, distribute, etc…

So I downloaded my Advanced Dungeons & Dragons’ Eye of the Beholder II – The Legend of Darkmoon game immediately.

How to get a DOS system that isn’t so fast the games are unplayable

Well, my first thought was VMWare, but again, Abandonia helped me out here.  There is a DOS Emulator called DOS Box.

I installed DOS Box on my Windows 7 box at home, with worry that it wouldn’t work well on Windows 7 64 bit, but to  my surprise, it worked well.

I do want to find a way to make the screen bigger.  And it would be cool to have a DOS VM along with DOS Box to test if it is better or worse, but hey this is sufficient for now.

Other Abandoned Games

There were many other abandoned games, such as the first Eye of the Beholder game as well as Eye of the Beholder III.  Also, Bards Tale I, II, III, and hundreds more.

So if you are looking for a blast from the past with one of your favorite games, check it out.

Changing the prop snippet for creating a Property in C#

Ok, so it is very common for the c# member variables to start with either an _ (underscore) or an m.  So when creating a property, you can save a lot of time by changing it to assume this as well.

For example, your class may look as follows:

namespace AgentConfigurationPlugin
{
    public class Class1
    {
        #region Member Variables
        String _MemberString;
        int _MemberInt;
        #endregion

        #region Constructors

        /*
		 * The default constructor
 		 */
        public Class1()
        {
        }

        #endregion

        #region Properties
        public String MemberString
        {
            get { return _MemberString; }
            set { _MemberString = value; }
        }

        public int Memberint
        {
            get { return _MemberInt; }
            set { _MemberInt = value; }
        }
        #endregion
    }
}

Note: I use the _ character even though it is hard to type (being up to the right of my pinky finger), so if you prefer, use the letter “m”, which is easy to type (being just below my pointer finger) and it also stands for “member variable”.

        #region Member Variables
        String mMemberString;
        int mMemberInt;
        #endregion

Anyway, whether it is an “m” or “_” or any other character, it is common to prefix member variables. So it would be useful if the property snippet assumed that prefix character as well.

The default snippet for creating a Property is located here:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#\prop.snippet

The contents looks as follows.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>prop</Title>
			<Shortcut>prop</Shortcut>
			<Description>Code snippet for an automatically implemented property</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>type</ID>
					<ToolTip>Property type</ToolTip>
					<Default>int</Default>
				</Literal>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[public $type$ $property$ { get; set; }$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

Change it to be like this:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>prop</Title>
			<Shortcut>prop</Shortcut>
			<Description>Code snippet for an automatically implemented property</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>type</ID>
					<ToolTip>Property type</ToolTip>
					<Default>int</Default>
				</Literal>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[public $type$ $property$
		{
    			get { return _$property$; }
    			set { _$property$ = value; }
		}
$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

The key section that fixes this is:

			<Code Language="csharp"><![CDATA[public $type$ $property$
		{
    			get { return _$property$; }
    			set { _$property$ = value; }
		}
$end$]]>

Or if you use “m” instead of “_” as I do, of course you would replace the “_” with an “m”.

			<Code Language="csharp"><![CDATA[public $type$ $property$
		{
    			get { return m$property$; }
    			set { m$property$ = value; }
		}
$end$]]>

Now when you create a member variable and then a property that matches it exactly except for the prefix character, the works is done for you, making you a more efficient programmer.

You may want to change the propg snippet as well.


Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.

How to query a SQL database in C#?

How to query a SQL database in C#? or How to execute a database query against a database in C#?

Having used other languages where this is much simpler, I was surprised at how “not simple” this was in C#. I expected it to be a little more complex than in some scripting language such as PHP, but it was way more complex.

It is nice to run the Query and store the results in a DataTable, so that is what my example shows.

There are a few simple steps to remember.

  1. Create a String to hold the database connection string.
    (Note: If you don’t know the proper format for a connection string use SqlConnectionBuilder.)
  2. Create a SQL connection object.
  3. Open the SQL connection.
  4. Create a String to hold the query.
  5. Create a SqlCommand object and pass the constructor the connection string and the query string.
  6. Use the above SqlCommand object to create a SqlDataReader object.
  7. Create a DataTable object to hold all the data returned by the query.
  8. Use the DataTable.Load(SqlDataReader) function to put the results of the query into a DataTable.
  9. Do something with the data in your DataTable here. For example, it is common to use a foreach loop to do something with each row.
  10. Close the SQL connection.

Here is how I do it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CountRows
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a String to hold the database connection string.
            // NOTE: Put in a real database connection string here or runtime won't work
            string sdwConnectionString = @"Data Source = ServerName; user id=UserName; password=P@sswd!; Initial Catalog = DatabaseName;";

            // Create a connection
            SqlConnection sdwDBConnection = new SqlConnection(sdwConnectionString);

            // Open the connection
            sdwDBConnection.Open();

            // Create a String to hold the query.
            string query = "SELECT * FROM MyTable";

            // Create a SqlCommand object and pass the constructor the connection string and the query string.
            SqlCommand queryCommand = new SqlCommand(query, sdwDBConnection);

            // Use the above SqlCommand object to create a SqlDataReader object.
            SqlDataReader queryCommandReader = queryCommand.ExecuteReader();

            // Create a DataTable object to hold all the data returned by the query.
            DataTable dataTable = new DataTable();

            // Use the DataTable.Load(SqlDataReader) function to put the results of the query into a DataTable.
            dataTable.Load(queryCommandReader);

            // Example 1 - Print your  Column Headers
            String columns = string.Empty;
            foreach (DataColumn column in dataTable.Columns)
            {
                columns += column.ColumnName + " | ";
            }
            Console.WriteLine(columns);

            // Example 2 - Print the first 10 row of data
            int topRows = 10;
            for (int i = 0; i < topRows; i++)
            {
                String rowText = string.Empty;
                foreach (DataColumn column in dataTable.Columns)
                {
                    rowText += dataTable.Rows[i][column.ColumnName] + " | ";
                }
                Console.WriteLine(rowText);
            }

            // Close the connection
            sdwDBConnection.Close();
        }
    }
}

So now the results are stored in a DataTable.

You can now access each row of data using the DataTable.Rows collection.

 

Return to ADO.NET and Database with C#

How to get the parent process that launched a C# application?

Hey all,

I have a process that is a C# process.  I need to do something different if someone just double-clicks the application than if it is launched by a certain other process.  So I decided to check the parent process.

I couldn’t find a simple C# only method. But I did find a code snippet that works. There were actually lots of posts on that provided the following code snippet or variations thereof, so I consider it to be public domain.  So obviously I didn’t write this part.

        private static Process GetParentProcess()
        {
            int iParentPid = 0;
            int iCurrentPid = Process.GetCurrentProcess().Id;

            IntPtr oHnd = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

            if (oHnd == IntPtr.Zero)
                return null;

            PROCESSENTRY32 oProcInfo = new PROCESSENTRY32();

            oProcInfo.dwSize =
            (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(PROCESSENTRY32));

            if (Process32First(oHnd, ref oProcInfo) == false)
                return null;

            do
            {
                if (iCurrentPid == oProcInfo.th32ProcessID)
                    iParentPid = (int)oProcInfo.th32ParentProcessID;
            }
            while (iParentPid == 0 && Process32Next(oHnd, ref oProcInfo));

            if (iParentPid > 0)
                return Process.GetProcessById(iParentPid);
            else
                return null;
        }

        static uint TH32CS_SNAPPROCESS = 2;

        [StructLayout(LayoutKind.Sequential)]
        public struct PROCESSENTRY32
        {
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szExeFile;
        };

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);

        [DllImport("kernel32.dll")]
        static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);

        [DllImport("kernel32.dll")]
        static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
    }

I took this code snippet and improved upon it and made myself the following static class. This class more easily exposes:

  • Parent Process Id
  • Parent Process name
  • Parent process executable name
  • Full path to parent process executable
  • Directory name where the parent process executable resides
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;

namespace ParentProcess
{
    public class ParentProcess
    {
        public static String ProcessName
        {
            get { return GetParentProcess().ProcessName; }
        }

        public static int ProcessId
        {
            get { return GetParentProcess().Id; }
        }

        public static String FullPath
        {
            get
            {
                return GetParentProcess().MainModule.FileName;
            }
        }

        public static String FileName
        {
            get
            {
                return System.IO.Path.GetFileName(GetParentProcess().MainModule.FileName);
            }
        }

        public static String DirectoryName
        {
            get
            {
                return System.IO.Path.GetDirectoryName(GetParentProcess().MainModule.FileName);
            }
        }

        private static Process GetParentProcess()
        {
            int iParentPid = 0;
            int iCurrentPid = Process.GetCurrentProcess().Id;

            IntPtr oHnd = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

            if (oHnd == IntPtr.Zero)
                return null;

            PROCESSENTRY32 oProcInfo = new PROCESSENTRY32();

            oProcInfo.dwSize =
            (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(PROCESSENTRY32));

            if (Process32First(oHnd, ref oProcInfo) == false)
                return null;

            do
            {
                if (iCurrentPid == oProcInfo.th32ProcessID)
                    iParentPid = (int)oProcInfo.th32ParentProcessID;
            }
            while (iParentPid == 0 && Process32Next(oHnd, ref oProcInfo));

            if (iParentPid > 0)
                return Process.GetProcessById(iParentPid);
            else
                return null;
        }

        static uint TH32CS_SNAPPROCESS = 2;

        [StructLayout(LayoutKind.Sequential)]
        public struct PROCESSENTRY32
        {
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szExeFile;
        };

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);

        [DllImport("kernel32.dll")]
        static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);

        [DllImport("kernel32.dll")]
        static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
    }
}

This makes it easier for me to simply include the class above in my code and make simple calls:

String exename = ParentProcess.FileName;
String FullPathToExe = ParentProcess.FullPath;
String DirectoryInWhichExeResides= ParentProcess.DirectoryName;

…and the pid and process name, etc…

I hope this helps you.

References
http://www.facebook.com/note.php?note_id=73447531256
http://www.debugging.com/bug/6657
http://www.eggheadcafe.com/software/aspnet/35541264/how-to-get-the-parent-pro.aspx


No Copyright.

Generic XML Serializer Class for C# and an XML Serialization usage example

UPDATE: This is now part of the EasyXml NuGet package. There is also an EasyXml.Sources NuGet package.

Hey all,

Today I was working on XML Serialization.

After learning how to do it, I discovered it takes four lines of code to write an XML and four lines of code to read in an XML.

However, I prefer one line of code to four so I made a Serializer.cs class with two static functions.

After thinking about it, I made these classes generic so they work with any type. I hope this helps someone.

using System;
using System.IO;
using System.Xml.Serialization;

namespace BlogTool
{
    public class Serializer
    {
        #region Functions
        public static void SerializeToXML(T t, String inFilename)
        {
            XmlSerializer serializer = new XmlSerializer(t.GetType());
            TextWriter textWriter = new StreamWriter(inFilename);
            serializer.Serialize(textWriter, t);
            textWriter.Close();
        }

        public static T DeserializeFromXML(String inFilename)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(T));
            TextReader textReader = new StreamReader(inFilename);
            T retVal = (T)deserializer.Deserialize(textReader);
            textReader.Close();
            return retVal;
        }
        #endregion
    }
}

So now if you have a class, you can easily serialize it to and from an XML with a single line.

Here is an example Project that contains these files:

  • Blog.cs
  • BlogList.cs
  • Program.cs
  • Serializer.cs

Blog.cs

using System;

namespace BlogTool
{
    [Serializable()]
    public class Blog
    {
        #region Member Variables
        String mBlogUrl;
        String mCategory;
        #endregion

        #region Constructors
        public Blog()
        {
        }

        public Blog(String inURL, String inCategory)
        {
            mBlogUrl = inURL;
            mCategory = inCategory;
        }
        #endregion

        #region Properties
        public String BlogUrl
        {
            get { return mBlogUrl; }
            set { mBlogUrl= value; }
        }

        public String Category
        {
            get { return mCategory; }
            set { mCategory= value; }
        }
        #endregion
    }
}

BlogList.cs

using System.Collections.Generic;

namespace BlogTool
{
    [Serializable]
    public class BlogList
    {
        #region Member Variables
        List mBlogs = new List();
        #endregion

        #region Constructors

        /*
		 * The default constructor
 		 */
        public BlogList()
        {
        }

        #endregion

        #region Properties
        public List Blogs
        {
            get { return mBlogs; }
            set { mBlogs = value; }
        }
        #endregion
    }
}

Program.cs

namespace BlogTool
{
    class Program
    {
        static void Main(string[] args)
        {
            BlogList bloglist = new BlogList();
            Blog b1 = new Blog("http://rhyous.com","Software");
            Blog b2 = new Blog("http://www.alittletipsy.com", "Crafts");
            bloglist.Blogs.Add(b1);
            bloglist.Blogs.Add(b2);
            Serializer.SerializeToXML(bloglist, "FavoriteBlogs.xml");
        }
    }
}

Serializer.cs

using System;
using System.IO;
using System.Xml.Serialization;

namespace BlogTool
{
    public class Serializer
    {
        #region Functions
        public static void SerializeToXML(T t, String inFilename)
        {
            XmlSerializer serializer = new XmlSerializer(t.GetType());
            TextWriter textWriter = new StreamWriter(inFilename);
            serializer.Serialize(textWriter, t);
            textWriter.Close();
        }

        public static T DeserializeFromXML(String inFilename)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(T));
            TextReader textReader = new StreamReader(inFilename);
            T retVal = (T)deserializer.Deserialize(textReader);
            textReader.Close();
            return retVal;
        }
        #endregion
    }
}

Ok, so now that you have the files, you can run this program.

In the bin\debug or bin\release directory where you executable is placed when you build, you will see the FavoriteBlogs.xml file. The XML should look as follows:

<!--?xml version="1.0" encoding="utf-8"?-->

      
127.0.0.1 or ::1
Software http://www.alittletipsy.com Crafts

I know, this is not written very well as a walk-thru, but I wrote it fast. Maybe I will clean it up later.

Update: 5/8/14

namespace Rhyous.EasyXml
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Xml;
    using System.Xml.Serialization;

    [ExcludeFromCodeCoverage]
    public static class Serializer
    {
        #region Functions

        /// <summary>
        /// This function writes the serialized XML to the file name passed in.
        /// </summary>
        /// <typeparam name="T">The object type to serialize.</typeparam>
        /// <param name="t">The instance of the object.</param>
        /// <param name="outFilename">The file name. It can be a full path.</param>
        /// <param name="inOmitXmlDeclaration"></param>
        /// <param name="inNameSpaces"></param>
        /// <param name="inEncoding"></param>
        public static void SerializeToXml<T>(T t, string outFilename, bool inOmitXmlDeclaration = false, XmlSerializerNamespaces inNameSpaces = null, Encoding inEncoding = null)
        {
            MakeDirectoryPath(outFilename);
            var ns = inNameSpaces;
            if (ns == null)
            {
                ns = new XmlSerializerNamespaces();
                ns.Add("", "");
            }
            var serializer = new XmlSerializer(t.GetType());
            var textWriter = (TextWriter)new StreamWriter(outFilename);
            if (inEncoding != null && inEncoding.Equals(Encoding.UTF8))
                textWriter = new Utf8StreamWriter(outFilename);
            var xmlWriter = XmlWriter.Create(textWriter, new XmlWriterSettings { OmitXmlDeclaration = inOmitXmlDeclaration });
            serializer.Serialize(xmlWriter, t, ns);
            textWriter.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="outFilename"></param>
        private static void MakeDirectoryPath(string outFilename)
        {
            var dir = Path.GetDirectoryName(outFilename);
            if (dir != null && !Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
        }

        /// <summary>
        /// This function returns the serialized XML as a string
        /// </summary>
        /// <typeparam name="T">The object type to serialize.</typeparam>
        /// <param name="t">The instance of the object.</param>
        /// <param name="inOmitXmlDeclaration"></param>
        /// <param name="inNameSpaces"></param>
        /// <param name="inEncoding"></param>
        public static string SerializeToXml<T>(T t, bool inOmitXmlDeclaration = false, XmlSerializerNamespaces inNameSpaces = null, Encoding inEncoding = null)
        {
            var ns = inNameSpaces;
            if (ns == null)
            {
                ns = new XmlSerializerNamespaces();
                ns.Add("", "");
            }
            var serializer = new XmlSerializer(t.GetType());
            var textWriter = (TextWriter)new StringWriter();
            if (inEncoding != null && inEncoding.Equals(Encoding.UTF8))
                textWriter = new Utf8StringWriter();
            var xmlWriter = XmlWriter.Create(textWriter, new XmlWriterSettings { OmitXmlDeclaration = inOmitXmlDeclaration });
            serializer.Serialize(xmlWriter, t, ns);
            return textWriter.ToString();
        }

        /// <summary>
        /// This function deserializes the XML file passed in.
        /// </summary>
        /// <typeparam name="T">The object type to serialize.</typeparam>
        /// <param name="inFilename">The file or full path to the file.</param>
        /// <returns>The object that was deserialized from xml.</returns>
        public static T DeserializeFromXml<T>(String inFilename)
        {
            if (string.IsNullOrWhiteSpace(inFilename))
            {
                return default(T);
            }
            // Wait 1 second if file doesn't exist, in case we are waiting on a
            // separate thread and beat it here.
            if (!File.Exists(inFilename))
                Thread.Sleep(1000);

            // File should exist by now.
            if (File.Exists(inFilename))
            {
                var deserializer = new XmlSerializer(typeof(T));
                var textReader = (TextReader)new StreamReader(inFilename);
                var reader = new XmlTextReader(textReader);
                reader.Read();
                var retVal = (T)deserializer.Deserialize(reader);
                textReader.Close();
                return retVal;
            }
            throw new FileNotFoundException(inFilename);
        }

        /// <summary>
        /// This function deserializes the XML string passed in.
        /// </summary>
        /// <typeparam name="T">The object type to serialize.</typeparam>
        /// <param name="inString">The string containing the XML.</param>
        /// <returns>The object that was deserialized from xml.</returns>
        public static T DeserializeFromXml<T>(ref string inString)
        {
            if (string.IsNullOrWhiteSpace(inString))
            {
                return default(T);
            }
            var deserializer = new XmlSerializer(typeof(T));
            var textReader = (TextReader)new StringReader(inString);
            var retVal = (T)deserializer.Deserialize(textReader);
            textReader.Close();
            return retVal;
        }
        #endregion

        #region UTF8StringWriter
        public sealed class Utf8StringWriter : StringWriter
        {
            public override Encoding Encoding { get { return Encoding.UTF8; } }
        }

        public sealed class Utf8StreamWriter : StreamWriter
        {
            public Utf8StreamWriter(string file)
                : base(file)
            {
            }

            public override Encoding Encoding { get { return Encoding.UTF8; } }
        }
        #endregion
    }
}

Sources:
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization

Where to get a Select a Directory Dialog (or Folder Picker or Choose Directory Dialog) in WPF?

Ok, so to my dismay, Microsoft has not created a default widget for selecting a directory.

I searched and searched and searched. Found some here are there, but none were really what I wanted.

I finally found one I like, so I thought I would share it with you. Here is what I like about the one I found:

http://www.ookii.org/software/dialogs/

How to get your project's version dynamically in C#?

Ok, so I wanted to create a little Help | About page that looks like this.

MyProgram 1.0.0.5

Author: Jared Barneck

Contributors: John, Mike, Mark, Tom, Bill, Jane, Ryan, Josh

I don’t really want to have to remember to change the version in the help file with each release, so I wanted to get the version dynamically.

Turns out that you can get the version as a string with a single line of code:

String theVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

Once you have the version as a string, you can display it how you want.

Tutorial – Creating a StaticResource and Binding to it

The XAML allows you to provide what are called StaticResources. Such resources can be given to a Window or to certain controls.

For this tutorial, I assume you are in Visual Studio 2008. I assume that you already know how to create a new Project and choose WPF Application. All examples assume you have a new WPF Application.

So lets get started with three examples of binding to StaticResources.

Example 1 – Making a String a StaticResource and Binding to it

This example will demonstrate instantiating a String as a StaticResource and binding a TextBox to it.

Step 1 – Add the elements

  1. Add three TextBox elements into the default Grid control.
            <ListBox Margin="12,12,0,0" Name="listBox1" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" />
            <ListBox Margin="138,12,20,0" Name="listBox2" Height="100" VerticalAlignment="Top" />
            <TextBox Margin="12,118,0,121" Name="textBox1" Width="120" IsReadOnly="True" HorizontalAlignment="Left" />
            <TextBox Margin="138,118,20,121" Name="textBox2" Width="120" IsReadOnly="True" HorizontalAlignment="Left" />
    
    

Step 2 – Add the static resources

  1. Add an xmlns reference to the System namespace. This is done by adding the xmlns:System line to as an attribute to the top Window element as shown:
    <Window x:Class="StaticResourceBinding.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="300" Width="300">
    
  2. Create a Windows.Resources section in the XAML and add three Strings to it as StaticResources.
        <Window.Resources>
            <System:String x:Key="FirstName">Jared</System:String>
            <System:String x:Key="LastName">Barneck</System:String>
            <System:String x:Key="Alias">Rhyous</System:String>
        </Window.Resources>
    

Step 3 – Adding Binding to each TextBox element’s Text property

  1. Configure the three TextBox elements to bind to each String added as a StaticResource by adding a Text attribute.
            <TextBox Text="{StaticResource FirstName}" Height="23" Margin="51,25,107,0" Name="textBox1" VerticalAlignment="Top" />
            <TextBox Text="{StaticResource LastName}" Height="23" Margin="51,54,107,0" Name="textBox2" VerticalAlignment="Top" />
            <TextBox Text="{StaticResource Alias}" Height="23" Margin="51,83,107,0" Name="textBox3" VerticalAlignment="Top" />
    

The final XAML looks as follows. No changes were made to the code behind at all.

<Window x:Class="StaticResourceBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <System:String x:Key="FirstName">Jared</System:String>
        <System:String x:Key="LastName">Barneck</System:String>
        <System:String x:Key="Alias">Rhyous</System:String>
    </Window.Resources>
    <Grid>
        <TextBox Height="23" Margin="51,25,107,0" Name="textBox1" VerticalAlignment="Top" Text="{StaticResource FirstName}"/>
        <TextBox Height="23" Margin="51,54,107,0" Name="textBox2" VerticalAlignment="Top" Text="{StaticResource LastName}"/>
        <TextBox Height="23" Margin="51,83,107,0" Name="textBox3" VerticalAlignment="Top" Text="{StaticResource Alias}"/>
    </Grid>
</Window>

Example 2 – Declaring an array as a StaticResource and Binding a ListBox to it

This example will demonstrate instantiating arrays as StaticResources and binding a ListBox to the arrays.

To show an example of building onto existing or previous learned knowledge, we are going to also implement binding each TextBox's Text properties to the ListBox's SelectedItem property.

Step 1 – Add the elements

  1. Add two ListBox and two TextBox elements into the default Grid control.
            <ListBox Margin="12,12,0,0" Name="listBox1" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" />
            <ListBox Margin="138,12,20,0" Name="listBox2" Height="100" VerticalAlignment="Top" />
            <TextBox Margin="12,118,0,121" Name="textBox1" Width="120" IsReadOnly="True" HorizontalAlignment="Left" />
            <TextBox Margin="138,118,20,121" Name="textBox2" Width="120" IsReadOnly="True" HorizontalAlignment="Left" />
    

Step 2 – Add the static resources

  1. Add an xmlns reference to the System namespace. This is done by adding the xmlns:System line to as an attribute to the top Window element as shown:
    <Window x:Class="StaticResourceBinding.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="300" Width="300">
    
  2. Create a Windows.Resources section in the XAML and add two arrays as StaticResources: one an array of strings, and one an array of integers.
        <Window.Resources>
            <x:Array x:Key="StringList" Type="System:String">
                <System:String>Line 1</System:String>
                <System:String>Line 2</System:String>
                <System:String>Line 3</System:String>
                <System:String>Line 4</System:String>
            </x:Array>
            <x:Array x:Key="IntArray" Type="System:Int32">
                <System:Int32>100</System:Int32>
                <System:Int32>200</System:Int32>
                <System:Int32>300</System:Int32>
                <System:Int32>400</System:Int32>
            </x:Array>
        </Window.Resources>
    

Step 3 – Adding Binding

  1. Configure one ListBox's Text property to bind to the String array and the other ListBox's Text property to bind to the Int32 array.
            <ListBox ItemsSource="{StaticResource StringList}" Margin="12,12,0,0" Name="listBox1" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" />
            <ListBox ItemsSource="{StaticResource IntArray}" Margin="138,12,20,0" Name="listBox2" Height="100" VerticalAlignment="Top" />
    
  2. We will also add binding to show you how you can combine binding to StaticResources and binding to another element’s property.Bind one TextBox to the listBox1.SelectedItem property and bind the other to the listBox2.SelectedItem.
            <TextBox Text="{Binding ElementName=listBox1, Path=SelectedItem}" Margin="12,118,0,121" Name="textBox1" Width="120" IsReadOnly="True" HorizontalAlignment="Left" />
            <TextBox Text="{Binding ElementName=listBox2, Path=SelectedItem}" Margin="138,118,20,121" Name="textBox2" Width="120" IsReadOnly="True" HorizontalAlignment="Left" />
    
  3. Build your application and run it. Notice as you select an item in the list, it displays.

The final XAML looks as follows. No changes were made to the code behind at all.

<Window x:Class="StaticResourceBinding2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <x:Array x:Key="StringList" Type="System:String">
            <System:String>Line 1</System:String>
            <System:String>Line 2</System:String>
            <System:String>Line 3</System:String>
            <System:String>Line 4</System:String>
        </x:Array>
        <x:Array x:Key="IntArray" Type="System:Int32">
            <System:Int32>100</System:Int32>
            <System:Int32>200</System:Int32>
            <System:Int32>300</System:Int32>
            <System:Int32>400</System:Int32>
        </x:Array>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{StaticResource StringList}" Margin="12,12,0,0" Name="listBox1" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" />
        <ListBox ItemsSource="{StaticResource IntArray}" Margin="138,12,20,0" Name="listBox2" Height="100" VerticalAlignment="Top" />
        <TextBox Text="{Binding ElementName=listBox1, Path=SelectedItem}" Margin="12,118,0,121" Name="textBox1" Width="120" IsReadOnly="True" HorizontalAlignment="Left" />
        <TextBox Text="{Binding ElementName=listBox2, Path=SelectedItem}" Margin="138,118,20,121" Name="textBox2" Width="120" IsReadOnly="True" HorizontalAlignment="Left" />
    </Grid>
</Window>

Example 3 – Adding Resources to a Control

In the previous two examples, we added the resources to the main Window object. However, a resource can be added to a control.

This example will demonstrate instantiating an array as a StaticResources for a control. We will then bind a TabControl’s ItemSource property to this array. This will cause a Tab to be created for each item in the array.

Step 1 – Add the elements

  1. Add a TabControl into the default Grid control.
            <TabControl Name="tabControl1">
    

Step 2 – Add the static resources

  1. Add an xmlns reference to the System namespace. This is done by adding the xmlns:System line to as an attribute to the top Window element as shown:
    <Window x:Class="StaticResourceBinding.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="300" Width="300">
    
  2. Create a Grid.Resources section in the XAML and add an array as a StaticResource under the Grid control.
            <Grid.Resources>
                <x:Array x:Key="TabList" Type="System:String">
                    <System:String>Tab 1</System:String>
                    <System:String>Tab 2</System:String>
                    <System:String>Tab 3</System:String>
                </x:Array>
            </Grid.Resources>
    

Step 3 – Adding Binding to the TabControl’s ItemSource Property

  1. Bind the TabControl's ItemSource property to bind to the String array.
            <TabControl Name="tabControl1" ItemsSource="{StaticResource TabList}">
    

The final XAML looks as follows. No changes were made to the code behind at all.

<Window x:Class="StaticResourceBinding3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.Resources>
            <x:Array x:Key="TabList" Type="System:String">
                <System:String>Tab 1</System:String>
                <System:String>Tab 2</System:String>
                <System:String>Tab 3</System:String>
            </x:Array>
        </Grid.Resources>
        <TabControl Name="tabControl1" ItemsSource="{StaticResource TabList}">
        </TabControl>
    </Grid>
</Window>

Hey, there is nothing wrong with more examples, so if you have an example of your own feel free to add it as a comment.


Copyright ® Rhyous.com – Linking to this post is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.