Blog

posts by Fons Sonnemans

Pocket PC TimeBox Control

0 Comments
By Fons Sonnemans, 27-mei-2002

Download TimeBox.zip

Introduction

I own a Pocket PC since a few months. It's a nice peace of hardware although I didn't use it much. There wasn't much practical use for it. Now I have decided to write my own Time Registration and maybe my own Car Mileage Administration application.  In both Smart Device Applications I have to enter a Time regullary. Microsoft didn't supply me with a good control for this so I decided to write one.

Writing a Smart Device Applications is made easy with the introduction of Visual Studio.NET 2003 and the .NET Compact Framework. In this article I describe how I have written a TimeBox control for the Pocket PC. It's not much different from a normal Windows Forms control.


TimeBox Sample running in the Pocket PC Emulator


I plan to use this TimeBox in my application so I have placed it in an separate project. Due to the low memory footprint you also have to create a designer able version of it. This version will have extra design-time only code (methods and attributes).

Controls Project

The ReflectionIT.Windows.Forms project consists of 3 controls: UpDownButton, SpinButton and TimeBox. The UpDownButton is used by the SpinButton control to draw a Up and a Down button. The SpinButton is used by the TimeBox to spin the hours and minutes.

Once you have compiled your control you need to copy it to specific locations so that Microsoft Visual Studio.NET and the .NET Compact Framework can find it. The runtime version of your control should be copied to the C:\Program Files\Microsoft Visual Studio .NET\CompactFrameworkSDK\v1.0.5000\Windows CE\ directory if you have done the default install.

I have created a special batch file (copytowince 2003.cmd) which compiles the controls project and the designer project and copies the DLL's to the correct folders.

Time class

The Time class is used to store the Hours and Minutes in. It supports arithmetic, comparison and conversion operator overloading.

Time t1=new Time(10,45);
Time t2=new Time(0,15);
Time t3=t1+ t2;     // 11:00

if(t3 < (12 *60)){
    Console.WriteLine(String.Format("Good morning, it is {0}", t3));
}

The Time class has a TypeConverter which is used to support designer serialization.

public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, objectvalue, Type destinationType){
    if(destinationType==typeof(InstanceDescriptor)) {
        // This will make a Time a serialiable property in the IntializeComponent method
        ConstructorInfo ci=typeof(Time).GetConstructor(new Type[]{typeof(int),typeof(int)});
        Time t =(Time)value;
        returnnew InstanceDescriptor(ci,newobject[]{t.Hours, t.Minutes});
    }

    if(destinationType.Equals(typeof(String))){
        returnvalue.ToString();
    }else{
        returnbase.ConvertTo (context,culture, value,destinationType);
    }
}

Designer Project

The designer version of the control project has a special Conditional Compilation Constant named 'NETCFDESIGNTIME'. This constant is used to add extra code for design-time support. Attributes are added to methods. Extra classes and methods are included for resizing, serialization and type conversion. The output of this project should be copied to the C:\program files\microsoft visual studio .net 2003\compactframeworksdk\v1.0.5000\Windows CE\Designer folder.

#if NETCFDESIGNTIME
[
Category("Appearance"),
Description("The current time of the timebox."),
]
#endif
public ReflectionIT.Time Time {
    get {
        return(this._time);
    }
    set {
        boolchanged=(_time !=value);
        this._time =value;

        if(changed){
            Invalidate();
            OnTimeChanged();
        }
    }
}

#if NETCFDESIGNTIME
publicvoid ResetTime() {
    this.Time =new Time(0);
}

publicbool ShouldSerializeTime() {
    returnthis.Time !=new Time(0);
}
#endif

Sample Client Project

The TimeBox Sample client project has two TimeBox controls and a disabled TextBox control. The difference between the two times is displayed in the TextBox using the TimeChanged event of the TimeBoxes.

private void timeBox_TimeChanged ( object sender , System.EventArgs e) {
    if(timeBoxStop.Time >=timeBoxStart.Time) {
        textBoxDuration.Text = (timeBoxStop.Time -timeBoxStart.Time).ToString();
    }else{
        textBoxDuration.Text = new ReflectionIT.Time().ToString();
    }
}

Links:

I have used the following articles to create this control and to write this article:

Conclusion

Writing this TimeBox control was realy a challenge. The documentation of the .NetCF is very limited. I hope that this article and sample code will help you write your own controls for the Pocket PC.

I hope I can port these applications to a Smartphone solution as soon as Microsoft will produce a .NET Compact Framework for it. A Pocket PC is just to big to carry around the whole day. Hopefully it works!

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl

READ MORE

WinForm WizardHeader Control

0 Comments
By Fons Sonnemans, 01-mei-2002

