最近有用户在论坛提问如何向 Excel 一样实现在单元格中创建列表,在这里记录下来,以供广大用户参考。

Step 1 : 自定义动作

首先,我们需要在自定义动作中抓取当前的编辑器,通过设置其文本来实现折行。代码如下:

 1: public class myAction : FarPoint.Win.Spread.Action
 2: {
 3:   public override void PerformAction(object sender)
 4:   {
 5:      FarPoint.Win.Spread.SpreadView ss = (FarPoint.Win.Spread.SpreadView)sender;
 6:      FarPoint.Win.Spread.CellType.GeneralEditor editor =
 (FarPoint.Win.Spread.CellType.GeneralEditor)ss.EditingControl;
 7:      string text = editor.Text;
 8:      text += "\r\n \u2022 ";
 9:      ss.Sheets[0].SetValue(ss.Sheets[0].ActiveRowIndex,
 ss.Sheets[0].ActiveColumnIndex, text);
 10:      ss.EditMode = true;
 11:      editor = (FarPoint.Win.Spread.CellType.GeneralEditor)ss.EditingControl;
 12:      editor.SelectionStart = editor.Text.Length;
 13:      editor.Text = text;
 14:   }
 15: }

 

Step 2 :设置自定义动作给 Spread

我们需要设置自定义动作给 Spread,来重写 Alt+Enter 快捷键动作。代码如下:

 1: FarPoint.Win.Spread.InputMap ancestorOfFocusedMap = 
fpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenAncestorOfFocused);
 2: FarPoint.Win.Spread.ActionMap am = fpSpread1.GetActionMap();
 3: am.Put("AltEnter", new myAction());
 4: ancestorOfFocusedMap.Put(new FarPoint.Win.Spread.Keystroke(Keys.Enter, Keys.None), 
FarPoint.Win.Spread.SpreadActions.MoveToNextRow );
 5: ancestorOfFocusedMap.Put(new FarPoint.Win.Spread.Keystroke(Keys.Enter, Keys.Alt)
, "AltEnter");
 6: fpSpread1.SetInputMap(FarPoint.Win.Spread.InputMapMode.WhenAncestorOfFocused,
 ancestorOfFocusedMap);

 

OutputDemo1

 

示例下载:

VS2010 + VB.NET + Framework 4.0 点击下载