Category Archives: Snippet

Insert Cash Snippet

Here is another one I created. Very much like the one for the date picker except this one is for money.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>SnippetFile1</Title>
      <Author>SheldonS</Author>
      <Description>Inserts controls with validators to enter cash amount into a textbox.</Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>ssic</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>txtCash</ID>
          <ToolTip>ID of the asp:TextBox control that will hold the cash value.</ToolTip>
          <Default>txtCash</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>valrCash</ID>
          <ToolTip>IDof the RequiredFieldValidator control.</ToolTip>
          <Default>valrCash</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>vaeRCash</ID>
          <ToolTip>ID of the AjaxControlToolkit ValidatorCalloutExtender control for the required field validator.</ToolTip>
          <Default>vaeRCash</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>valxCash</ID>
          <ToolTip>ID of the RegularExpressionValidator control.</ToolTip>
          <Default>valxCash</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>vceXCash</ID>
          <ToolTip>IDof the AjaxControlToolkit ValidatorCalloutExntender control for the regular expression validator.</ToolTip>
          <Default>vceXCash</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[<asp:TextBox ID="$txtCash$" runat="server" Width="50px" />
<asp:RequiredFieldValidator ID="$valrCash$" runat="server" ControlToValidate="$txtCash$" 
    Text="*" ErrorMessage="You must provide the Amount." SetFocusOnError="true" 
    Display="Dynamic">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="$vaeRCash$" runat="server" TargetControlID="$valrCash$">
</cc1:ValidatorCalloutExtender>
<asp:RegularExpressionValidator ID="$valxCash$" runat="server" ControlToValidate="$txtCash$" 
    Text="*" ErrorMessage="You must provide a valid entry for Amount. #.##" SetFocusOnError="true" 
    Display="Dynamic" ValidationExpression="^-?[0-9]{0,2}(\.[0-9]{1,2})?$|^-?(100)(\.[0]{1,2})?$">
</asp:RegularExpressionValidator>
<cc1:ValidatorCalloutExtender ID="$vceXCash$" runat="server" TargetControlID="$valxCash$">
</cc1:ValidatorCalloutExtender>]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Snippet Designer

I recently found out about this little extension for Visual Studio. You can find the Snippet Designer in the Visual Studio Gallery, or at CodePlex.

I am using the tool to recreate the date picker snippet I posted earlier. Channel9 also has a Visual Studio Toolbox video up about snippets.

A Date Picker Snippet

This is a snippet I created to insert the controls needed to create a date picker. I use the AjaxControl Toolkit’s Calendar Extender control to create a date picker. This also inserts a couple validation controls to make it required and to make sure it is a proper date.

