Feedback

C# - TableView

Veröffentlicht von am 11/6/2006
(2 Bewertungen)
frei skalierbares und scrollbares Control mit Tabellenauflistung und farbiger Markierung der selektierten Zelle
vornehmlich für .NET Compact Framework
 public class TableView : Panel
    {
        public class TableCell : Panel
        {
            protected Label m_cellText;
            protected PictureBox m_picture;
            protected Boolean m_selected;
            protected System.Drawing.Color m_backGroundColor, m_foreGroundColor;

            public Boolean Selected
            {
                get
                {
                    return m_selected;
                }
                set
                {
                    m_selected = value;
                    if (value) // TableCell was selected
                    {
                        m_cellText.BackColor = System.Drawing.Color.DarkBlue;
                        m_cellText.ForeColor = System.Drawing.Color.White;
                    }
                    else // TableCell was deselected
                    {
                        m_cellText.BackColor = m_backGroundColor;
                        m_cellText.ForeColor = m_foreGroundColor;
                    }
                }
            }
            public System.Drawing.Font CellFont
            {
                get
                {
                    return m_cellText.Font;
                }
                set
                {
                    m_cellText.Font = value;
                }
            }
            public System.Drawing.Size CellTextSize
            {
                get
                {
                    if (this.TopLevelControl != null)
                    {
                        System.Drawing.Graphics g = this.TopLevelControl.CreateGraphics();
                        System.Drawing.Size actCellTextSize = g.MeasureString(m_cellText.Text, this.CellFont).ToSize();
                        g.Dispose();
                        return actCellTextSize;
                    }
                    else
                        return m_cellText.Size;
                }
            }

            public TableCell(string cellText)
            {
                m_cellText = new Label();
                m_cellText.Text = cellText;
                this.Controls.Add(m_cellText);
                this.Controls.Add(m_picture);
                m_selected = false;
                m_cellText.Font = new System.Drawing.Font("Arial", 8.0f, System.Drawing.FontStyle.Bold);
            }

            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
                if (m_picture != null)
                {
                    m_picture.Left = 0;
                    m_picture.Width = m_picture.Image.Width;
                    m_picture.Height = m_picture.Image.Height;
                    m_cellText.Left = m_picture.Width + 2;
                    m_cellText.Width = this.Width - m_picture.Width - 2;
                }
                else
                {
                    m_cellText.Left = 0;
                    m_cellText.Width = this.Width;
                    m_cellText.Height = this.Height;

                }
            }

            public void changeBackColor(System.Drawing.Color newBackColor)
            {
                this.BackColor = newBackColor;
                m_backGroundColor = newBackColor;
                m_cellText.BackColor = this.BackColor;
                if (m_picture != null)
                    m_picture.BackColor = this.BackColor;
            }

            public override string Text
            {
                get
                {
                    return m_cellText.Text;
                }
                set
                {
                    m_cellText.Text = value;
                }
            }
        }

        protected int m_cellHeight = 20;

        protected Panel m_table;
        protected TableCell[] m_cells;
        protected string[] m_wordlist;
        protected int m_maxRows;
        protected int m_rows, m_columns;
        protected bool m_isScrolling;
        protected Timer m_ScrollTimer;

        public int numberOfColumns
        {
            get
            {
                return m_columns;
            }
            set
            {
                m_columns = value;
                initTableView();
            }
        }
        public int numberOfRows
        {
            get
            {
                return m_rows;
            }
            set
            {
                m_rows = value;
                initTableView();
            }
        }

        public TableCell[] Items
        {
            get
            {
                return m_cells;
            }
        }
        public TableCell this[int i]
        {
            get
            {
                return m_cells[i];
            }
            set
            {
                m_cells[i] = value;
            }
        }
        public System.Collections.IEnumerator GetEnumerator()
        {
            return m_cells.GetEnumerator();
        }
        public System.Drawing.Font CellFont
        {
            get
            {
                return m_cells[0].CellFont;
            }
            set
            {
                for (int i = 0; i < m_cells.Length; i++)
                {
                    m_cells[i].CellFont = value;
                }
                OnResize(System.EventArgs.Empty);
                this.Refresh();
            }
        }

        public void select(int i)
        {
            m_cells[i].Selected = true;
        }
        public void deselect(int i)
        {
            m_cells[i].Selected = false;
        }

        public TableView()
        {
            m_wordlist = new string[] { "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10", "test11" };
            m_columns = 4;
            m_rows = 4;
            m_table = new Panel();
            this.Controls.Add(m_table);
            loadWordList(m_wordlist);
        }
        public TableView(String[] wordlist)
        {
            m_columns = 4;
            m_rows = 4;
            m_table = new Panel();
            this.Controls.Add(m_table);
            loadWordList(wordlist);
        }
        public TableView(string[] words, int columns, int rows)
        {
            m_columns = columns;
            m_maxRows = (words.Length / columns)
                    + (((double)words.Length / (double)columns) > (words.Length / columns)
                        ? 1
                        : 0);
            m_rows = rows;
            m_table = new Panel();
            loadWordList(words);
        }

        public void loadWordList(string[] wordlist)
        {
            m_wordlist = wordlist;
            m_table.Controls.Clear();
            m_cells = new TableCell[wordlist.Length];
            for (int i = 0; i < m_cells.Length; i++)
            {
                m_cells[i] = new TableCell((i + 1).ToString() + " " + m_wordlist[i]);
                m_cells[i].Tag = m_wordlist[i];
                m_table.Controls.Add(m_cells[i]);
                m_cells[i].changeBackColor(this.BackColor);
            }
            initTableView();
        }

        protected void initTableView()
        {
            m_maxRows = (m_wordlist.Length / m_columns)
                    + (((double)m_wordlist.Length / (double)m_columns) > (m_wordlist.Length / m_columns)
                        ? 1
                        : 0);
            OnResize(System.EventArgs.Empty);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            SuspendLayout();
            m_table.Width = this.Width;
            //CellHeight and -Width calculation including change of number of columns
            m_cellHeight = 0;
            int cellWidth = 0;
            System.Drawing.Size actSize = new System.Drawing.Size();
            for (int i = 0; i < m_cells.Length; i++)
            {
                actSize = m_cells[i].CellTextSize;
                m_cellHeight = (actSize.Height + 2 > m_cellHeight) ? actSize.Height + 2 : m_cellHeight;
                cellWidth = (actSize.Width + 4 > cellWidth) ? actSize.Width + 2 : cellWidth;
            }
            m_columns = (this.Width / cellWidth > 0) ? this.Width / cellWidth : 3;
            m_maxRows = (m_wordlist.Length / m_columns)
                        + (((double)m_wordlist.Length / (double)m_columns) > (m_wordlist.Length / m_columns)
                          ? 1
                          : 0);
            m_table.Height = m_cellHeight * m_maxRows;
            //Change of Cellwidth and -height
            for (int i = 0; i < m_cells.Length; i++)
            {
                m_cells[i].Width = this.Width / m_columns;
                m_cells[i].Height = m_cellHeight;
                m_cells[i].Left = (i % m_columns) * m_cells[i].Width;
                m_cells[i].Top = (i / m_columns) * m_cells[i].Height;
            }
            m_table.Top = 0;
            ResumeLayout();
        }

        public void ChangeBackColor(System.Drawing.Color newBackColor)
        {
            this.BackColor = newBackColor;
            m_table.BackColor = this.BackColor;
            foreach (TableCell tc in m_cells)
                tc.changeBackColor(this.BackColor);
        }

        public void scrollTo(object rowObject)
        {
            int row = (int)rowObject;
            int actualRow = getTopRowNumber();
            if (actualRow != row)
            {
                int steps = 10;
                int actualTop = m_table.Top;
                int topDifference = (actualRow - row) * m_cellHeight;
                for (int i = 1; i < steps; i++)
                {
                    m_table.Top = actualTop + (int)((double)topDifference / (double)steps * (double)i);
                    this.Refresh();
                    System.Threading.Thread.Sleep(20);
                }
                m_table.Top = actualTop + topDifference;
            }
            m_isScrolling = false;
        }
        public void scrollToAsync(int row)
        {
            while (m_isScrolling) ;
            m_isScrolling = true;

        }

        public int getTopRowNumber()
        {
            return -m_table.Top / m_cellHeight;
        }
        public int getRowOfItem(int i)
        {
            return i / m_columns;
        }
    }

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!