Thursday, December 8, 2011

WPF: Radial Panel, custom panel

Here is a cool 'custom panel' I've found that might be very handy...
The Joy Of Programming: A Simple Radial Panel for WPF and SilverLight

Usage examples
Nicer selection menu, Clock digits, dynamic elements in radial presentation rather than list...

Example outputs

source - http://www.codeproject.com/KB/WPF/SpiderControl.aspx

WPF: Bulletdecorator XAML example

BulletDecorator is basically like any editor bullet (like MS word's bullet). You can specify how to draw the bullet itself and decorate what ever you need.

Suppose we want to do the following effect in WPF:

  • This is a bullet
Here is the source for it:
Code Snippet
  1. <BulletDecorator>
  2.     <BulletDecorator.Bullet>
  3.         <Polygon Margin=" 2, 0, 0, 0" Points="0, 5 5, 0 10, 5 5, 10" Fill=" Blue" />
  4.     </BulletDecorator.Bullet>
  5.     <TextBlock Margin="10, 0, 0, 0" Text="This is a bullet" />
  6. </BulletDecorator>


And here is the results:



Source - Bulletdecorator in WPF | Controls | w3mentor

Wednesday, December 7, 2011

TUTORIAL: VS - Creating Code Snippets - CodeProject

I was in the middle of creating my experiance/article about the snippets subject when I got an email from http://www.codeproject.com/ with this wonderful article.

Extending Visual Studio Part 1 - Creating Code Snippets - CodeProject

I've already created some snippets, feel free to use them:

'Decorator-Pattern' snippet (activation: dp_decorator + TAB):
Code Snippet
  1. xml version="1.0" encoding="utf-8" ?>
  2. <CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3. <CodeSnippet Format="1.0.0">
  4. <Header>
  5. <Title>Decorator DPTitle>
  6. <Shortcut>dp_decoratorShortcut>
  7. <Description>Code snippet for decorator design patternDescription>
  8. <Author>S.OAuthor>
  9. <SnippetTypes>
  10. <SnippetType>ExpansionSnippetType>
  11. SnippetTypes>
  12. Header>
  13. <Snippet>
  14. <Declarations>
  15. <Literal>
  16. <ID>ComponentID>
  17. <ToolTip>Component nameToolTip>
  18. <Default>ComponentDefault>
  19. Literal>
  20. <Literal>
  21. <ID>DecoratorID>
  22. <ToolTip>Decorator nameToolTip>
  23. <Default>DecoratorDefault>
  24. Literal>
  25. Declarations>
  26. <Code Language="csharp">
  27. // Decorator patern:
  28. // http://en.wikipedia.org/wiki/Decorator_pattern#Structure
  29. // http://www.dofactory.com/Patterns/PatternDecorator.aspx
  30. abstract class $Component$
  31. {
  32. public abstract void Operation();
  33. }
  34. abstract class $Decorator$ : $Component$
  35. {
  36. protected $Component$ _$Component$;
  37. public void SetComponent($Component$ component)
  38. {
  39. this._$Component$ = component;
  40. }
  41. public override void Operation()
  42. {
  43. if (_$Component$ != null)
  44. {
  45. _$Component$.Operation();
  46. // + [your operation here]
  47. }
  48. }
  49. }
  50. $end$
  51. ]]>
  52. Code>
  53. Snippet>
  54. CodeSnippet>
  55. CodeSnippets>

