How Can I Implement the Grid-like Control Using C# 2010 Express
I need a control开发者_如何学编程 like this:
This is from the Microsoft Word: Insert => Symbols.
- This dialog has a grid-like control with a list of Unicode Characters;
- You can select any character;
- The selected character's Unicode is displayed;
- User cannot edit the character;
Besides, I need to extend it's feature: 1. User can delete the cell with the selected character; 2. User can add a list of characters (from a file or whatever).
I'm asking what built-in controls I should use to implement this specific control.
Thanks.
Peter
I was able to create a simple mock up using the standard DataGridView control.
private void InitilizeDataGridView(DataGridView view)
{
var defaultCellStyle = new DataGridViewCellStyle();
defaultCellStyle.ForeColor = SystemColors.ControlText;
defaultCellStyle.WrapMode = DataGridViewTriState.False;
defaultCellStyle.SelectionBackColor = SystemColors.Highlight;
defaultCellStyle.BackColor = System.Drawing.SystemColors.Window;
defaultCellStyle.SelectionForeColor = SystemColors.HighlightText;
defaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
defaultCellStyle.Font = new Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((0)));
view.DefaultCellStyle = defaultCellStyle;
view.MultiSelect = false;
view.RowHeadersVisible = false;
view.AllowUserToAddRows = false;
view.ColumnHeadersVisible = false;
view.AllowUserToResizeRows = false;
view.AllowUserToDeleteRows = false;
view.AllowUserToOrderColumns = true;
view.AllowUserToResizeColumns = false;
view.BackgroundColor = SystemColors.Control;
for(var i = 0; i < 16; i++)
{
view.Columns.Add(new DataGridViewTextBoxColumn { AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, Resizable = DataGridViewTriState.False });
}
DataGridViewRow row = null;
for (int index = 32, cell = 0; index < 255; index++, cell++)
{
if(cell % 16 == 0)
{
if(row != null)
{
view.Rows.Add(row);
}
row = new DataGridViewRow { Height = 40 };
row.CreateCells(view);
cell = 0;
}
if (row != null)
{
row.Cells[cell].Value = Char.ConvertFromUtf32(index);
}
}
}
In WinForms, you can add fixed-size Label
controls to a FlowLayoutPanel
at runtime.
Note that this will not scale well; do not make thousands of Label controls.
If you want large numbers of characters, you can make a single screenfull of labels, then add a ScrollBar control and handle the Scroll event to change the label captions.
精彩评论