Results 1 to 2 of 2

Thread: Custom Control: Organizing Properties

  1. #1

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551

    Custom Control: Organizing Properties

    Hello,

    I'm creating a custom control, which will require a very long list of different properties for it...

    Now, to keep things organized and easy for both me and the user using this control, i would like to sort of try and group these properties into sub tags in my control's tag...

    these are my properties:

    VB Code:
    1. Table
    2.     Display
    3.         BorderWidth     '//Width of border around whole table
    4.         Width           '//Width
    5.         Height          '//Height
    6.  
    7.     Colors
    8.         BorderColor     '//Color of border around whole table
    9.         BGColor         '//Background Color of whole table
    10.        
    11.  
    12.  
    13. Header
    14.     Display
    15.         ShowHeader      '//Determines if header is shown
    16.         BorderWidth     '//Width of Border of table around whole header
    17.         ShowNextButton      '//Show a Next Month Button
    18.         ShowPrevButton      '//Show a Previous Month Button
    19.         ShowMonthDropDown   '//Show the DropDownList for months (false shows a plain text month on)
    20.         NextButtonText      '//Caption of button for Next Month
    21.         PrevButtonText      '//Caption of button for Previous Month
    22.         Width           '//Width
    23.         Height          '//Height
    24.            
    25.    
    26.     Colors
    27.         BorderColor     '//Border Color of entire table around header
    28.         BGColor         '//Background Color of entire header table
    29.  
    30.     Fonts
    31.         Face            '//Font Face
    32.         Size            '//Font Size
    33.         Color           '//Font Color
    34.         Align           '//Alignment of Font
    35.         VAlign          '//Verticle Alignment of Font
    36.         Bold            '//Font Bold
    37.         Italic          '//Font Italic
    38.         Underline       '//Font Underline
    39.  
    40.  
    41. DaysOfWeek
    42.     Display
    43.         ShowDaysOfWeek      '//Shows the days above the columns
    44.         ShowDayNumbers      '//Shows the day number
    45.         StartingDayOfWeek   '//Number 1 is sunday, 7 is saturday for which the week starts on
    46.         BorderWidth     '//Width of Border around whole table
    47.         CellBorderWidth     '//Width of border of each individual cell
    48.        
    49.        
    50.     Colors
    51.         BorderColor     '//Color of border around whole DaysOfWeekTable
    52.         BGColor         '//Background of entire daysofweek table
    53.         CellBorderColor     '//Color of border around each cell
    54.         CellBGColor     '//Background color of each cell
    55.        
    56.     Fonts
    57.         Face            '//Font Face
    58.         Size            '//Font Size
    59.         Color           '//Font Color
    60.         Align           '//Font Alignment
    61.         VAlign          '//Font Verticle Alignment
    62.         Bold            '//Font Bold
    63.         Italic          '//Font Italic
    64.         Underline       '//Font Underline
    65.  
    66.  
    67. Calender
    68.     Display
    69.         ShowDayNumbers      '//Shows the number
    70.         EventsPerDay        '//Number of events shown in a cell before it links to more... 
    71.         BorderWidth     '//Width of cell's border
    72.         CellWidth       '//Width of Cell
    73.         CellHeight      '//Height of Cell
    74.        
    75.  
    76.     Colors
    77.         BorderColor     '//Cell's Border Color
    78.         BGColor         '//Cell's Background Color
    79.  
    80.     DayNumberFont
    81.         Face            '//Font Face
    82.         Size            '//Font Size
    83.         Color           '//Font Color
    84.         Align           '//Font Alignment
    85.         VAlign          '//Font Verticle Alignment
    86.         Bold            '//Font Bold
    87.         Italic          '//Font Italic
    88.         Underline       '//Font Underline
    89.    
    90.     EventFont
    91.         Face            '//Font Face
    92.         Size            '//Font Size
    93.         Color           '//Font Color
    94.         Align           '//Font Alignment
    95.         VAlign          '//Font Verticle Alignment
    96.         Bold            '//Font Bold
    97.         Italic          '//Font Italic
    98.         Underline       '//Font Underline
    99.  
    100.  
    101. Footer
    102.     Display
    103.         ShowHeader      '//Determines if header is shown
    104.         BorderWidth     '//Width of Border of table around whole header
    105.         ShowNextButton      '//Show a Next Month Button
    106.         ShowPrevButton      '//Show a Previous Month Button
    107.         ShowMonthDropDown   '//Show the DropDownList for months (false shows a plain text month on)
    108.         NextButtonText      '//Caption of button for Next Month
    109.         PrevButtonText      '//Caption of button for Previous Month
    110.         Width           '//Width
    111.         Height          '//Height
    112.            
    113.    
    114.     Colors
    115.         BorderColor     '//Border Color of entire table around header
    116.         BGColor         '//Background Color of entire header table
    117.  
    118.     Fonts
    119.         Face            '//Font Face
    120.         Size            '//Font Size
    121.         Color           '//Font Color
    122.         Align           '//Alignment of Font
    123.         VAlign          '//Verticle Alignment of Font
    124.         Bold            '//Font Bold
    125.         Italic          '//Font Italic
    126.         Underline       '//Font Underline

    now, as you can see, the list is quite lengthy, and i've already divided them into sort of subsections....

    this is sort of how i'd like to allow the user to use the control:

    VB Code:
    1. <MyControl:Calender Runat="Server">
    2.     <MyControl:CalenderTable BorderWidth="1" BorderColor="#000000" />
    3.     <MyControl:CalenderHeader properties here... />
    4.     <MyControl:CalenderDaysOfWeek properties here... />
    5.     <MyControl:CalenderDays properties here... />
    6.     <MyControl:CalenderFooter properties here... />
    7. </MyControl:Calender>

    Not sure if you understand what i mean with this example, but i'm just trying to organize things easier so the user using this control can easily set properties without having them all in the <MyControl:Calender Runat="Server" /> Tag, and thus forcing huge property names...

    I'm more than open to any suggestions of other means of setting this up.. this was just what i came up with, by no means is it what i am stuck on going with...

    i appreciate the input!

  2. #2
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Got enuf properties there ???

    Anyway....you need to create a ControlBuilder and override AddParsedSubObject to handle nested controls like that. I just got into that part of making controls, so the following might not be the most efficent or complex, but it does work. I change some of the property names so you could follow it better.

    Code:
    using System;
    using System.Collections;
    using System.Web;
    using System.Web.UI;
    
    namespace JWM.WebControls
    {
                     public class CustomControlBuilder : ControlBuilder 
    	{
    
    		public override Type GetChildControlType(String tagName, IDictionary attributes) 
    		{
    			if (String.Compare(tagName, "CalendarTable", true) == 0) 
    			{
    				return typeof(JWM.WebControls.CalTable);
    			}
    			if (String.Compare(tagName, "CalendarFooter", true) == 0) 
    			{
    				return typeof(JWM.WebControls.CalFooter);
    			}
    			return null;
    		}
    	}
    
    	[ControlBuilderAttribute(typeof(CustomControlBuilder))]
    	public class CB : Control 
    	{
    
    		private ArrayList _items         = new ArrayList();
    		private JWM.WebControls.CalTable ct = new CalTable();
    		private JWM.WebControls.CalFooter cf = new CalFooter(); 
    
    		protected override void AddParsedSubObject(Object obj) 
    		{
    			if (obj is JWM.WebControls.CalTable) 
    			{
    				ct.BorderColor = ((JWM.WebControls.CalTable)obj).BorderColor;
    				ct.BorderWidth = ((JWM.WebControls.CalTable)obj).BorderWidth;
    			}
    			if (obj is JWM.WebControls.CalFooter) 
    			{
    				cf.ShowNextButton = ((JWM.WebControls.CalFooter)obj).ShowNextButton;
    			}
    		}
    
    		protected override void Render(HtmlTextWriter output) 
    		{
    			output.Write("Border Color = " + ct.BorderColor + "<br>");
    			output.Write("Border Width = " + ct.BorderWidth + "<br>");
    			output.Write("ShowNextButton = " + cf.ShowNextButton + "<br>");
    		}
    	}    
    	public class CalTable : Control 
    	{
    		private Int32 _borderWidth;
    		private String _borderColor;
    
    		public Int32 BorderWidth 
    		{
    			get 
    			{
    				return _borderWidth;
    			}
    			set 
    			{
    				_borderWidth = value;
    			}
    		}
    		public String BorderColor
    		{
    			get 
    			{
    				return _borderColor;
    			}
    			set 
    			{
    				_borderColor = value;
    			}
    		}
    	}
    	public class CalFooter : Control 
    	{
    		private bool _showNextButton;
    
    		public bool ShowNextButton 
    		{
    			get 
    			{
    				return _showNextButton;
    			}
    			set 
    			{
    				_showNextButton = value;
    			}
    		}
    	}
    }
    Place that in a WebControl....compile it and add a WebApp to the project. The add the control to the Page and change the HTML to:

    Code:
    <cc1:CB id="CB1" runat="server">
        <CalendarTable BorderWidth="5" BorderColor="Pink"/>
        <CalendarFooter ShowNextButton="true" />
    </cc1:CB>
    The render part of the control is very simple. It just outputs the properties you set. The cc1 part might need to be changed.

    HTH
    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width