The 'Event changed property' snippet (eprop + TAB):
Code Snippet
  1. xml version="1.0" encoding="utf-8" ?>
  2. <CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3. <CodeSnippet Format="1.0.0">
  4. <Header>
  5. <Title>event changed propertyTitle>
  6. <Shortcut>epropShortcut>
  7. <Description>Code snippet for eprop...Description>
  8. <Author>SOAuthor>
  9. <SnippetTypes>
  10. <SnippetType>ExpansionSnippetType>
  11. SnippetTypes>
  12. Header>
  13. <Snippet>
  14. <Declarations>
  15. <Literal Editable="true">
  16. <ID>prop_nameID>
  17. <ToolTip>property nameToolTip>
  18. <Function>PropName()Function>
  19. <Default>PropNameDefault>
  20. Literal>
  21. <Literal Editable="true">
  22. <ID>prop_typeID>
  23. <ToolTip>property typeToolTip>
  24. <Function>PropType()Function>
  25. <Default>ObjectDefault>
  26. Literal>
  27. Declarations>
  28. <Code Language="csharp">
  29. #region $prop_name$
  30. ///
  31. /// Occurs when $prop_name$ changes.
  32. ///
  33. public event EventHandler $prop_name$Changed;
  34. ///
  35. /// Raises the event.
  36. ///
  37. /// The instance containing the event data.
  38. protected virtual void On$prop_name$Changed(EventArgs e)
  39. {
  40. if ($prop_name$Changed != null)
  41. $prop_name$Changed(this, e);
  42. }
  43. private $prop_type$ _$prop_name$ = null;$end$
  44. ///
  45. /// Gets or sets the type of the region.
  46. ///
  47. ///
  48. /// The type of the region.
  49. ///
  50. public $prop_type$ $prop_name$
  51. {
  52. get { return _$prop_name$; }
  53. set
  54. {
  55. if (_$prop_name$ != value)
  56. {
  57. _$prop_name$ = value;
  58. On$prop_name$Changed(new EventArgs());
  59. }
  60. }
  61. }
  62. #endregion
  63. ]]>
  64. Code>
  65. Snippet>
  66. CodeSnippet>
  67. CodeSnippets>

And the 'TODO' snippet (TD + TAB), need some polish like auto date/time stamp, priority...:
Code Snippet
  1. xml version="1.0" encoding="utf-8" ?>
  2. <CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3. <CodeSnippet Format="1.0.0">
  4. <Header>
  5. <Title>TODOTitle>
  6. <Shortcut>TDShortcut>
  7. <Description>Code snippet for TODO...Description>
  8. <Author>SOAuthor>
  9. <SnippetTypes>
  10. <SnippetType>ExpansionSnippetType>
  11. SnippetTypes>
  12. Header>
  13. <Snippet>
  14. <Declarations>
  15. Declarations>
  16. <Code Language="csharp">// TODO:P, TimeStamp, S.O: $end$]]>
  17. Code>
  18. Snippet>
  19. CodeSnippet>
  20. CodeSnippets>

My advice: if you already created a code pattern twice (meaning - you copied and pasted a code and renamed some variables and function/property names), add it to your snippets, you'll need it and it will save time.

REMARK: Please follow the codeproject artical if you wish to install/use them...


Tuesday, December 6, 2011

TIP: Creating a file, fast, with incremental values (using FOR /L).

