Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Use C# write a designer like PowerDesigner

2.72/5 (14 votes)
14 Aug 2006 1   643  
How to develop a database structure designer use C#

Sample Image - xdbdesigner.png

Introduction

XDBDesigner is a WYSWYG  designer which can design database structure , just lick PowerDesigner , and it can load PDM file which created by PowerDesigner . It can save design result to a XML file and load design from a XML file . It is a appliction of XDesignerLib .

Using the code

XDBDesigner is a application of XDesignerLib , XDesignerLib is a middle ware which use to create WYSWYG designer easy and fast . XDesignerLib already bring a set of design DOOM ,  people can extend design DOM to create own designer . For example , in XDBDesigner , it create a tablefield element by the following C# code : 

public class DesignDBFieldElement : XDesignerDom.DesignLabelElement 
{
    public static System.Drawing.SolidBrush BackBrush = 
          new System.Drawing.SolidBrush( System.Drawing.Color.FromArgb( 0xff,0xA9,0xD2,0xFE)) ;

    public override bool RefreshView(DesignPaintEventArgs e)
    {
        if( myOwnerDocument.RuntimeScaleRate < 0.1 )
            return true;
        int index = myOwnerDocument.GetImageIndex( this );
        if( index >= 0 )
            this.Icon = myOwnerDocument.ImageList[ index ] ;
        if( this.LinkHightlight )
            e.FillDrawRects(  System.Drawing.SystemBrushes.Highlight , e.ViewBounds );
        else if( this.IsCurrent )
            e.FillDrawRects( System.Drawing.SystemBrushes.Info , e.ViewBounds );
        base.CustomDraw( e );
        return true;
    }
    public override System.Drawing.Color TextColor
    {
        get
        {
            if( this.LinkHightlight )
                return System.Drawing.SystemColors.HighlightText ;
            else
            {
                if( "new".Equals( Tag ))
                    return System.Drawing.Color.Blue ;
                else if( "delete".Equals ( Tag ))
                    return System.Drawing.Color.Red ;
                else
                    return System.Drawing.SystemColors.WindowText ;
            }
        }
        set
        {
            base.TextColor = value;
        }
    }
    public override bool LoadProperties(XDesignerProperty.IPropertyPackage Loader)
    {
        if( Loader.HasModified("customproperties")) 
            this.CustomProperties.ParseString( Loader.GetString("customproperties"));
        if( Loader.HasModified("convertlist")) 
            this.ConvertList.ParseString( Loader.GetString("convertlist"));
        if( Loader.HasModified("falsevalue")) 
            this.FalseValue = Loader.GetString("falsevalue");
        if( Loader.HasModified("truevalue")) 
            this.TrueValue = Loader.GetString("truevalue");
        if( Loader.HasModified("isboolean")) 
            this.IsBoolean = Loader.GetBoolean("isboolean" , false);
        if( Loader.HasModified("defaultvalue")) 
            this.DefaultValue = Loader.GetString("defaultvalue") ;
        if( Loader.HasModified("hasdefaultvalue")) 
            this.HasDefaultValue = Loader.GetBoolean("hasdefaultvalue" , false );
        if( Loader.HasModified("fformat")) 
            this.FFormat = Loader.GetString("fformat");
        if( Loader.HasModified("nullable")) 
            this.Nullable = Loader.GetBoolean("nullable" , true );
        if( Loader.HasModified("indexed")) 
            this.Indexed = Loader.GetBoolean("indexed" , false );
        if( Loader.HasModified("key")) 
            this.PrimaryKey = Loader.GetBoolean("key" , false );
        if( Loader.HasModified("fwidth")) this.FWidth = Loader.GetString("fwidth" ) ;
        if( Loader.HasModified("ftype")) this.FType = Loader.GetString("ftype");
        if( Loader.HasModified("remark")) this.Remark = Loader.GetString("remark");
        if( Loader.HasModified("desc")) this.Desc = Loader.GetString("desc");
        if( Loader.HasModified("fname") ) this.FName = Loader.GetString("fname");
        
        if( Loader.FromUI )
        {
            if( (( DesignDBTableElement ) myParent).RefreshPreferredWidth())
            {
                myParent.Resize();
                myParent.Invalidate();
            }
            if( this.PrimaryKey )
                ( ( DesignDBTableElement ) myParent ).KeyField = this.FName ;
            myOwnerDocument.RaiseChangeElement( this );
        }
        return true;
    }

    public override bool SaveProperties(XDesignerProperty.IPropertyPackage Saver)
    {
        Saver.SetString("convertlist", ConvertList.ToString());
        Saver.SetString("customproperties" , this.CustomProperties.ToString());
        Saver.SetString("falsevalue" , this.FalseValue );
        Saver.SetString("truevalue" , this.TrueValue );
        Saver.SetBoolean("isboolean" , this.IsBoolean , false );
        Saver.SetString("defaultvalue" , this.DefaultValue );
        Saver.SetBoolean("hasdefaultvalue" , this.HasDefaultValue , false );
        Saver.SetString("fformat" , this.FFormat );
        Saver.SetBoolean("indexed" , this.Indexed , false );
        Saver.SetBoolean("key" ,this.PrimaryKey , false );
        Saver.SetBoolean("nullable" , this.Nullable , true);
        Saver.SetString("fwidth" , this.FWidth );
        Saver.SetString("ftype" , this.FType );
        Saver.SetString("remark" , this.Remark );
        Saver.SetString("desc" , this.Desc );
        Saver.SetString("fname" , this.FName );
        if( myOwnerDocument.ForXSLTFlag )
        {
            Saver.SetString("isstring" , IsStringField() ? "1" : "0");
            Saver.SetString("isinteger" , IsIntegerField() ? "1" : "0");
            Saver.SetString("isfloat" , IsFloatField() ? "1" : "0" );
            Saver.SetString("isbyte" , this.IsByteField() ? "1" : "0");
            int index = myParent.Items.IndexOf( this );
            Saver.SetString("fixindex" ,
                index > 9 ? index.ToString() : index.ToString() + " " );
        }
        if( Saver is PropertyListItemCollection )
        {
            PropertyListItemCollection items = ( PropertyListItemCollection ) Saver ;
            PropertyStringItem item = ( PropertyStringItem ) items["fformat"];
            item.ListItems = new string[] {"String",
                          "Integer",
                          "Decimal",
                          "Double",
                          "DateTime",
                          "Base64",
                          "YYYYMMDDHHMMSS",
                          "YYYYMMDDHHMM",
                          "YYYYMMDDHH",
                          "YYYYMMDD",
                          "HHMMSS"};
            item = ( PropertyStringItem ) items["ftype"];
            item.ListItems = new string[]{ "image",
                         "text",
                         "uniqueidentifier",
                         "tinyint",
                         "smallint",
                         "int",
                         "smalldatetime",
                         "real",
                         "money",
                         "datetime",
                         "float",
                         "sql_variant",
                         "ntext",
                         "bit",
                         "decimal",
                         "numeric",
                         "smallmoney",
                         "bigint",
                         "varbinary",
                         "varchar",
                         "binary",
                         "char",
                         "timestamp",
                         "nvarchar",
                         "nchar",
                         "sysname"};
        }
        return true;
    }

    public object Tag = null;
    public override string TagName
    {
        get{ return "field";}
    }
    public DesignDBFieldElement()
    {
        base.bolMultiline = false;
        this.intAlign = System.Drawing.StringAlignment.Near ;
    }
    ///<summary>
    /// 字段名称
    ///</summary>            
    public string FName
    {
        get{ return strID ;}
        set{ strID = value;}
    }

    private string strDesc ;
    ///<summary>
    /// 字段说明
    ///</summary>            
    public string Desc
    {
        get{ return strDesc ;}
        set{ strDesc = value;}
    }

    private string strRemark ;
    ///<summary>
    /// 字段详细说明
    ///</summary>            
    public string Remark
    {
        get{ return strRemark ;}
        set{ strRemark = value;}
    }

    private string strFType ;
    ///<summary>
    /// 字段类型
    ///</summary>            
    public string FType
    {
        get{ return strFType ;}
        set{ strFType = value;}
    }

    private string strFWidth ;
    ///<summary>
    /// 字段长度
    ///</summary>            
    public string FWidth
    {
        get{ return strFWidth;}
        set{ strFWidth = value;}
    }

    private string strFFormat ;
    ///<summary>
    /// 字段内容格式
    ///</summary>            
    public string FFormat
    {
        get{ return strFFormat ;}
        set{ strFFormat = value;}
    }

    private bool bolNullable = true;
    ///<summary>
    /// 是否为空
    ///</summary>            
    public bool  Nullable
    {
        get{ return bolNullable ;}
        set{ bolNullable = value;}
    }

    private bool bolPrimaryKey = false;
    ///<summary>
    /// 是否为主键
    ///</summary>            
    public bool  PrimaryKey
    {
        get{ return bolPrimaryKey ;}
        set{ bolPrimaryKey = value;}
    }

    private bool bolIndexed = false;
    ///<summary>
    /// 是否索引
    ///</summary>            
    public bool  Indexed
    {
        get{ return bolIndexed ;}
        set{ bolIndexed = value;}
    }

    private bool bolHasDefaultValue = false;
    ///<summary>
    /// 是否有默认值
    ///</summary>            
    public bool  HasDefaultValue
    {
        get{ return bolHasDefaultValue ;}
        set{ bolHasDefaultValue = value;}
    }

    private string strDefaultValue ;
    ///<summary>
    /// 默认值
    ///</summary>            
    public string DefaultValue
    {
        get{ return strDefaultValue;}
        set{ strDefaultValue = value;}
    }

    private bool bolIsBoolean = false;
    ///<summary>
    /// 是否是布尔类型的字段
    ///</summary>            
    public bool  IsBoolean
    {
        get{ return bolIsBoolean ;}
        set{ bolIsBoolean = value;}
    }

    private string strTrueValue = null;
    ///<summary>
    /// 表示真的数值
    ///</summary>            
    public string TrueValue
    {
        get{ return strTrueValue ;}
        set{ strTrueValue = value;}
    }

    private string strFalseValue = null;
    ///<summary>
    /// 表示假的数值
    ///</summary>            
    public string FalseValue
    {
        get{ return strFalseValue ;}
        set{ strFalseValue = value;}
    }

    /// <summary>
    /// 转换列表
    /// </summary>
    public XDesignerCommon.StringValueItemCollection ConvertList =
         new XDesignerCommon.StringValueItemCollection();
    public XDesignerCommon.StringValueItemCollection CustomProperties = 
        new XDesignerCommon.StringValueItemCollection();
    
    public string ConvertListString
    {
        get{ return ConvertList.ToString(); }
        set{ ConvertList.ParseString( value );}
    }
}//public class DesignDBFieldElement : XDesigner.DesignLabel 

Points of Interest

Every one know it is very hard to develop a WYSWYG  designer , but using XDesignerLib middle ware , you can create your own WYSWYG designer fast and easy . What are you waiting for ? Try it at once . 

Learn more information about XDBDesigner , please visit http://www.xdesigner.cn/xdbdesigner/default-eng.htm .

Learn more information about XDesignerLib , please visit http://www.xdesigner.cn/xdesignerlib/default-eng.htm .

Lear more information about author , please visit http://www.xdesigner.cn/default-eng.htm or mail to yyf9989@hotmail.com .

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here