विंडोज फॉर्म के लिए C # में एक उदाहरण तालिका का उपयोग करके एक कस्टम आइटम बनाना

अच्छा दिन!

इस लेख में, मैं सी # विंडोज फॉर्म के लिए अपने तत्वों के निर्माण का वर्णन करूंगा।

एक उदाहरण के लिए मैं सभी DataGridView कार्यक्षमता के साथ तालिका बनाऊंगा। हम बाद में अपने तत्वों पर आगे बढ़ेंगे। हम कई पाठों में पहला तत्व बनाएंगे। इस पाठ में हम टेबल ड्राइंग से ड्रा करेंगे, और यह भी: कॉलम, पंक्तियों, कोशिकाओं का निर्माण।

लिखने के लिए हम .Net फ्रेमवर्क 4.7.x, विजुअल स्टूडियो 2019 के विकास के वातावरण का उपयोग करेंगे।

सबसे पहले, हम एक नियमित विंडोज फॉर्म प्रोजेक्ट बनाएंगे। मुझे लगता है कि इसे दिखाने की कोई आवश्यकता नहीं है। और उसके बाद ही हम प्रोजेक्ट बनाते हैं "लाइब्रेरी ऑफ विंडोज फॉर्म कंट्रोल्स" (चलिए इसे CustomControl कहते हैं)।

इसके बाद, हम एक UserControl.cs फ़ाइल बनाएंगे। हम इसे हटाते हैं और सामान्य वर्ग TableCustoms.cs बनाते हैं। हमारी कक्षा नियंत्रण वर्ग से वारिस होगी।

आगे उसी फाइल में हम कुछ और क्लासेज बनाएंगे, जैसे: कॉलम, रो, सेल। आइए प्रत्येक पर अलग से विचार करें। आइए कॉलम से शुरू करें:

    [Serializable]
    public class Column
    {
        public string Name { get; set; } = "NameColumn";//  
        public string Caption { get; set; } = "CaptionColumn";// 
        public int Width { get; set; } = 100;// 
        public Color Back { get; set; } = Color.White;// 
        
        public Column()
        { 
        
        }
    }

पंक्ति वर्ग:

    [Serializable]
    public class Row
    {
        public int Heigth { get; set; } = 20;//  
        public List<Cell> Cells { get; set; } = new List<Cell>();// 
        public Row()
        {
 
        }
        public Row(List<Cell> cells)
        {
            Cells = cells;
        }

    }

सेल वर्ग (नकल करने के लिए, ICloneable इंटरफ़ेस को जोड़ने के लिए):

    [Serializable]
    public class Cell : ICloneable
    {
        public object Value { get; set; } = null;//  
        public Cell()
        {
        }
        public object Clone()
        {
            return MemberwiseClone();
        }
    }

अब हमारे मुख्य TableCustoms वर्ग की स्थापना करें:

   public class TableCustoms : Control
    {
        #region 
        public ObservableCollection<Column> Columns { get; set; } = new ObservableCollection<Column>();//  
        private ObservableCollection<Row> rows = new ObservableCollection<Row>();// 
        private int countRow = 0;// 
        #endregion
        #region 
        public int CountRow //        N 
        {
            get { return countRow; }
            set
            {
                //   
                if (value > countRow)
                {
                    int iteration = value - countRow;
                    for (int i = 0; i < iteration; i++)
                    {
                        rows.Add(new Row());
                    }
                }
                //    
                if (value < countRow)
                {
                    int iteration = countRow - value;
                    for (int i = 0; i < iteration; i++)
                    {
                        rows.Remove(rows[rows.Count - 1]);
                    }
                }

                countRow = value;
            }
        }
        //     ,   
        public ObservableCollection<Row> Rows
        {
            get { return rows; }
            set { }
        }

        public int ColumnHeaderHeigth { get; set; } = 20;//  
        public int RowHeaderWidth { get; set; } = 20;//  
        public Color ColumnHeaderBack { get; set; } = SystemColors.Control;//    
        public Color BorderColor { get; set; } = Color.Black;//   
        public bool NumerableRows { get; set; } = false;//  
        #endregion
        //  ,     
        private void EditColumn()
        {

        }
        //  
        private void EditRows()
        {
            if (countRow < rows.Count)//  
            {
                rows[rows.Count - 1].Cells = CreatCells(Columns.Count);//    
                countRow++;
            }
            if (CountRow > rows.Count)//  
            {
                countRow--;
            }

        }
        //  N  
        private List<Cell> CreatCells(int Count)
        {
            // return Enumerable.Repeat(new Cell(), Count).ToList();
            List<Cell> result = new List<Cell>();
            for (int i = 0; i < Count; i++)
            {
                result.Add(new Cell());
            }
            return result;
        }
        public TableCustoms()
        {
            rows.CollectionChanged += (e, v) => EditRows();//  
            Columns.CollectionChanged += (e, v) => EditColumn();//  
            BackColor = SystemColors.AppWorkspace;// 
            PanelTable panelTable = new PanelTable(this);//  
            panelTable.Dock = DockStyle.Fill;//    Control
            Controls.Add(panelTable);//   Control
        }
    }