By Fons Sonnemans (May 2002)

Download Wizard.zip

Introduction

In Windows XP all wizard dialogs have a nice looking header. This WizardHeader control can be used to add these headers to your wizard forms.

Usage

The following steps explain how you can use the WizardHeader control in your WinForm projects.

Step1 - Download the Zip file and extract it.
Download Wizard.zip  and extract it to you local harddrive. This version was compiled using the final version of Microsoft .NET Framework SDK. The C# source of the WizardHeader control is included.

Step2 - Design the Form
Create a Windows Application project.

Step3 - Add the WizardHeader control to the Toolbox
Right-click the Toolbox and select 'Customize Toolbox...'Select the '.NET Framework Components' Tab and click the 'Browse' button.


Open the 'WizardControlLibary.dll' in the Wizard\WizardControlLibrary\Bin\Release folder. The 'WizardHeader' control is checked and will be visible in the Toolbox. Click OK.

Step4 - Add a WizardHeader to the form and set its properties 

Set the properties:

  • Text
  • Description
  • Image

Step5 - Run the application


Conclusion

The WizardHeader control is an small control. Use it in your wizard forms or study it to learn more about creating .NET controls.

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl

READ MORE

Add a Splitter to a WinForm

2 Comments
By Fons Sonnemans, 01-apr-2002

Download SplitterDemo.zip

Introduction

I used to program in Visual Basic 6.0. It is a great tool but not perfect. It was lacking some crucial controls. A Splitter control was one of them. Microsoft has added it to the .NET framework (thanks) but forgot to make it easy to use. You must place it on a Form, set some properties for it and then also some (Dock) properties of the other two controls. Then you have to set the order of the controls correctly by using 'Bring to Front' or 'Send to Back'.

This article demonstrates the use of a simple AddSplitter() method which simplifies the use of splitters.

AddSplitter() method

/// <summary>
/// Add a splitter between the two given controls
/// </summary>
/// <param name="control1">Control 1</param>
/// <param name="control2">Control 2</param>
/// <param name="vertical">Vertical orientation</param>
/// <returns>The created Splitter</returns>
public static Splitter AddSplitter(Control control1, Control control2,bool vertical)
{
    Control parent =control1.Parent;

    // Validate
    if (parent!=control2.Parent)
        thrownew ArgumentException(
                "Both controls must be placed on the same Containter");

    if(parent.Controls.Count > 2)
        thrownew ArgumentException(
                "There may only be 2 controls on the Container");

     parent.SuspendLayout();

    // Move control2 before control1
    if (parent.Controls.IndexOf(control2) > parent.Controls.IndexOf(control1))
        parent.Controls.SetChildIndex(control2,0);

    // Create splitter
    Splitter splitter=new Splitter();
    splitter.Dock = System.Windows.Forms.DockStyle.Left;

    // Set controls properties
    control2.Dock = DockStyle.Fill;
    if(vertical)
    {
        control1.Dock = DockStyle.Left;
        splitter.Dock = DockStyle.Left;
        splitter.Width = 6;
    }
    else
    {
        control1.Dock = DockStyle.Top;
        splitter.Dock = DockStyle.Top;
        splitter.Height = 6;
        splitter.Cursor = Cursors.HSplit;
    }

    // Add splitter to the parent in the middle
    parent.Controls.Add(splitter);
    parent.Controls.SetChildIndex(splitter,1);
    parent.ResumeLayout();

    return splitter;
}

Usage

I will explain how you can create a Form with two Spitters as shown above. Create a Windows Form and a a ListBox and a Panel to it. Add another ListBox and TextBox and place them in the Panel.

Do not place any Spliters on the Form, they will be added using the AddSplitter() method. Modify the constructor of the Form.

public FormBoth()
{
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // Add Splitter
    //
    ControlUtil.AddSplitter(listBox1,panel1, true);
    ControlUtil.AddSplitter(listBox2,textBox1,false);
}

The vertical splitter is added between Listbox1 and the panel. The horizontal splitter is added between ListBox2 and TextBox1.

Conclusion

Splitters are great but not easy to use, the AddSplitter() method solves this.

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl

READ MORE

WinForm DualList Component

0 Comments
By Fons Sonnemans, 01-mrt-2002

Download DualList.zip

Introduction

This article demonstrates the use of the DualList component in a Microsoft .NET WinForm application. It doesn't explain how the component was written, you can figure that out by examining the C# source code (see download).

Problem Description

In many of my applications there are dialogs in which you have two listboxes and some buttons to move (or copy) item(s) from one listbox to the other. This seems very easy to implement, but it isn't. You have to implement a lot of features:

  • Actions: Move Selected, Copy Selected, Move All, Copy All
  • DoubleClick support, inclusive (re)setting the Default button
  • Select next item
  • Enable and disable buttons
  • MultiSelect support