I needed to create a file (called urls.txt) that contains lots of image urls, here is the solution:


  1. Create a batch file (I'll call it gget.bat).
  2. copy and paste the following to gget.bat file.

REM ~~~~~ gget.bat file content ~~~~~~~~~~~~~~~

@echo off
for /L %%a in (0,1,1984) do (
echo http://domain.com/images/%%a.jpg >>c:\urls.txt
)

ECHO DONE!
pause
REM ~~~~~ gget.bat file content ~~~~~~~~~~~~~~~


  1. change the numbers in "(0,1,1984)" to the desired ones (please read more about 'FOR /L' here.).
the result is a file 'c:\urls.txt' that contains this:

http://domain.com/images//0.jpg 
http://domain.com/images//1.jpg 
http://domain.com/images//2.jpg 
http://domain.com/images//3.jpg 
...
http://domain.com/images//1983.jpg 
http://domain.com/images//1984.jpg 

Hope it helps...

Monday, December 5, 2011

TIP - How to check an errorlevel of a program...

First understand a little about error levels... Batch files - Errorlevels:

Next... Write this code the a batch file and run it (change the runme.exe to the program that you wish to check the error-level).
#~~~~~~~~~~~~~~~~~~~~~~~~~
runme.exe
ECHO.%ERRORLEVEL%
PAUSE
#~~~~~~~~~~~~~~~~~~~~~~~~~

Hope it helps...

Tuesday, November 15, 2011

Example: C# dynamic 'Enum' like solution.

Example usage:


Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace GuiLib.TeraRegion
  5. {
  6.     public class TeraRegionModel
  7.     {
  8.  
  9.         /// <summary>
  10.         /// Dynamic enum of RegionTypes.
  11.         /// <remarks>* This is a [Flags] and normal mixed enum!
  12.         /// * 0 is reserved! it's not used!
  13.         /// * 1 is reserved! it's void.
  14.         /// Find it's max and assigne it's next value like this:
  15.         ///     var next_enum_val = RegionTypes.Values.Max<ulong>();
  16.         ///     RegionTypes.Add("new_item", next_enum_val*2);
  17.         /// </remarks>
  18.         /// </summary>
  19.         public static Dictionary<string, ulong> RegionTypes = new Dictionary<string, ulong>()
  20.         {
  21.             {"void", 1}
  22.         };
  23.  
  24.  
  25.         #region RegionType
  26.  
  27.         /// <summary>
  28.         /// Occurs when RegionType changes.
  29.         /// </summary>
  30.         public event EventHandler RegionTypeChanged;
  31.  
  32.         /// <summary>
  33.         /// Raises the <see cref="E:RegionTypeChanged"/> event.
  34.         /// </summary>
  35.         /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  36.         protected virtual void OnRegionTypeChanged(EventArgs e)
  37.         {
  38.             if (RegionTypeChanged != null)
  39.                 RegionTypeChanged(this, e);
  40.         }
  41.  
  42.         private ulong _RegionType = 1;
  43.  
  44.  
  45.         /// <summary>
  46.         /// Gets or sets the type of the region.
  47.         /// </summary>
  48.         /// <value>
  49.         /// The type of the region.
  50.         /// </value>
  51.         public ulong RegionType
  52.         {
  53.             get
  54.             {
  55.                 return _RegionType;
  56.             }
  57.  
  58.             set
  59.             {
  60.                 if (_RegionType != value)
  61.                 {
  62.                     _RegionType = value;
  63.  
  64.                     OnRegionTypeChanged(new EventArgs());
  65.                 }
  66.             }
  67.         }
  68.         #endregion
  69.  
  70.     }
  71. }

Monday, November 14, 2011

Tip: C# dynamic 'Enum' like solution.

Hi,
This is very usefull if you wish to be able to create dynamic enums.

Motivation
* You need to enumerate something but wish to add items to it in runtime (e.g. your server should support v1 protocol and v2 protocol where v2 protocol extends v1.enum).

* You wish to extand this enum dynamically by an outside addin.

Solution and prof



Code Snippet
  1. enum MyEnum : ulong
  2. {
  3.     // NOTICE: string_name = ulong_value,
  4.     first = 2<<0,
  5.     second = 2<<1,
  6.     last = 2<<2,
  7.  
  8.     max = 2 << 63
  9.     // dynamic string name = dynamic ulong value!
  10. }
  11.  
  12. Dictionary<string, ulong> DynamicEnum1 = new Dictionary<string, ulong>()
  13. {
  14.     {"first", 2 << 0},
  15.     {"second", 2 << 1},
  16.     {"last", 2 << 2},
  17.  
  18.     {"max", 2 << 63}
  19. };
  20.  
  21. public void usage()
  22. {
  23.     MyEnum qwe = MyEnum.first;
  24.  
  25.     DynamicEnum1.Add("another_one", 2 << 3);
  26.     UInt64 qwe2 = DynamicEnum1["first"];
  27.  
  28.     // (UInt64)qwe == qwe2!!
  29. }


Hope it helps.