स्क्रोलबार्स का उपयोग करने के लिए हमें स्क्रॉल करने योग्य कॉन्ट्रोल का उपयोग करने की आवश्यकता है, इसलिए हम पैनलटेबल क्लास बनाएंगे, स्क्रॉलेबल कॉन्ट्रॉल को वारिस करेंगे और इसे कंट्रोल पर रखेंगे (अगले पाठ में मैं बताऊंगा कि हम दो अलग-अलग कंट्रोल क्यों बनाते हैं, और अभी स्क्रॉल करने योग्य कॉन्ट्रोल का उपयोग नहीं करते हैं):

 internal class PanelTable : ScrollableControl//Control  ScrolLbar
        {
            private TableCustoms BParent;//  ,    
            public PanelTable(TableCustoms bParent)
            {
                HScroll = true;//   
                VScroll = true;//   
                AutoScroll = true;//   
                BParent = bParent;
            }
            // 
            protected override void OnPaint(PaintEventArgs e)
            {
                Matrix m = new Matrix();
                m.Translate(this.AutoScrollPosition.X, this.AutoScrollPosition.Y, MatrixOrder.Append);
                e.Graphics.Transform = m;
                Graphics graf = e.Graphics;
                int maxWidth = 0;// AutoScrollMinSize
                int maxHeight = 0;// AutoScrollMinSize
                                  // 
                foreach (Column item in BParent.Columns)
                {
                    maxWidth += item.Width;
                }
                // 
                foreach (Row item in BParent.Rows)
                {
                    maxHeight += item.Heigth;
                }
                AutoScrollMinSize = new Size(maxWidth + 100, maxHeight + 100);// AutoScrollMinSize      
                graf.Clear(BParent.BackColor);
                DrawHeaderColumns(graf);//   
                DrawHeaderRows(graf);//   
                DrawCells(graf);// 
                base.OnPaint(e);
            }
            /// <summary>
            ///   
            /// </summary>
            /// <param name="graf"></param>
            private void DrawHeaderColumns(Graphics graf)
            {
                int x = 2;
                Rectangle rect;
                rect = new Rectangle(x, 1, BParent.RowHeaderWidth, BParent.ColumnHeaderHeigth);
                graf.DrawRectangle(new Pen(BParent.BorderColor), rect);
                graf.FillRectangle(new SolidBrush(BParent.ColumnHeaderBack), rect);
                x += BParent.RowHeaderWidth + 1;
                foreach (Column item in BParent.Columns)
                {
                    rect = new Rectangle(x, 1, item.Width, BParent.ColumnHeaderHeigth);
                    graf.DrawRectangle(new Pen(BParent.BorderColor), rect);
                    graf.FillRectangle(new SolidBrush(BParent.ColumnHeaderBack), rect);
                    if (item.Caption.Length != 0)
                    {
                        StringFormat sf = new StringFormat();
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Center;
                        graf.DrawString(item.Caption, new Font("Times", 9), Brushes.Black, rect, sf);
                    }
                    x += item.Width + 1;
                }
            }
            //  
            private void DrawHeaderRows(Graphics graf)
            {
                int y = 1;
                int i = 0;
                Rectangle rect;
                y += BParent.RowHeaderWidth + 1;
                foreach (Row item in BParent.Rows)
                {
                    rect = new Rectangle(2, y, BParent.RowHeaderWidth, item.Heigth);
                    graf.DrawRectangle(new Pen(BParent.BorderColor), rect);
                    graf.FillRectangle(new SolidBrush(BParent.ColumnHeaderBack), rect);
                    if (BParent.NumerableRows)
                    {
                        StringFormat sf = new StringFormat();
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Center;
                        graf.DrawString(i.ToString(), new Font("Times", 9), Brushes.Black, rect, sf);
                    }
                    i++;
                    y += item.Heigth + 1;
                }
            }
            // 
            private void DrawCells(Graphics graf)
            {

                int x = 2 + BParent.RowHeaderWidth + 1;
                int y = 2 + BParent.ColumnHeaderHeigth;
                Rectangle rect;
                int i = 0;
                foreach (Row itemRow in BParent.Rows)
                {
                    foreach (Column itemColumn in BParent.Columns)
                    {
                        rect = new Rectangle(x, y, itemColumn.Width, itemRow.Heigth);
                        graf.DrawRectangle(new Pen(BParent.BorderColor), rect);
                        graf.FillRectangle(new SolidBrush(Color.White), rect);
                        if (itemRow.Cells[i].Value != null)
                        {
                            StringFormat sf = new StringFormat();
                            sf.Alignment = StringAlignment.Center;
                            sf.LineAlignment = StringAlignment.Center;
                            graf.DrawString(itemRow.Cells[i].Value.ToString(), new Font("Times", 9), Brushes.Black, rect, sf);
                        }
                        x += itemColumn.Width + 1;
                        i++;
                    }
                    i = 0;
                    y += itemRow.Heigth + 1;
                    x = 2 + BParent.RowHeaderWidth + 1;
                }
            }
        }

उसके बाद, तत्व के "प्रोजेक्ट को फिर से इकट्ठा करें" और तत्व को फॉर्म में जोड़ें (मुख्य प्रोजेक्ट में):

छवि

अब हमारे तत्व के कुछ तरीकों की जाँच करें:

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
             tableCustoms1.Rows.Add(new Row());// 
             tableCustoms1.Rows.Add(new Row());
             tableCustoms1.Rows[0].Cells[0].Value = "1";//  
             tableCustoms1.Rows[1].Cells[1].Value = "2";
            tableCustoms1.CountRow++;//  
            tableCustoms1.Rows[0].Cells[0].Value = "";
        }
    }

हम अपना प्रोजेक्ट लॉन्च करते हैं:

छवि

Source: https://habr.com/ru/post/undefined/


All Articles