You don't want to program this in every form which has 2 listboxes and some move or copy buttons. This would lead to lots of (duplicate) code which is very difficult to maintain. A generic (reusable) solution is required.


Class vs. Control vs. Component

There are three possible solutions: a class, a (composite) control and a component.

Class
Within your form you create an instances of this class and initialize it with an 'action' (move or copy and selected or all), 2 listboxes and 1 button. You need an instance for each button on your form. The class handles all events of the controls (e.g. Button.Click, ListBox.DoubleClick) and executes the requested action. The solution works fine but is not user-friendly. You have to do everything from code, you can not use the designer.

Composite control
This composite control would hold the 2 listboxes and 8 buttons. One button for each action in each direction. The user (programmer) can set the properties and handle events easily using the designer. It has the disadvantage that it is not very flexible. The layout of the composite controls will need lot's of special properties which not contribute to the problem description (low cohesion).

Component
The component is a "best of both worlds" solution. You can set it's properties using the designer and doesn't need any layout properties because it doesn't contain any controls.

DualList Component

The DualList component takes care of all the features mentioned in the problem domain. You can place it on the form as a sort of "invisible control". Next you set its behavior properties: Action, Button, ListBoxFrom and ListBoxTo. Optionally you can set the DoubleClickSupport and EnableDisableSupport properties.

The DualList handles all events of the controls (e.g. Button.Click, ListBox.DoubleClick) and executes the requested action.

+DualList : Component
{Implements: IComponent, IDisposable}
 
 DualList(in container : IContainer)
 DualList()
 CopyAll() : Void
 CopySelected() : Void
 MoveAll() : Void
 MoveSelected() : Void
 Action : DualListAction
 Button : Button
 DoubleClickSupport : Boolean
 AutoDisableButton : Boolean
 ListBoxFrom : ListBox
 ListBoxTo : ListBox
 AfterAction : AfterActionHandler
 BeforeAction : BeforeActionHandler

Usage

The following steps explain how you can use the DualList component in your WinForm projects.

Step1 - Download the Zip file and extract it.
Download DualList.zip and extract it to you local harddrive. This version was compiled using the final version of Microsoft .NET Framework SDK. The C# source of the DualList component is included.

Step2 - Design the Form
Create a form, place 2 listboxes and the required buttons on the form. The example only uses an Add and Remove button. These buttons move the selected items between the listboxes. Add some items to the listboxes.

You can off course set the Sorted property of the listboxes to True.

Step3 - Add the DualList component to the Toolbox
Right-click the Toolbox and select 'Customize Toolbox...'Select the '.NET Framework Components' Tab and click the 'Browse' button.

Open the 'ReflectionITWinFormComponents.dll' in the DualList\ComponentLibrary\Bin\Debug folder. The 'DualList' is checked and will be visible in the Toolbox. Click OK.

Step4 - Add a DualList to the form and set its properties
Drag two DualList components on your form.

Set the properties for both components

Step5 - Run the application

DualList events

The DualList component has 2 events. The BeforeAction event can be used to Cancel the action or to change the moved/copied Item. The AfterAction event can be used to process your changes (e.g. write them to your database, select the moved/copied item).

private void dualListAdd_AfterAction ( object sender , DualListActionEventArgs e)
{
    listAssigned.SelectedIndex = e.Index;
}

privatevoid dualListDelete_AfterAction(objectsender, DualListActionEventArgs e)
{
    listAvailable.SelectedIndex = e.Index;
}

Conclusion

The DualList Component is an easy solution for a difficult feature. It takes care of all logic needed to move and or copy items from one ListBox to another. It demonstrates the possibilities of .NET components. A very new, powerful feature of the .NET framework.

You can off course extend the class with Drag-and-drop support. That would really complete it. I might do it myself in the near future. Check this site for updates.

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl

READ MORE

ASP.NET RollOver Images

0 Comments
By Fons Sonnemans, 01-jan-2002

By Inge Schepers, Fons Sonnemans (Februari 2002)

Download RollOverImageDemo.zip

Introduction

A RollOver image is an image that changes its picture when the mouse moves over it.

Example:      Move your mouse over the image!

There are many ways to implement this. One easy way of doing this is creating a helper class called 'RollOverImageManager'. This helper class does two things: add the 'onMouseOver' and 'onMouseOut' attributes to the image; add a javascript which preloads the 'MouseOver' images.


using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

namespace RollOverImageDemo
{
    /// <summary>
    /// Utility class for an ASP.NET page that preloads images that
    /// are used for mouseovers.
    ///
    /// Be sure to notice that this code is provided as a technology sample,
    /// 'as is' and no warranties are made by the authors.
    ///
    /// For questions and comments: i.j.m.c.schepers@wxs.nl,
    ///                             Fons.Sonnemans@reflectionit.nl
    ///
    /// </summary>
    public class RollOverImageManager
    {
        private ArrayList mImages;

        public RollOverImageManager()
        {
            mImages=new ArrayList();
        }

        publicvoid AddImage(stringimageURL)
        {
            mImages.Add(imageURL);
        }

        publicvoid InitializeImage(Image control, stringimageURLMouseOver)
        {
            InitializeImage(control,imageURLMouseOver,control.ImageUrl);
        }

        publicvoid InitializeImage(Image control,
                                    stringimageURLMouseOver,
                                    stringimageURLMouseOut)
        {
            AddImage(imageURLMouseOver);

            AddAttribute(control.Attributes,"onMouseOut",imageURLMouseOut);
            AddAttribute(control.Attributes,"onMouseOver",imageURLMouseOver);

        }

        publicvoid InitializeImage(HtmlImage control, stringimageURLMouseOver)
        {
            InitializeImage(control,imageURLMouseOver,control.Src);
            }

        publicvoid InitializeImage(HtmlImage control,
                                    stringimageURLMouseOver,
                                    stringimageURLMouseOut)
        {
            AddImage(imageURLMouseOver);

            AddAttribute(control.Attributes,"onMouseOut",imageURLMouseOut);
            AddAttribute(control.Attributes,"onMouseOver",imageURLMouseOver);

        }

        publicvoid AddAttribute(AttributeCollection a,
                                 stringeventName,
                                 stringimageUrl)
        {
            try
            {
                a.Remove(eventName);
            }
            finally
            {
                a.Add(eventName,"this.src='" +imageUrl+"'");
            }
        }

        /// <summary>
        /// Preload the images
        /// </summary>
        publicvoid LoadImages(Page webPage)
        {
            // Create JavaScript
            StringBuilder buildString=new StringBuilder();
            buildString.Append("\n<SCRIPT LANGUAGE='JavaScript'>\n");
            buildString.Append("<!--\n");
            buildString.Append("var aImages = new Array();\n");
            for(int i =0; i < mImages.Count; i++)
            {
                buildString.Append("aImages[" +
                    i +"] = new Image();\n");
                buildString.Append("aImages[" +
                    i +"].src = '"+
                    mImages[i].ToString()+"';\n");
            }
            buildString.Append("//-->\n");
            buildString.Append("</script>\n");

            // Register Client Script
            if(webPage== null)
            {
                thrownew ArgumentException(
                    "The Page object is invalid.");
            }
            if(webPage.Request.Browser.JavaScript == true)
            {
                webPage.RegisterClientScriptBlock("ImageLoader",
                    buildString.ToString());
            }
        }

    }
}
The InitalizeImage() methods adds the 'onMouseOver' and 'onMouseOut' attributes to the control. The images 'onMouseOver' imageUrl is added to an ArrayList. This ArrayList is used by the LoadImages() method to generate a JavaScript which loads these images. This is done to prevent the extra load of the image the moment you move the mouse over it.

Usage

You use the RollOverImageManager in the Page_Load event of your web page. You create an instance of the class. Initialize the Image controls with the images and call the LoadImage() method.

privatevoid Page_Load(object sender, System.EventArgs e)
{
    // Create the Manager
    RollOverImageManager m =new RollOverImageManager();

    // Intialize the Image Controls
    m.InitializeImage(ImageButton1,"print2.gif");
    m.InitializeImage(Image1,"print2.gif");

    // Load the Images (generate the JavaScript)
    m.LoadImages(this);
}
WebForm1.aspx.cs

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="RollOverImageDemo.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <metaname="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <metaname="CODE_LANGUAGE" Content="C#">
    </HEAD>
    <body>
        <formid="WebForm1"method="post"runat="server">
            <asp:ImageButton id="ImageButton1"runat="server" ImageUrl="help.gif">
            </asp:ImageButton>
            <asp:Image id="Image1"runat="server" ImageUrl="help.gif">
            </asp:Image>
        </form>
    </body>
</HTML>
WebForm1.aspx

Conclusion

The RollOverImageManager is a simple helper class which lets you implement RollOver images easily. We have also experimented with an other solution. In the solution we subclassed the Image control and added an extra ImageUrlMouseOver property. This property was used to generate the OnMouseOver event. We did not use subclassing because it would lead to a lot of duplicate code unless delegation was used. Using a combination of subclassing and delegation would also take away from the simplicity of the solution, which is why we decided not to use it.

Tags: Web, ASP.NET

READ MORE

ASP.NET NumberBox Control

