Archive for the Category code

 
 

Interesting blog posts (February 11)

Ohad’s Weblog IE7Pro – a must have add-in if you use IE7 !

Simple Living Simple Thinking Zip and Unzip files programmatically

Bill’s House O Insomnia AcceptChanges and Updates once more

Certifications and Software Development More C# 3.0 ? Extension Methods

Thom Robbins .NET Weblog Introduction to Building XML Web Services with ASMX

Validating properties in a PropertyGrid control

The easiest way to validate a certain property is using the PropertyValueChanged event.The code bellow shows how to limit the valid range of Age property to be 1-150

using System.Windows.Forms; 
namespace WindowsApplication1 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
            //init person object  
            Person person = new Person(); 
            person.FirstName = "George"; 
            person.Age = 33; 
            propertyGrid.SelectedObject = person; 
            propertyGrid.PropertyValueChanged+=  new PropertyValueChangedEventHandler( propertyGrid_PropertyValueChanged ); 
        } 
 
        private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) 
        { 
            if (e.ChangedItem.Label == "Age" && !IsAgeValid((int)e.ChangedItem.Value) ) 
            { 
                // the entered age value is wrong - show error message 
                e.ChangedItem.PropertyDescriptor.SetValue( propertyGrid.SelectedObject, e.OldValue); 
                MessageBox.Show("Wrong age", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
            } 
        } 
 
        ///<summary>
        /// Determines whether age is valid 
        /// </summary>  
        /// <param name="age">The age.</param>  
        /// <returns>  
        /// <c>true</c> if is age valid ; otherwise, <c>false</c>. 
        /// </returns>  
        private static bool IsAgeValid( int age ) 
        { 
            return ((age > 0) && (age < 150)); 
        } 
    } 
}

 

Technorati tags: propertygrid, dotnet, programming

Interesting blog posts

Jon Galloway – Google Personalized Homepage (IG) – Read a post inline

David Hayden – Validation Application Block Presentation and New Features – Enterprise Library 3.0

ScottGu’s Blog – ASP.NET AJAX 1.0 Released

Dmitryr’s blog – Finding out the current trust level in ASP.NET

Michael’s Blog – Transforms, Opacity Masks and Animation to Create an Animated Reflection in WPF/E

 

Technorati tags: blogging, programming, c#, asp.net

Return the value for the specific attribute using XPathNodeIterator

I will use the following XML document in the example below.

<?xml version="1.0" encoding="utf-8" ?>
<users>
  <user FirstName="Tom" LastName="Adams" Age="23" />
  <user FirstName="Jhon" LastName="Brams" Age="17" />
  <user FirstName="Bill" LastName="Smith" Age="33" />
</users>

The code prints out all the user nodes of the users node that  have an age with a value greater than 17.

// Read the xml from resource file 
string xml = Resources.Users; 
using (StringReader sr = new StringReader(xml)) 
{ 
    XPathDocument document = new XPathDocument(sr); 
    XPathNavigator navigator = document.CreateNavigator(); 
    XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable); 
    //Selects all the user nodes of the users node that 
    // have an age with a value greater than 17  
    XPathNodeIterator it = navigator.Select("/users/user[@Age>17]"); 
    while (it.MoveNext()) 
    { 
        // access the atributes  
        string firstName = it.Current.GetAttribute("FirstName", ns.DefaultNamespace); 
        string lastName = it.Current.GetAttribute("LastName", ns.DefaultNamespace); 
        // Print out  
        Console.WriteLine( "{0} {1}", firstName, lastName ); 
    } 
}

Detecting Net Framework directory

You can use

System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()

or

HttpRuntime.ClrInstallDirectory property ( don’t forget to add reference to System.Web.dll )


Page 2 of 512345