Sunday 15 May 2016

C# form inheritance


---------------------------------------------------------------------------------------------------
//class supplier

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1.model
{
    class supplier
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public string info { get { return ID.ToString() + "     " + Name; } set { info = value; } }
    }
}

-------------------------------------------------------------------------------------------------------
//class supplier manager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ClassLibrary1.model
{
    class supplier_manager
    {
        public static List<supplier> suppliers = new List<supplier>();

        public static string file_path { get; set; }

        public static void generate()
        {        
            suppliers.Add(new supplier { ID = 1, Name = "aaa" });
            suppliers.Add(new supplier { ID = 2, Name = "bbb" });
            suppliers.Add(new supplier { ID = 3, Name = "ccc" });
        }

        public static void load()
        {
            if (suppliers != null)
            {
                suppliers.Clear();
            }

            FileStream stream = new FileStream(file_path, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(stream);

            string data = reader.ReadLine();        

            while (data != null)
            {
                try
                {
                    string[] value = data.Split(',');

                    supplier s = new supplier();
                    s.ID = Convert.ToInt32(value[0]);
                    s.Name = value[1];

                    suppliers.Add(s);
                }
                catch (Exception e1)
                {
                }

                data = reader.ReadLine();
            }

            stream.Close();
            reader.Close();
         
        }

        public static void save()
        {
            FileStream stream = new FileStream(file_path, FileMode.Create, FileAccess.Write);
            StreamWriter writter = new StreamWriter(stream);

            foreach (supplier s in suppliers)
            {
                writter.WriteLine(s.ID + "," + s.Name);
            }

            writter.Close();

            stream.Close();
        }

    }
}

----------------------------------------------------------------------------------------------------------------
//supplier manager form



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary1.model;

namespace ClassLibrary1
{
    public partial class supplier_manager_form : Form
    {
        public supplier_manager_form()
        {
            InitializeComponent();
        }



        private void supplier_manager_form_Load(object sender, EventArgs e)
        {
            //supplier_manager.generate();
        }

        private void base_function_Click(object sender, EventArgs e)
        {
            Process();
        }

        protected virtual void Process()
        {
            MessageBox.Show("base function");
        }

        private void load_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Text|*.csv";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                supplier_manager.file_path = openFileDialog1.FileName;

                supplier_manager.load();

                refresh.PerformClick(); //call button click
            }
         
        }

        private void save_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Text|*.csv";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                supplier_manager.file_path = openFileDialog1.FileName;

                supplier_manager.save();
            }
        }

        public void refresh_Click(object sender, EventArgs e)
        {
            uxrecord.DataSource = null; //refresh listbox
            uxrecord.DataSource = supplier_manager.suppliers;
            uxrecord.DisplayMember = "info";
            uxrecord.ValueMember = "ID";
        }

     
    }
}

--------------------------------------------------------------------------------------------------------
//maintance form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary1.model;

namespace ClassLibrary1
{
    public partial class maintance : Form
    {
        public maintance()
        {
            InitializeComponent();
        }

        private void add_Click(object sender, EventArgs e)
        {
            supplier s = new supplier();

            try
            {
                s.ID = Convert.ToInt32(ID_textbox.Text);
                s.Name = Name_textbox.Text;

                supplier_manager.suppliers.Add(s);
            }
            catch (Exception e2)
            {
            }

            
        }       

        private void delete_Click(object sender, EventArgs e)
        {
            supplier s = new supplier();
            s = supplier_manager.suppliers.Find(x => x.ID == Convert.ToInt32(ID_textbox.Text));
            if (s != null)
            {
                supplier_manager.suppliers.Remove(s);
            }
        }

        private void search_ID_Click(object sender, EventArgs e)
        {
            supplier s = new supplier();
            s = supplier_manager.suppliers.Find(x => x.ID == Convert.ToInt32(ID_textbox.Text));
            if (s != null)
            {
                Name_textbox.Text = s.Name;
            }
        }

        private void search_Name_Click(object sender, EventArgs e)
        {
            supplier s = new supplier();
            s = supplier_manager.suppliers.Find(x => x.Name == Name_textbox.Text);
            if (s != null)
            {
                ID_textbox.Text = s.ID.ToString();
            }
        }


    }
}

--------------------------------------------------------------------------------------------------------------
//inherited supplier manager form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace inheritant_form
{
    public partial class inherited_supplier_manager_form : ClassLibrary1.supplier_manager_form
    {
        public inherited_supplier_manager_form()
        {
            InitializeComponent();
        }

        protected override void Process()
        {
            MessageBox.Show("override supplier manager function");
        }

        private void inherited_supplier_manager_form_FormClosing(object sender, FormClosingEventArgs e)
        {
            mdi.child_position = this.Location; //next form opens in current closed form position 
            mdi.supplier_manager_open = false; //only one type of form open at one time  
        }
    }
}

---------------------------------------------------------------------------------------------------------
//inherited maintenance form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace inheritant_form
{
    public partial class inherited_maintance_form : ClassLibrary1.maintance
    {
        public inherited_maintance_form()
        {
            InitializeComponent();
        }

        private void inherited_maintance_form_FormClosing(object sender, FormClosingEventArgs e)
        {
            mdi.child_position = this.Location;
            mdi.maintance_form_open = false;
        }

    }
}

-------------------------------------------------------------------------------------------------------------
//mdi parent form



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace inheritant_form
{
    public partial class mdi : Form
    {
        public mdi()
        {
            InitializeComponent();
        }

        public static Point child_position { get; set; }
        public static bool supplier_manager_open { get; set; }
        public static bool maintance_form_open { get; set; }

        private void supplierManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (supplier_manager_open == false)
            {
                inherited_supplier_manager_form child = new inherited_supplier_manager_form();

                child.MdiParent = this;
                child.Show();
                child.Location = child_position;

//next form open in different position
                child_position = new Point(child_position.X + 50, child_position.Y + 50); 

                supplier_manager_open = true;
            }
        }

        private void mdi_Load(object sender, EventArgs e)
        {
            child_position = new Point(20, 20);
        }

        private void maintanceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (maintance_form_open == false)
            {
                inherited_maintance_form child = new inherited_maintance_form();

                child.MdiParent = this;
                child.Show();
                child.Location = child_position;

                child_position = new Point(child_position.X + 50, child_position.Y + 50);

                maintance_form_open = true;
            }
        }
    }
}

No comments:

Post a Comment