0 Comments
By Fons Sonnemans, 01-jan-2002

By Fons Sonnemans

Number: Download NumberBoxDemo.zip

The NumberBox control is an ASP.NET control, which lets the user only input numerical values. The base class of NumberBox class is the TextBox class. It uses JavaScript to validate the KeyPress event. An extra RegularExpresionValidator can be used to validate the control.

Special public properties:

  • AllowNegatives
  • DecimalPlaces
  • DecimalSymbol
  • Text
  • Value
  • ValidationRegularExpression

Demo

Number:     

Sample Project

NumberBox Class

using System;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;

namespace NumberBoxDemo
{
    /// <summary>
    /// Number Box ASP.NET control
    ///
    /// Created by Fons Sonnemans, Reflection IT
    ///
    /// Be sure to notice that this code is provided as a technology sample
    /// and 'as is', no warranties are made by the author.
    ///
    /// For questions and comments: Fons.Sonnemans@reflectionit.nl
    ///
    /// </summary>
    [
    ToolboxData("<{0}:NumberBox runat=server></{0}:NumberBox>"),
    DefaultProperty("DecimalPlaces")
    ]
    publicclass NumberBox : TextBox
    {

        privateintmDecimalPlaces=0;
        privatecharmDecimalSymbol='.';
        privateboolmAllowNegatives=true;

        /// <summary>
        /// Gets or sets the number of decimals for the number box.
        /// </summary>
        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(0),
        Description("Indicates the number of decimal places to display.")
        ]
        publicvirtualint DecimalPlaces
        {
            get{return mDecimalPlaces;}
            set{mDecimalPlaces=value;}
        }

        /// <summary>
        /// Gets or sets the digit grouping symbol for the number box.
        /// </summary>
        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue("."),
        Description("The digit grouping symbol.")
        ]
        publicvirtualchar DecimalSymbol
        {
            get{return mDecimalSymbol;}
            set{mDecimalSymbol=value;}
        }

        /// <summary>
        /// Gets or sets wheter negative numbers are allowed in the number box.
        /// </summary>
        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(true),
        Description("True when negative values are allowed")
        ]
        publicvirtualbool AllowNegatives
        {
            get{return mAllowNegatives;}
            set{mAllowNegatives=value;}
        }

        /// <summary>
        /// Gets or sets the value of the number box.
        /// </summary>
        publicvirtualdouble Value
        {
            get
            {
                try
                {
                    return ParseStringToDouble(this.Text);
                }
                catch(FormatException e)
                {
                    thrownew
                    InvalidOperationException("NumberBox does nog contain a valid Number.");
                }
                catch(Exception e)
                {
                    throw e;
                }

            }
            set
            {
                if((value < 0)&!AllowNegatives)
                    thrownew
                    ArgumentOutOfRangeException("
                    Only positivevaluesareallowedforthis NumberBox");

                //base.Text = value.ToString(this.Format);
                base.Text =value.ToString(GetFormat()).Replace(".", DecimalSymbol.ToString());
            }
        }

        /// <summary>
        /// Gets or sets the text content of the number box.
        /// </summary>
        overridepublicstring Text
        {
            get
            {
                returnbase.Text;
            }
            set
            {
                try{
                    this.Value = ParseStringToDouble(value);
                }catch (FormatException e) {
                    base.Text =value;
                }
                catch(Exception e)
                {
                    throw e;
                }
            }
        }


        /// <summary>
        ///    Add a JavaScript to the Page and call it from the onKeyPress event
        /// </summary>
        /// <param name="e"></param>
        overrideprotectedvoid OnPreRender(EventArgs e) {

            if(this.Page.Request.Browser.JavaScript ==true)
            {
                // Build JavaScript        
                StringBuilder s =new StringBuilder();
                s.Append("\n<script type='text/javascript' language='JavaScript'>\n");
                s.Append("<!--\n");
                s.Append("    function NumberBoxKeyPress(event, dp, dc, n) {\n");
                s.Append("         var myString = new String(event.srcElement.value);\n");
                s.Append("         var pntPos = myString.indexOf(String.fromCharCode(dc));\n");
                s.Append("         var keyChar = window.event.keyCode;\n");
                s.Append("     if ((keyChar < 48) || (keyChar > 57)) {\n");
                s.Append("         if (keyChar == dc) {\n");
                s.Append("             if ((pntPos != -1) || (dp < 1)) {\n");
                s.Append("                 return false;\n");
                s.Append("             }\n");
                s.Append("         } else \n");
                s.Append("if (((keyChar == 45) && (!n || myString.length != 0)) || (keyChar != 45)) \n");
                s.Append("             return false;\n");
                s.Append("     }\n");
                s.Append("     return true;\n");
                s.Append("    }\n");
                s.Append("// -->\n");
                s.Append("</script>\n");

                // Add the Script to the Page
                this.Page.RegisterClientScriptBlock("NumberBoxKeyPress", s.ToString());

                // Add KeyPress Event
                try{
                    this.Attributes.Remove("onKeyPress");
                }finally {
                    this.Attributes.Add("onKeyPress","return NumberBoxKeyPress(event, "
                                        + DecimalPlaces.ToString() +", "
                                        +((int)DecimalSymbol).ToString()+", "
                                        + AllowNegatives.ToString().ToLower() +")");
                }
            }
        }

        /// <summary>
        /// Returns the RegularExpression string which can be used for validating
        /// using a RegularExpressionValidator.
        /// </summary>
        virtualpublicstring ValidationRegularExpression
        {
            get
            {
                StringBuilder regexp= new StringBuilder();

                if(AllowNegatives)
                    regexp.Append("([-]|[0-9])");

                regexp.Append("[0-9]*");

                if(DecimalPlaces > 0)
                {
                    regexp.Append("([");
                    regexp.Append(DecimalSymbol);
                    regexp.Append("]|[0-9]){0,1}[0-9]{0,");
                    regexp.Append(DecimalPlaces.ToString());
                    regexp.Append("}$");
                }

                returnregexp.ToString();
            }
        }

        /// <summary>
        /// Parse a String to a Double
        /// </summary>
        /// <param name="s">string to be parsed to a double</param>
        /// <returns>double value</returns>
        virtualprotecteddouble ParseStringToDouble(string s)
        {
            s = s.Replace(DecimalSymbol.ToString(),".");
            returndouble.Parse(s);
        }

        /// <summary>
        /// Returns the FormatString used to display the value in the number box
        /// </summary>
        /// <returns>Format string</returns>
        virtualprotectedstring GetFormat()
        {
            StringBuilder f =new StringBuilder();
            f.Append("0");
            if(DecimalPlaces > 0)
            {
                f.Append(".");
                f.Append('0', DecimalPlaces);
            }

            return f.ToString();
        }

    }
}