Below is the source for my snippet.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Insert Date Picker</Title>
      <Shortcut>ssdtp</Shortcut>
      <Description>Code snippet to insert an asp:textbox that uses the AjaxControlToolkit's calendar extender, an asp:image control, and several validation controls.</Description>
      <Author>SheldonS</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>txtDate</ID>
          <ToolTip>ID of date text box control.</ToolTip>
          <Default>txtDate</Default>
        </Literal>
        <Literal>
          <ID>imgDate</ID>
          <ToolTip>ID of the image control.</ToolTip>
          <Default>imgDate</Default>
        </Literal>
        <Literal>
          <ID>valrDate</ID>
          <ToolTip>ID of the required field validation control.</ToolTip>
          <Default>valrDate</Default>
        </Literal>
        <Literal>
          <ID>caleDate</ID>
          <ToolTip>ID of the calendar extender control.</ToolTip>
          <Default>caleDate</Default>
        </Literal>
        <Literal>
          <ID>vceRDate</ID>
          <ToolTip>ID of the required field validator callout extender.</ToolTip>
          <Default>vceRDate</Default>
        </Literal>
        <Literal>
          <ID>valxDate</ID>
          <ToolTip>ID of the regular expression field validation control.</ToolTip>
          <Default>valxDate</Default>
        </Literal>
        <Literal>
          <ID>vceXDate</ID>
          <ToolTip>ID of the regular expression field validator callout extender.</ToolTip>
          <Default>vceXDate</Default>
        </Literal>
      </Declarations>
      <Code Language="html">
        <![CDATA[
        <asp:TextBox ID="$txtDate$" runat="server" Width="150" />
        <asp:Image ID="$imgDate$" runat="server" ImageUrl="~/Images/calendar.jpg" CssClass="clickable" />
        <cc1:CalendarExtender ID="$caleDate$" runat="server" TargetControlID="$txtDate$" PopupButtonID="$imgDate$" />
        <asp:RequiredFieldValidator ID="$valrDate$" runat="server" ControlToValidate="$txtDate$" 
            Text="*" ErrorMessage="You must provide a Date." SetFocusOnError="true" 
            Display="Dynamic">
        </asp:RequiredFieldValidator>
        <cc1:ValidatorCalloutExtender ID="$vceRDate$" runat="server" TargetControlID="$valrDate$" PopupPosition="Right" ViewStateMode="Enabled">
        </cc1:ValidatorCalloutExtender>
        <asp:RegularExpressionValidator ID="$valxDate$" runat="server" ControlToValidate="$txtDate$" 
            Text="*" ErrorMessage="You must enter a proper Date mm/dd/yyyy." SetFocusOnError="true" 
            Display="Dynamic" ValidationExpression="^(((((((0?[13578])|(1[02]))[\.\-/]?((0?[1-9])|([12]\d)|(3[01])))|(((0?[469])|(11))[\.\-/]?((0?[1-9])|([12]\d)|(30)))|((0?2)[\.\-/]?((0?[1-9])|(1\d)|(2[0-8]))))[\.\-/]?(((19)|(20))?([\d][\d]))))|((0?2)[\.\-/]?(29)[\.\-/]?(((19)|(20))?(([02468][048])|([13579][26])))))$">
        </asp:RegularExpressionValidator>
        <cc1:ValidatorCalloutExtender ID="$vceXDate$" runat="server" TargetControlID="$valxDate$" PopupPosition="Right" ViewStateMode="Enabled">
        </cc1:ValidatorCalloutExtender>
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

UPDATE: Here is the source for the insertdatepicker.snippet file as generated by Snippet Designer.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>InsertDatePicker</Title>
      <Author>SheldonS</Author>
      <Description>Creates a group of controls to act like a date picker with validator controls.  Uses controls from the AjaxControl Toolkit.</Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>ssdatepicker</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>txtDate</ID>
          <ToolTip>ID of the asp:TextBox control to hold the date.</ToolTip>
          <Default>txtDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>imgDate</ID>
          <ToolTip>IDof the asp:Image control of the calendar image.</ToolTip>
          <Default>imgDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>caleDate</ID>
          <ToolTip>IDof the AjaxControlToolkit CalendarExtender control.</ToolTip>
          <Default>caleDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>valrDate</ID>
          <ToolTip>ID of the asp:RequiredFieldValidator control.</ToolTip>
          <Default>valrDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>vceRDate</ID>
          <ToolTip>ID of the AjaxControlToolkit CalloutExtender for the RequiredFieldValidator control.</ToolTip>
          <Default>vceRDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>valxDate</ID>
          <ToolTip>IDof the asp:RegularExpressionValidator control.</ToolTip>
          <Default>valxDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>vceXDate</ID>
          <ToolTip>ID of the AjaxControlToolkit CalloutExtender for the asp:RegularExpressionValidator control.</ToolTip>
          <Default>vceXDate</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="html"><![CDATA[<asp:TextBox ID="$txtDate$" runat="server" Width="150" />
<asp:Image ID="$imgDate$" runat="server" ImageUrl="~/Images/calendar.jpg" CssClass="clickable" />
<cc1:CalendarExtender ID="$caleDate$" runat="server" TargetControlID="$txtDate$" PopupButtonID="$imgDate$" />
<asp:RequiredFieldValidator ID="$valrDate$" runat="server" ControlToValidate="$txtDate$"
    Text="*" ErrorMessage="You must provide a Date." SetFocusOnError="true" Display="Dynamic">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="$vceRDate$" runat="server" TargetControlID="$valrDate$"
    PopupPosition="Right" ViewStateMode="Enabled">
</cc1:ValidatorCalloutExtender>
<asp:RegularExpressionValidator ID="$valxDate$" runat="server" ControlToValidate="$txtDate$"
    Text="*" ErrorMessage="You must enter a proper Date mm/dd/yyyy." SetFocusOnError="true"
    Display="Dynamic" ValidationExpression="^(((((((0?[13578])|(1[02]))[\.\-/]?((0?[1-9])|([12]\d)|(3[01])))|(((0?[469])|(11))[\.\-/]?((0?[1-9])|([12]\d)|(30)))|((0?2)[\.\-/]?((0?[1-9])|(1\d)|(2[0-8]))))[\.\-/]?(((19)|(20))?([\d][\d]))))|((0?2)[\.\-/]?(29)[\.\-/]?(((19)|(20))?(([02468][048])|([13579][26])))))&">
</asp:RegularExpressionValidator>
<cc1:ValidatorCalloutExtender ID="$vceXDate$" runat="server" TargetControlID="$valxDate$"
    PopupPosition="Right" ViewStateMode="Enabled">
</cc1:ValidatorCalloutExtender>]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>