Spread for Winforms 表格控件:在单元格中创建列表

发布时间:2013/11/10 00:11 发布者:iceman

返回博客中心

最近有用户在论坛提问如何向 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 点击下载


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网