Validation

You can use a normal RegularExpressionValidator control to validate the NumberBox. Invalid nummers are then rejected without causing a roundtrip to the server. Invalid numbers can still be entered using the clipboard Paste option. I don't know how to avoid this.

You can use the NumberBox.ValidationRegularExpression property to set the RegularExpressionValidator.ValidationRegularExpression property to the correct RegularExpression.

        private voidPage_Load(objectsender, System.EventArgs e)
        { 
            if(!IsPostBack)
            {
                valOne.ValidationExpression=numOne.ValidationRegularExpression;
                valTwo.ValidationExpression=numTwo.ValidationRegularExpression;
            }
        }


 

WebForm1.aspx

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="NumberBoxDemo.WebForm1"%>
<%@ Register TagPrefix="rit" Namespace="NumberBoxDemo" Assembly="NumberBoxDemo"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <metacontent="Microsoft Visual Studio 7.0"name="GENERATOR">
        <metacontent="C#"name="CODE_LANGUAGE">
        <metacontent="JavaScript (ECMAScript)"name="vs_defaultClientScript">
        <metacontent="http://schemas.microsoft.com/intellisense/ie5"name="vs_targetSchema">
    </HEAD>
    <body style="FONT-FAMILY: Verdana">
        <formid="Form1"method="post"runat="server">
            <H2>
                NumberBox Demo Page
            </H2>
            <P>
                <TABLE cellSpacing="1"cellPadding="1"width="100%"border="0">
                    <TR>
                        <TD style="WIDTH: 300px">
                            Numberbox1:
                        </TD>
                        <TD>
                            <rit:numberboxid="numOne"runat="server" DecimalSymbol=","
                             MaxLength="8" DecimalPlaces="2">
                                32,46
                            </rit:numberbox>
                        </TD>
                        <TD>
                            <asp:regularexpressionvalidatorid="valOne"runat="server"
                            ControlToValidate="numOne">
                            Invalid Number</asp:regularexpressionvalidator>
                        </TD>
                    </TR>
                    <TR>
                        <TD style="WIDTH: 300px"vAlign="top">
                            <FONT size="1">(Double, 2decimals,','&nbsp;DecimalSymbol)</FONT>
                        </TD>
                        <TD>
                        </TD>
                        <TD>
                        </TD>
                    </TR>
                    <TR>
                        <TD style="WIDTH: 300px">
                            &nbsp;
                        </TD>
                        <TD>
                        </TD>
                        <TD>
                        </TD>
                    </TR>
                    <TR>
                        <TD style="WIDTH: 300px">
                            Numberbox2:
                        </TD>
                        <TD>
                            <rit:numberboxid="numTwo"runat="server" MaxLength="8"
                             DecimalSymbol="." AllowNegatives="False">
                            </rit:numberbox>
                        </TD>
                        <TD>
                            <asp:RegularExpressionValidator id="valTwo"runat="server"
                            ControlToValidate="numTwo">
                            Invalid Number</asp:RegularExpressionValidator>
                        </TD>
                    </TR>
                    <TR>
                        <TD style="WIDTH: 300px"vAlign="top">
                            <FONT size="1">(Poisitive Integer)</FONT>
                        </TD>
                        <TD>
                        </TD>
                        <TD>
                        </TD>
                    </TR>
                    <TR>
                        <TD style="WIDTH: 300px">
                        </TD>
                        <TD>
                        </TD>
                        <TD>
                        </TD>
                    </TR>
                </TABLE>
            </P>
            <P>
            </P>
            <P>
                <asp:buttonid="btnSubmit"runat="server" Text="Submit"></asp:button>
            </P>
            <P>
                <asp:PlaceHolder id="PlaceHolder1"runat="server"></asp:PlaceHolder>
            </P>
            <P>
            </P>
        </form>
    </body>
</HTML>

WebForm1.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace NumberBoxDemo
{
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1: System.Web.UI.Page
    {
        protected NumberBoxDemo.NumberBox numOne;
        protected System.Web.UI.WebControls.RegularExpressionValidator valOne;
        protected NumberBoxDemo.NumberBox numTwo;
        protected System.Web.UI.WebControls.RegularExpressionValidator valTwo;
        protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
        protected System.Web.UI.WebControls.Button btnSubmit;

        public WebForm1()
        {
            Page.Init +=new System.EventHandler(Page_Init);
        }

        privatevoid Page_Load(objectsender, System.EventArgs e)
        {
            // Put user code to initialize the page here

            if(!IsPostBack)
            {
                valOne.ValidationExpression =numOne.ValidationRegularExpression;
                valTwo.ValidationExpression =numTwo.ValidationRegularExpression;
            }
        }

        privatevoid Page_Init(objectsender, EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
        }

        #region Web Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        privatevoid InitializeComponent()
        {
            this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
            this.Load +=new System.EventHandler(this.Page_Load);

        }
        #endregion

        privatevoidbtnSubmit_Click(object sender, System.EventArgs e)
        {
            numTwo.Value = new Random().NextDouble()*1000;

            this.PlaceHolder1.Controls.Clear();
            this.PlaceHolder1.Controls.Add(new LiteralControl(
             "Numberbox1.Value = "
             +numOne.Value.ToString()+"<br>"));
            this.PlaceHolder1.Controls.Add(new LiteralControl(
             "Numberbox1.Text = "
             +numOne.Text +"<br>"));
            this.PlaceHolder1.Controls.Add(new LiteralControl(
             "Numberbox1.ValidationRegularExpression = "
             +numOne.ValidationRegularExpression +"<br>"));
        }

    }
}

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl


Tags: Web, ASP.NET

READ MORE

Create Property Macro for VS.NET 2002 & 2003

0 Comments
By Fons Sonnemans, 01-jan-2002

By Fons Sonnemans

Download LocalizedText.zip

The CreateProperty macro is simple Visual Studio.Net (Final) macro which can be used to generate the code for a 'private Field and a public Property Get/Set method'.

Example:

1. Unzip the ReflectionIT macro.zip
 
2.  Start Visual Studio.Net.
 
3.  Open the Macro Explorer (Alt-F8)
 
4.  Right-click on the Macros, and select 'Load Macro Project...'
 
 
5.  Select the (unzipped) 'ReflectionIT.vsmacros' file and select 'Open'. 
6. Open a CodeWindow and add a public, private or protected field.

Example:

7. Select the text you just have entered and run the 'CreateProperty' macro

8. Your text is automatically replaced by a private field and an public property

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl


READ MORE

Stock Sample Web Service

0 Comments
By Fons Sonnemans, 01-jan-2002

By Fons Sonnemans

Download StockServices.zip

The Stock Sample Web Service uses the Html2Xml service to retrieve the Finance page of Yahoo. It then searches the Xml, using an XPath expression, for the current value and returns it. Hopefully Yahoo never changes their HTML structure.

VB.NET Source Code

Imports System.Web.Services
Imports System.Xml
Imports Html2XmlService <_o3a_p> 

Namespace ReflectionIT.WebServices.StockServices <_o3a_p />

<WebService(Namespace:="http://www.reflectionit.nl/webservices/", Description:="StockServices created by Reflection IT.")> _
Public Class StockServices <_o3a_p> 

