home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Edit List and Spin / EnumerateEnumeration / EnumerateEnumeration.cs next >
Encoding:
Text File  |  2001-01-15  |  7.6 KB  |  210 lines

  1. //---------------------------------------------------
  2. // EnumerateEnumeration.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Reflection;           // For the Assembly class
  7. using System.Text;                 // For the StringBuilder class
  8. using System.Windows.Forms;
  9.  
  10. class EnumerateEnumeration: Form
  11. {
  12.      Button   button;
  13.      TextBox  tbLibrary, tbNamespace, tbEnumeration, tbOutput;
  14.      CheckBox cbHex;
  15.  
  16.      public static void Main()
  17.      {
  18.           Application.Run(new EnumerateEnumeration());
  19.      }
  20.      public EnumerateEnumeration()
  21.      {
  22.           Text = "Enumerate Enumeration";
  23.           ClientSize = new Size(242, 164);
  24.  
  25.           Label label    = new Label();
  26.           label.Parent   = this;
  27.           label.Text     = "Library:";
  28.           label.Location = new Point(8, 8);
  29.           label.Size     = new Size(56, 8);
  30.  
  31.           tbLibrary          = new TextBox();
  32.           tbLibrary.Parent   = this;
  33.           tbLibrary.Text     = "system.windows.forms";
  34.           tbLibrary.Location = new Point(64, 8);
  35.           tbLibrary.Size     = new Size(120, 12);
  36.           tbLibrary.Anchor  |= AnchorStyles.Right;
  37.  
  38.           ToolTip tooltip = new ToolTip();
  39.           tooltip.SetToolTip(tbLibrary, 
  40.                                    "Enter the name of a .NET dynamic\n" +
  41.                                    "link libary, such as 'mscorlib',\n" + 
  42.                                    "'system.windows.forms', or\n" +
  43.                                    "'system.drawing'.");
  44.  
  45.           label          = new Label();
  46.           label.Parent   = this;
  47.           label.Text     = "Namespace:";
  48.           label.Location = new Point(8, 24);
  49.           label.Size     = new Size(56, 8);
  50.  
  51.           tbNamespace          = new TextBox();
  52.           tbNamespace.Parent   = this;
  53.           tbNamespace.Text     = "System.Windows.Forms";
  54.           tbNamespace.Location = new Point(64, 24);
  55.           tbNamespace.Size     = new Size(120, 12);
  56.           tbNamespace.Anchor  |= AnchorStyles.Right;
  57.  
  58.           tooltip.SetToolTip(tbNamespace, 
  59.                                    "Enter the name of a namespace\n" +
  60.                                    "within the library, such as\n" +
  61.                                    "'System', 'System.IO',\n" +
  62.                                    "'System.Drawing',\n" +
  63.                                    "'System.Drawing.Drawing2D',\n" +
  64.                                    "or 'System.Windows.Forms'.");
  65.                          
  66.           label          = new Label();
  67.           label.Parent   = this;
  68.           label.Text     = "Enumeration:";
  69.           label.Location = new Point(8, 40);
  70.           label.Size     = new Size(56, 8);
  71.  
  72.           tbEnumeration          = new TextBox();
  73.           tbEnumeration.Parent   = this;
  74.           tbEnumeration.Text     = "ScrollBars";
  75.           tbEnumeration.Location = new Point(64, 40);
  76.           tbEnumeration.Size     = new Size(120, 12);
  77.           tbEnumeration.Anchor  |= AnchorStyles.Right;
  78.  
  79.           tooltip.SetToolTip(tbEnumeration, 
  80.                                    "Enter the name of an enumeration\n" +
  81.                                    "defined in the namespace.");
  82.  
  83.           cbHex                 = new CheckBox();
  84.           cbHex.Parent          = this;
  85.           cbHex.Text            = "Hex";
  86.           cbHex.Location        = new Point(192, 16);
  87.           cbHex.Size            = new Size(40, 8);
  88.           cbHex.Anchor          = AnchorStyles.Top | AnchorStyles.Right;
  89.           cbHex.CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged);
  90.  
  91.           tooltip.SetToolTip(cbHex, "Check this box to display the\n" +
  92.                                     "enumeration values in hexadecimal.");
  93.  
  94.           button          = new Button();
  95.           button.Parent   = this;
  96.           button.Text     = "OK";
  97.           button.Location = new Point(192, 32);
  98.           button.Size     = new Size(40, 16);
  99.           button.Anchor   = AnchorStyles.Top | AnchorStyles.Right;
  100.           button.Click   += new EventHandler(ButtonOkOnClick);
  101.  
  102.           AcceptButton = button;
  103.  
  104.           tooltip.SetToolTip(button, 
  105.                                    "Click this button to display results.");
  106.           
  107.           tbOutput            = new TextBox();
  108.           tbOutput.Parent     = this;
  109.           tbOutput.ReadOnly   = true;
  110.           tbOutput.Multiline  = true;
  111.           tbOutput.ScrollBars = ScrollBars.Vertical;
  112.           tbOutput.Location   = new Point(8, 56);
  113.           tbOutput.Size       = new Size(226, 100);
  114.           tbOutput.Anchor     = AnchorStyles.Left | AnchorStyles.Top |
  115.                                 AnchorStyles.Right | AnchorStyles.Bottom;
  116.  
  117.           AutoScaleBaseSize = new Size(4, 8);
  118.  
  119.                // Initialize the display.
  120.  
  121.           ButtonOkOnClick(button, EventArgs.Empty);
  122.      }
  123.      void CheckBoxOnCheckedChanged(object sender, EventArgs ea)
  124.      {
  125.           button.PerformClick();
  126.      }
  127.      void ButtonOkOnClick(object sender, EventArgs ea)
  128.      {
  129.           FillTextBox(tbOutput, tbLibrary.Text, tbNamespace.Text, 
  130.                       tbEnumeration.Text, cbHex.Checked);
  131.      }
  132.      public static bool FillTextBox(TextBox tbOutput, string strLibrary,
  133.                                     string strNamespace, 
  134.                                     string strEnumeration, bool bHexadecimal)
  135.      {
  136.           string strEnumText = strNamespace + "." + strEnumeration;
  137.            string strAssembly;
  138.          
  139.           try
  140.           {
  141.                 strAssembly = 
  142.                    Assembly.LoadWithPartialName(strLibrary).FullName;
  143.           }
  144.           catch
  145.           {
  146.               return false;
  147.           }
  148.           string strFullText = strEnumText + "," + strAssembly;
  149.  
  150.                // Get the type of the enum.
  151.  
  152.           Type type = Type.GetType(strFullText, false, true);
  153.  
  154.           if(type == null) 
  155.           {
  156.                tbOutput.Text = "\"" + strFullText + 
  157.                                "\" is not a valid type.";
  158.                return false;
  159.           }
  160.           else if(!type.IsEnum)
  161.           {
  162.                tbOutput.Text = "\"" + strEnumText + 
  163.                                "\" is a valid type but not an enum.";
  164.                return false;
  165.           }
  166.  
  167.                // Get all the members in that enum.
  168.  
  169.           string[] astrMembers = Enum.GetNames(type);
  170.           Array    arr         = Enum.GetValues(type);
  171.           object[] aobjMembers = new object[arr.Length];
  172.  
  173.           arr.CopyTo(aobjMembers, 0);
  174.  
  175.                // Create a StringBuilder for the text.
  176.  
  177.           StringBuilder sb = new StringBuilder();
  178.  
  179.                // Append the enumeration name and headings.
  180.  
  181.           sb.Append(strEnumeration);
  182.           sb.Append(" Enumeration\r\nMember\tValue\r\n");
  183.  
  184.                // Append the text rendition and the actual numeric values.
  185.  
  186.           for (int i = 0; i < astrMembers.Length; i++)
  187.           {
  188.                sb.Append(astrMembers[i]);
  189.                sb.Append("\t");
  190.  
  191.                if (bHexadecimal)
  192.                     sb.Append("0x" + Enum.Format(type, aobjMembers[i], "X"));
  193.                else
  194.                     sb.Append(Enum.Format(type, aobjMembers[i], "D"));
  195.  
  196.                sb.Append("\r\n");
  197.           }
  198.                // Append some other information.
  199.  
  200.           sb.Append("\r\nTotal = " + astrMembers.Length + "\r\n");
  201.           sb.Append("\r\n" + type.AssemblyQualifiedName + "\r\n");
  202.  
  203.                // Set the text box Text property from the StringBuilder.
  204.  
  205.           tbOutput.Text = sb.ToString();
  206.           tbOutput.SelectionLength = 0;
  207.           return true;
  208.      }
  209. }
  210.