有用戶提出,怎樣使編輯狀態下的單元格文本垂直居中顯示。Spread 表格控件單元格有兩種狀態,編輯狀態和普通顯示狀態。在顯示狀態下,咱們能夠經過單元格的格式相關屬性來設置單元格樣式、對齊方式。該功能咱們須要經過自定義單元格方式實現。 c#
實現該功能的核心代碼以下: app
1: public override Control GetEditorControl(Control parent, FarPoint.Win.Spread.Appearance appearance, float zoomFactor) 2: { 3: TextBox tb = new TextBox(); 4: FarPoint.Win.Spread.FpSpread spread = parent as FarPoint.Win.Spread.FpSpread; 5: float height = spread.Sheets[0].ActiveRow.Height; 6: float width = spread.Sheets[0].ActiveColumn.Width; 7: tb.Width = (int)width; 8: tb.Location = new Point(0, (int)(height / 2 - tb.Height / 2) + 2); 9: tb.Text = spread.Sheets[0].ActiveCell.Text; 10: uc = new MyEditorControl(tb); 11: uc.Height = (int)height; 12: uc.Width = (int)width; 13: return uc; 14: }
自定義單元格編譯器由 TextBox 和 UserControl 組成。咱們能夠經過調整 TextBox 的位置來實現輸入時文本居中。主要是經過獲取當前編輯單元格的大小,來設置自定義編輯器的大小,從而肯定 TextBox 的位置。 編輯器
在設置單元格內容以後,咱們經過如下代碼返回單元格值: ide
1: public override object GetEditorValue() 2: { 3: return uc.Tb.Text; 4: }
基於 UserControl 實現的自定義編輯器代碼以下: this
1: public partial class MyEditorControl : UserControl 2: { 3: TextBox tb; 4: 5: public MyEditorControl(TextBox tb1) 6: { 7: this.BackColor = Color.White; 8: tb = tb1; 9: tb.BorderStyle = System.Windows.Forms.BorderStyle.None; 10: this.Controls.Add(tb); 11: } 12: public TextBox Tb 13: { 14: get { return tb; } 15: set { tb = value; } 16: } 17: }
效果圖: spa
源碼下載:VS 2010 + Spread Studio .NET 7 + .NET 4.0: 點擊下載 code
使用控件查看地址:點擊查看 orm