Inherits System.Web.Services.WebService <_o3a_p> 

Public Sub New()
   MyBase.New()
   'This call is required by the Web Services Designer.
   InitializeComponent()
   'Add your own initialization code after the InitializeComponent() call 
End Sub <_o3a_p />

'Required by the Web Services Designer
Private components As System.ComponentModel.Container <_o3a_p> 

'NOTE: The following procedure is required by the Web Services Designer
'It can be modified using the Web Services Designer.
'Do not modify it using the code editor. <_o3a_p />

<System.Diagnostics.DebuggerStepThrough()>_
Private Sub InitializeComponent()
   components = New System.ComponentModel.Container()
End Sub <_o3a_p />

Protected Overloads Overrides Sub Dispose(ByVal disposing AsBoolean)
   'CODEGEN: This procedure is required by the Web Services Designer
  
'Do not modify it using the code editor.
End Sub <_o3a_p />

<WebMethod(Description:="Get the Yahoo stock quote.")> _
Public Function GetStockQuote(ByVal symbol AsString) AsDouble <_o3a_p />

   ' Build the Url
   Dim url AsString = "http://finance.yahoo.com/q?s=" + symbol + "&d=v1" <_o3a_p />

   ' Get the XmlDocument using the Html2Xml WebServices
  
Dim xml As XmlDocument = New XmlDocument()
  
xml.LoadXml(New Html2XmlService.Html2XmlServices().Url2XmlString(url)) <_o3a_p />

   ' Find the Nodes using a XPath expression
   Dim l As XmlNodeList = xml.SelectNodes("/html/body/center/table/tr/td/table/tr/td/b") <_o3a_p />

   ' Validate and return value
   If (l.Count <> 2) Then
      Throw New Exception("symbol not found.")
   End If
   Return Double.Parse(l(1).InnerXml) <_o3a_p />

End If <_o3a_p />

End Function <_o3a_p />

End Class <_o3a_p />

End Namespace

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl


Tags: Web, XML, ASP.NET

READ MORE

Visual Basic 6.0 Procedure Sort Add-In

0 Comments
By Fons Sonnemans, 01-jan-2002

By Fons Sonnemans

Download ProcedureSort.zip

The Procedures Sort Add-In is a great add-in for those who like well-organized code. You can use it to sort your procedures in the active CodeWindow alphabetically. It preserve all you procedure attributes, which you normally loose when you do it manually. Event-handlers and Interface-implementations will be placed at the end of your code.


Install & Usage:

1. Unzip the ProcedureSort.zip
 
2.  Register the ProcedureSort.dll you just unzipped using Regsvr32.exe.
 
3.  Start Visual Basic 6.0.
 
4.  Select menu 'Add-Ins', 'Add-in Manager...'.
 
 
5.  You see now an extra button in your Standard toolbar.

6. Open a CodeWindow. 

Example:

7. Click on the new 'Sort Procedures' toolbar button. All procedures will be sorted alphabetically. 

8. You can set the following options (Menu 'Add-Ins', 'Procedure Sort Options...'):
- Show Constructor and Destructor at top 
- Case Sensitive
- Number of empty lines between procedures


READ MORE

MS Outlook 2000 and MS Agent

0 Comments
By Fons Sonnemans, 01-jan-2002

By Fons Sonnemans

Download OutlookAgent.zip

You can use Microsoft Agent in Outlook 2000 to read your incoming mail.

Requirements:

  • Microsoft Outlook 2000
  • Microsoft Agent
  • Microsoft Agent Characters
  • Lernout & Hauspie TruVoice Text-to-Speech (TTS) Engine

For more information about installing MS Agent software have a look at: http://www.msagentring.org/setup.htm


10 Steps:

1. Install the requirements.
 
2.  Download this Zipfile and Unzip the CAgent.cls file to a folder (c:\temp).
 
3.  Select in 'Outlook 2000' the menu option 'Tools', 'Macro', 'Visual Basic Editor???' or press 'Alt-F11'.
 
4.  Select in the 'Microsoft Visual Basic' editor menu 'Tools', 'References???'.
   
 
5.  Check the checkboxes in front off 'Microsoft Agent Control 2.0' and 'Microsoft Scripting Runtime'.
 
6.  Open the 'File' menu and select the option 'Import File???'.

 
7. Select the CAgent.cls file and select Open.

 
The project explorer should look like:

 
8. Add the following code to the (existing) ThisOutlookSession.

 
9.  Save the project by clicking the 'Save' button in the toolbar.

 
10. Close de 'Microsoft Visual Basic' Editor.
MS Agent will now read your incoming e-mails. You can easily test it by sending a mail to yourself.


Tags: VB

READ MORE

All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.