WPF Logical Resources

WPF Logical Resources
A Logical Resource in WPF is a XAML defined object that can be used by WPF elements inside the user interface.
You can declare a logical resource for the entire window and will be usable by all elements on that window, or you can declare it inside the Resources collection of an element, where it will be usable only within that element. You can also declare it for an entire application and it will usable anywhere.

       
              
              
       



       
            	
              	       
                      
            	
       



       
              
              
       


The application resources collection can be found inside the App.xaml file located in your project.

Every resource must have an x:Key property which defines the name of that specific object. It doesn’t have to be unique in the entire application, but it must be unique in the Resource Collection in which it was defined.

A resource in XAML can be accessed by using the {StaticResource name} syntax.


Mnemonic Keys in WPF

Mnemonic keys are keys that when pressed in combination with the ALT key move the focus to the control they are assigned to.

Labels in WPF have a built in support for mnemonic keys.

A mnemonic key can be specified by using the underscore symbol ( _ ) in front of the key you wish to assign. At runtime, you will notice that the key will appear underlined when the ALT key will be pressed.

Here is an example in XAML:



In this example, we have designated to Label1 a TextBox named TextBox1 and when the ALT+S keys will be pressed in combination, the textbox will receive focus.

SQL Banker's Rounding Function

The Banker's Rounding, also known as the Gaussian Rounding or Rounding to Even, rounds a given number if its fraction is half to the nearest even number.

The bellow SQL function rounds any given decimal number to a number of given digits and returns the rounded decimal number. It reads the number from right to left depending on the number of digits required and then rounds it. If the fraction is not half, then common rounding takes place.

CREATE FUNCTION [dbo].[BankersRounding]
(
	@value DECIMAL(28, 10),
	@digits INT
)

RETURNS DECIMAL (28, 10)
AS
BEGIN

	DECLARE @roundedValue DECIMAL(28, 10)
	DECLARE @roundingType INT 
	
		IF (ABS(@value - ROUND(@value, @digits, 1)) * POWER(10, @digits + 1) = 5)
			BEGIN
			
				IF (ROUND(ABS(@value) * POWER(10, @digits), 0, 1) % 2 = 0)
					BEGIN
						SET @roundingType = 1 
					END
				ELSE
					BEGIN
						SET @roundingType = 0
					END
						
				SET @roundedValue = ROUND(@value, @digits, @roundingType)
			END
				
		ELSE
			BEGIN
				SET @roundedValue = ROUND(@value, @digits)
			END
						
	RETURN @roundedValue
END

In C# there's the Round method inside the Math class that can take an enumeration overload called MidpointRounding.ToEven that does the exact same thing.

Here's an example:

using System;

namespace BankersRoundingProject
{
    public class BankersRounding
    {
        public static decimal GetRoundedValue(decimal value, int digits)
        {
            return Math.Round(value, digits, MidpointRounding.ToEven);
        }
    }
}

Temperature converting

In this post I'll try to explain you how to convert temperatures from Celsius to Fahrenheit or vice versa. I will create a small WinForms application that actually does that.

I used two doubles to store the temperature's values (one for Celsius and one for Fahrenheit), two labels (of which one will be displayed after the actual transformation takes places and will contain the transformed value), one combobox and one textbox. Oh... and almost forgot about the button that triggers the transformation and displays the result.

One more thing you will notice is that I've used Math.Round with two decimals to get a nice transformation result and I've used the String.Format method to display the transformed value instead of the usual concatenation.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TemperatureTrasformer
{
    public partial class mainForm : Form
    {
        double celsius;
        double fahrenheit;

        ComboBox cboxTempType;
        TextBox txtTemp;
        Label lblInitial;
        Label lblResult;
        Button btnConvert;

        public mainForm()
        {
            InitializeComponent();

            //Set up the form's proprieties
            this.Text = "Temperature Converter";
            this.ClientSize = new Size(250, 150);
            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;

            //Set up the form's controls
            lblInitial = new Label();
            lblInitial.Location = new Point(10, 10);
            lblInitial.AutoSize = true;
            lblInitial.Text = "Enter the temperature you want to convert:";

            txtTemp = new TextBox();
            txtTemp.Location = new Point(10, lblInitial.Bottom + 5); 
            txtTemp.Width = 100;

            cboxTempType = new ComboBox();
            cboxTempType.Location = new Point(txtTemp.Right + 10, lblInitial.Bottom + 5);
            cboxTempType.Width = 100;
            cboxTempType.DropDownStyle = ComboBoxStyle.DropDownList;
            cboxTempType.Items.Add("Celsius");
            cboxTempType.Items.Add("Fahrenheit");
            cboxTempType.SelectedIndex = 0;

            btnConvert = new Button();
            btnConvert.Text = "Convert";
            btnConvert.Location = new Point(10, txtTemp.Bottom + 10);
            btnConvert.AutoSize = true;
            btnConvert.Click += new EventHandler(btnConvert_Click);

            lblResult = new Label();
            lblResult.Location = new Point(10, btnConvert.Bottom + 10);
            lblResult.AutoSize = true;

            this.Controls.Add(lblInitial);
            this.Controls.Add(cboxTempType);
            this.Controls.Add(txtTemp);
            this.Controls.Add(btnConvert);
        }

        void btnConvert_Click(object sender, EventArgs e)
        {
            //Convert to Fahrenheit
            if (cboxTempType.SelectedIndex == 0)
            {
                celsius = Convert.ToDouble(txtTemp.Text);
                fahrenheit = Math.Round((celsius * 9) / 5 + 32, 2);
                lblResult.Text = String.Format("The temperature in Fahrenheit is {0} degrees", fahrenheit);
                this.Controls.Add(lblResult);
            }
            //Convert to Celsius
            else if (cboxTempType.SelectedIndex == 1)
            {
                fahrenheit = Convert.ToDouble(txtTemp.Text);
                celsius = Math.Round((fahrenheit - 32) / 9 * 5, 2);
                lblResult.Text = String.Format("The temperature in Celsius is {0} degrees", celsius);
                this.Controls.Add(lblResult);
            }
        }
    }
}

Installing IIS 5.1 (for Win XP) and enabling .aspx pages

Windows XP can only accept IIS 5.1, but IIS 5.1 does not accept by default .aspx pages. This is because .aspx pages were introduced with the Framework 2.0 while IIS 5.1 predates this framework.

Installing IIS 5.1 on Windows XP:

1. Go to Control Panel
2. Open Add/Remove Programs
3. Select the Add/Remove Windows Components Tab
4. Select Internet Information Services (IIS) from the list
5. Click Next and follow the wizard

Once the installation is complete you can open your web browser and navigate to http://localhost/. IF IIS was installed correctly you should see its homepage. You will notice on the IIS homepage the page formats that it supports, but .aspx pages will not be present. To enable these pages on IIS you will need to have at least .NET Framework 2.0 installed.

Enabling .aspx pages on ISS 5.1:

1. Navigate to C:\Windows\Microsoft .NET\Framework
2. Enter the framework directory (v4.0.30319 for .NET 4.0 as example)
3. Search for a file named aspnet_regiis.exe
4. Run this program with the “ -i” command line parameter

You should see how IIS is reconfigured to use the latest framework. Now that everything should is set up, all you’ll have to do is to copy the website in the C:\Inetpub\wwwroot directory. On the local machine you can run the website by navigating to http://localhost/ or from any computer on the intranet by accessing your machine’s IP.

NOTE that IIS 5.1 can host only one site.