Spread for Winforms 表格控件 提供了 24 种丰富的单元格类型,通过用单元格类型或定制自己的类型,确定在一个单元格中可以输入什么样的数据、避免程序员不必要的检查和验证,并为用户提供一种自然的输入数据的方式。
其中 RichTextBoxCellType 用于在单元格中显示富文本类型,增强了文本的样式丰富性。本文将阐述“如何通过自定义RichTextBoxCellType 右键菜单,给单元格部分文字添加下划线”。
具体实现方法如下:
1.设置单元格类型为 RichTextBoxCellType :
1: FarPoint.Win.Spread.SheetView sv = this.fpSpread1.Sheets[0];
2:
3: FarPoint.Win.Spread.CellType.RichTextCellType rtb;
4: rtb = new FarPoint.Win.Spread.CellType.RichTextCellType();
5: rtb.Multiline = true;
6:
7: System.IO.StreamReader reader = new System.IO.StreamReader("test.rtf");
8: string text = reader.ReadToEnd();
9:
10:
11: sv.Cells[0, 0].CellType = rtb;
12: sv.Cells[0, 0].Value = text;
13:
14: sv.Rows[0].Height = 200;
15: sv.Columns[0].Width = 200;
2.添加 Spread EditModeOn 事件获取Spread编辑事件:
1: this.fpSpread1.EditModeOn+=new EventHandler(fpSpread1_EditModeOn);
3.获取单元格编辑器,并且添加 ContextMenu 用于定制文字格式:
1: RichTextBox editor;
2: void fpSpread1_EditModeOn(object sender, EventArgs e)
3: {
4: editor = this.fpSpread1.EditingControl as RichTextBox;
5: if (editor!=null)
6: {
7: ContextMenu cm = new ContextMenu();
8: MenuItem itm = new MenuItem();
9: itm.Text = "添加下滑线";
10: itm.Click += new EventHandler(itm_Click);
11: cm.MenuItems.Add(itm);
12: editor.ContextMenu = cm;
13: }
14: }
4.设置选择文字的字体样式,添加下划线:
1: void itm_Click(object sender, EventArgs e)
2: {
3: Font font = new System.Drawing.Font("SimSum", 12, FontStyle.Underline);
4: editor.SelectionFont = font;
5: }
Demo 下载:Demo 下载:vs2010 + .NET 4.0 + Studio for Silverlight 2013V2.5 + C#