Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 300
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

TableView


Autor: Surrogard
Sprache: C#
Bewertung: 5
(1 Bewertung)
Anzahl der Aufrufe: 7055
  
Kick it on dotnet-kicks.de  

Beschreibung:

frei skalierbares und scrollbares Control mit Tabellenauflistung und farbiger Markierung der selektierten Zelle
vornehmlich für .NET Compact Framework


Abgelegt unter: Tabelle, Table, .NET Compact Framework, scrollen, skalieren.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
 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;
        }
    }

Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.