LineControl.cs 12 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyControlLib
{
    public partial class LineControl : UserControl
    {
        
        public LineControl()
        {
            InitializeComponent();
        }

        public LineControl(string strName,int col, int row)
        {
            InitializeComponent();
            this.Name = strName;
            
        }

        /// <summary>
        /// 方向排布 
        /// </summary>
        public  enum LayDirection
        {
            Left=0,
            Right=2,
            Up=3,
            Down=1,
        }
        /// <summary>
        /// 元件排布 函数
        /// </summary>
        /// <param name="Name">元件的名称</param>
        /// <param name="start">命名的基数 从0开始</param>
        /// <param name="col">列(共29,从0开始)</param>
        /// <param name="row">行(共13,从0开始)</param>
        /// <param name="count">此次添加的元素个数</param>
        /// <param name="direction">以该(col,row)开始以何方向添加元素</param>
        public void AddControlToTableLayout(string sName,int start,int col,int row,int count,LayDirection direction,bool Remark)
        {
            if (col < 0 || col > 20) { throw new ArgumentOutOfRangeException(string.Format("超出了定义范围{0} [0,19]", col)); }
            if (row < 0 || row > 7) { throw new ArgumentOutOfRangeException(string.Format("超出了定义范围{0} [0,6]", row)); }
            
            #region 方向判断
            int t = (int)direction;
            switch (t)
            {
                case 0://left
                    {
                        if (col - count < -1)
                        { 
                            throw new ArgumentOutOfRangeException(string.Format("超出了定义范围{0}:{1}-{2}<-1", direction.ToString(), col, count)); 
                        }
                        #region
                        string strName = sName;
                        for (int i = 0; i < count; i++)
                        {
                            LeftStation uc = new LeftStation();
                            string str=string.Format(strName + "{0:D4}", i+start);
                            if (tableLayoutPanel1.Controls.Find(str, false).ToArray().Length < 1)
                            {
                                uc.Name = str;
                                Control ct = tableLayoutPanel1.GetControlFromPosition(col - i, row);
                                tableLayoutPanel1.Controls.Remove(ct);
                                tableLayoutPanel1.Controls.Add(uc, col - i, row);
                                uc.Dock = DockStyle.Fill;
                                if(Remark==true)
                                {
                                    if (i == count - 1 || i == 0) { continue; }
                                    //left 的工位名称为其下一个
                                    if (i == 0 || i == count - 1) { continue; }
                                    Label lb = new Label();
                                    lb.Font = new Font("宋体", 12);
                                    lb.Text = str;
                                    Control ct1 = tableLayoutPanel1.GetControlFromPosition(col - i, row + 1);
                                    if (ct1 == null)
                                    {
                                        tableLayoutPanel1.Controls.Add(lb, col - i, row + 1);
                                        lb.Dock = DockStyle.Fill;
                                    }
                                }
                            }
                            else
                            {
                                throw new ArgumentOutOfRangeException(string.Format("重名名错误{1}",str));
                            }
                        }
                        #endregion
                        break;
                    }
                case 1://down
                    {
                        if (row + count > 7)
                        {
                            throw new ArgumentOutOfRangeException(string.Format("超出了定义范围{0}:{1}+{2}>6", direction.ToString(), row, count));
                        }
                        #region
                        string strName = sName;
                        for (int i = 0; i < count; i++)
                        {
                            DownStation uc = new DownStation();
                            string str = string.Format(strName + "{0:D4}", i+start);
                            if (tableLayoutPanel1.Controls.Find(str, false).ToArray().Length < 1)
                            {
                                uc.Name = str;
                                Control ct = tableLayoutPanel1.GetControlFromPosition(col, row + i);
                                tableLayoutPanel1.Controls.Remove(ct);
                                tableLayoutPanel1.Controls.Add(uc, col , row+i);
                                uc.Dock = DockStyle.Fill;
                                if (Remark == true)
                                {
                                    if (i == 0 || i == count - 1) { continue; }
                                    Label lb = new Label();
                                    lb.Font = new Font("宋体", 12);
                                    lb.Text = str;
                                    Control ct1 = tableLayoutPanel1.GetControlFromPosition(col + 1, row + i);
                                    if (ct1 == null)
                                    {
                                        tableLayoutPanel1.Controls.Add(lb, col + 1, row + i);
                                        lb.Dock = DockStyle.Fill;
                                    }
                                }
                            }
                            else
                            {
                                throw new ArgumentOutOfRangeException(string.Format("重名名错误{1}", str));
                            }
                        }
                        #endregion
                        break;
                    }
                case 2://right
                    {
                        if (col + count > 20)
                        {
                            throw new ArgumentOutOfRangeException(string.Format("超出了定义范围{0}:{1}+{2}>15", direction.ToString(), col, count));
                        }
                        #region
                        string strName = sName;
                        for (int i = 0; i < count; i++)
                        {
                            RightStation uc = new RightStation();
                            string str = string.Format(strName + "{0:D4}", i+start);
                            if (tableLayoutPanel1.Controls.Find(str, false).ToArray().Length < 1)
                            {
                                uc.Name = str;
                                Control ct = tableLayoutPanel1.GetControlFromPosition(col + i, row);
                                tableLayoutPanel1.Controls.Remove(ct);
                                tableLayoutPanel1.Controls.Add(uc, col + i, row);
                                uc.Dock = DockStyle.Fill;
                                if (Remark == true)
                                {
                                    if (i == 0 || i == count - 1) { continue; }
                                    Label lb = new Label();
                                    lb.Font = new Font("宋体",12);
                                    lb.Text = str;
                                    Control ct1 = tableLayoutPanel1.GetControlFromPosition(col + i, row - 1);
                                    if (ct1 == null)
                                    {
                                        tableLayoutPanel1.Controls.Add(lb, col + i, row - 1);
                                        lb.Dock = DockStyle.Fill;
                                    }
                                }
                            }
                            else
                            {
                                throw new ArgumentOutOfRangeException(string.Format("重名名错误{1}", str));
                            }
                        }
                        #endregion
                        break;
                    }
                case 3://Up
                    {
                        if (row - count < -1)
                        {
                            throw new ArgumentOutOfRangeException(string.Format("超出了定义范围{0}:{1}-{2}<-1", direction.ToString(), row, count));
                        }
                        #region
                        string strName = sName;
                        for (int i = 0; i < count; i++)
                        {
                            UpStation uc = new UpStation();
                            string str = string.Format(strName + "{0:D4}", i+start);
                            if (tableLayoutPanel1.Controls.Find(str, false).ToArray().Length < 1)
                            {
                                uc.Name = str;
                                Control ct = tableLayoutPanel1.GetControlFromPosition(col, row - i);
                                tableLayoutPanel1.Controls.Remove(ct);
                                tableLayoutPanel1.Controls.Add(uc, col , row-i);
                                uc.Dock = DockStyle.Fill;
                                if(Remark==true)
                                {
                                    if (i == 0 || i == count - 1) { continue; }
                                    Label lb = new Label();
                                    lb.Font = new Font("宋体", 12);
                                    lb.Text = str;
                                    Control ct1 = tableLayoutPanel1.GetControlFromPosition(col - 1, row - i);
                                    if (ct1 == null)
                                    {
                                        tableLayoutPanel1.Controls.Add(lb, col - 1, row - i);
                                        lb.Dock = DockStyle.Fill;
                                    }
                                }
                            }
                            else
                            {
                                throw new ArgumentOutOfRangeException(string.Format("重名名错误{1}", str));
                            }
                        }
                        #endregion
                        break;
                    }
                default: 
                    {
                        throw new ArgumentOutOfRangeException("未定义此方向");
                    }
            }
            #endregion
           
        }

        /// <summary>
        /// 增加设备到监控位置
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Name"></param>
        /// <param name="col"></param>
        /// <param name="row"></param>
        public void AddEquipmentToTableLayout(string Code, string Name, int col, int row)
        {
            if (col < 0 || col > 20) { throw new ArgumentOutOfRangeException(string.Format("超出了定义范围{0} [0,19]", col)); }
            if (row < 0 || row > 7) { throw new ArgumentOutOfRangeException(string.Format("超出了定义范围{0} [0,6]", row)); }
            Equipment uc = new Equipment();
            uc.Name = Code;
            uc.DisplayText = Name;
            if (tableLayoutPanel1.Controls.Find(Name, false).ToArray().Length < 1)
            {
                
                Control ct = tableLayoutPanel1.GetControlFromPosition(col , row);
                tableLayoutPanel1.Controls.Remove(ct);
                tableLayoutPanel1.Controls.Add(uc, col , row);
                uc.Dock = DockStyle.Fill;
               
            }
            else
            {
                throw new ArgumentOutOfRangeException(string.Format("重名名错误{1}", Name));
            }
        }
    }
}