Click here to Skip to main content
15,885,366 members
Articles / Multimedia / GDI+

100% Reflective Class Diagram Creation Tool

Rate me:
Please Sign up or sign in to vote.
4.98/5 (498 votes)
14 Jun 2011CPOL28 min read 1.8M   39.6K   1.2K  
100% Reflective Class Diagram Creation Tool
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace AutoDiagramer
{
    #region frmSave CLASS
    /// <summary>
    /// An settings form to allow the user to save the current diagram
    /// </summary>
    public partial class frmSave : Form
    {
        #region Instance Fields
        //instance fields
        private string _SaveDirectory;
        private string _SaveFile;
        private ImageFormat _ImgFormat;
        #endregion
        #region Constructor
        /// <summary>
        /// Creates a new frmSave object
        /// </summary>
        public frmSave()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw |
                     ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true);
            //add all items to the combo and select the 1st index
            cmbImageFormat.Items.Add("Bmp");
            cmbImageFormat.Items.Add("Emf");
            cmbImageFormat.Items.Add("Exif");
            cmbImageFormat.Items.Add("Gif");
            cmbImageFormat.Items.Add("Jpeg");
            cmbImageFormat.Items.Add("Png");
            cmbImageFormat.SelectedIndex = 0;
            //set default ImageFormat
            RequiredImageFormat = ImageFormat.Bmp;

        }
        #endregion
        #region Public Properties
        /// <summary>
        /// The Save directory
        /// </summary>
        public string SaveDirectory
        {
            get { return _SaveDirectory; }
            set { _SaveDirectory=value; }
        }

        /// <summary>
        /// The Save file
        /// </summary>
        public string SaveFile
        {
            get { return _SaveFile; }
            set { _SaveFile=value; }
        }

        /// <summary>
        /// The image format
        /// </summary>
        public ImageFormat RequiredImageFormat
        {
            get { return _ImgFormat; }
            set { _ImgFormat = value; }
        }
        #endregion
        #region Private Methods
        /// <summary>
        /// Initialise the GUI with a default path for file saving
        /// </summary>
        /// <param name="sender">frmSettings</param>
        /// <param name="e">the event args</param>
        private void frmSettings_Load(object sender, EventArgs e)
        {
            //initialise the GUI with a default path for file saving
            string currPath = @"c:\";
            saveFileDialog1.InitialDirectory = @currPath;
            txtDirectory.Text = @currPath + @"File." + @cmbImageFormat.SelectedItem.ToString();
            SaveDirectory = @currPath;
            SaveFile = txtDirectory.Text;
        }

        /// <summary>
        /// Sets the SaveDirectory property to the value of
        /// the txtDirectory text box and closes this form
        /// </summary>
        /// <param name="sender">btnOk</param>
        /// <param name="e">the event args</param>  
        private void btnOk_Click(object sender, EventArgs e)
        {
            this.DialogResult=DialogResult.OK;
            SaveFile = txtDirectory.Text;
            this.Close();
        }

        /// <summary>
        /// Prompts the user to save a file to a location of their choice
        /// </summary>
        /// <param name="sender">btnDirectory</param>
        /// <param name="e">the event args</param>
        private void btnDirectory_Click(object sender, EventArgs e)
        {
            
            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                if (!saveFileDialog1.FileName.Equals(string.Empty))
                {
                    //OK so, now update GUI and relevant properties
                    txtDirectory.Text = saveFileDialog1.FileName + "." + RequiredImageFormat.ToString();
                    int idx = txtDirectory.Text.LastIndexOf(@"\");

                    SaveDirectory = txtDirectory.Text.Substring(0,idx);
                    SaveFile = txtDirectory.Text;
                    saveFileDialog1.Dispose();
                    GC.Collect();
                }
            }
        }


        /// <summary>
        /// Determines which ImageFormat should now be used, based on
        /// the cmbImageFormat.SelectedItem
        /// </summary>
        /// <param name="sender">cmbImageFormat</param>
        /// <param name="e">the event args</param>
        private void cmbImageFormat_SelectedValueChanged(object sender, EventArgs e)
        {

            //which ImageFormat should now be used
            switch (cmbImageFormat.SelectedItem.ToString())
            {
                case "Bmp":
                    RequiredImageFormat = ImageFormat.Bmp;
                    break;
                case "Emf":
                    RequiredImageFormat = ImageFormat.Emf;
                    break;
                case "Exif":
                    RequiredImageFormat = ImageFormat.Exif;
                    break;
                case "Gif":
                    RequiredImageFormat = ImageFormat.Gif;
                    break;
                case "Jpeg":
                    RequiredImageFormat = ImageFormat.Jpeg;
                    break;
                case "Png":
                    RequiredImageFormat = ImageFormat.Png;
                    break;
                default:
                    RequiredImageFormat = ImageFormat.Bmp;
                    break;
            }

            int idx = txtDirectory.Text.LastIndexOf(".");
            if (idx != -1)
            {
                string fNewName = txtDirectory.Text.Substring(0,idx);
                txtDirectory.Text = fNewName + "." + RequiredImageFormat.ToString();
                SaveFile = txtDirectory.Text;
            }
        }
        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
    #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions