【问题标题】:How Can I Implement the Grid-like Control Using C# 2010 Express如何使用 C# 2010 Express 实现类似网格的控件
【发布时间】:2010-11-22 02:08:42
【问题描述】:

我需要这样的控件:

这是来自 Microsoft Word:插入 => 符号。

  1. 此对话框有一个类似网格的控件,其中包含 Unicode 字符列表;
  2. 您可以选择任何字符;
  3. 显示所选字符的Unicode;
  4. 用户无法编辑字符;

此外,我需要扩展它的功能: 1.用户可以删除选中字符的单元格; 2. 用户可以添加字符列表(来自文件或其他)。

我在问我应该使用什么内置控件来实现这个特定的控件。

谢谢。

彼得

【问题讨论】:

  • WinForms? WPF? ASP.NET?银光?

标签: c# controls


【解决方案1】:

我能够使用标准 DataGridView 控件创建一个简单的模型。

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);
        }               
    }            
}

【讨论】:

  • 感谢您的回复。我实际上是在尝试 DataGridView,它看起来不错。第一个问题是:如果要删除其中一个,则该字符之后的所有内容都需要移动;第二个问题是:如果控件的宽度改变了,增加列数是非常困难的,因为它是一个二维控件(FlowLayoutPanel在这种情况下工作得更酷,但是FlowLayoutPanel由于其他问题不是最佳选择) .
  • 无论如何,我会再次尝试您的建议。使用 DataGridView 的 UI 外观和事件处理似乎是最佳选择 :-)
  • 我使用 DataGridView 实现的。就目前而言,它看起来还不错。我有一个问题:如何不显示空白单元格?假设只有 20 个字符,但一行必须有 16 个字符,所以第 2 行应该只有 4 个字符。但我不知道如何不显示额外的 12 个空白单元格。谢谢。
【解决方案2】:

在 WinForms 中,您可以在运行时将固定大小的 Label 控件添加到 FlowLayoutPanel

请注意,这不会很好地扩展;不要制作数千个标签控件。
如果你想要大量的字符,你可以制作一个满屏的标签,然后添加一个 ScrollBar 控件并处理 Scroll 事件来更改标签标题。

【讨论】:

  • 我也在尝试使用FlowLayoutPanel,但是有很多东西我必须自己实现:例如箭头键移动,选择....
猜你喜欢
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 2020-10-23
相关资源
最近更新 更多