セルの編集開始時に入力カーソルをセル内の一番右に移動したい

文書番号 : 38159     文書種別 : 使用方法     登録日 : 2015/03/02     最終更新日 : 2015/03/02
文書を印刷する
対象製品
SPREAD for ASP.NET 8.0J
詳細
メモメモ
  • 既存のセル型を継承しカスタムセルを作成する場合、不具合修正や内部仕様の変更により動作が変わる可能性があります。

既存のセル型を継承したセルを作成し、独自のクライアント側スクリプトを記述する方法が考えられます。

◎サンプルコード(VB)
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  If IsPostBack Then
    Return
  End If

  ' セル型と値の設定
  Dim tc As New CustomTextCell
  FpSpread1.ActiveSheetView.Columns(0).CellType = tc
  FpSpread1.ActiveSheetView.SetValue(0, 0, "abcdefg")

  ' スクリプトの設定
  Dim script As StringBuilder = New StringBuilder()
  script.Append("function onFocus(tx) {")
  script.Append("  var range = tx.createTextRange();")
  script.Append("  range.move('character', tx.value.length);")
  script.Append("  range.select();")
  script.Append("}")
  ClientScript.RegisterStartupScript(Me.GetType, "", script.ToString(), True)
End Sub

Private Class CustomTextCell
  Inherits FarPoint.Web.Spread.TextCellType

  Public Overrides Function GetEditorControl(id As String, parent As System.Web.UI.WebControls.TableCell, style As FarPoint.Web.Spread.Appearance, margin As FarPoint.Web.Spread.Inset, value As Object, upperLevel As Boolean) As System.Web.UI.Control
    Dim editor As TextBox = MyBase.GetEditorControl(id, parent, style, margin, value, upperLevel)
    editor.Attributes.Add("onfocus", "onFocus(this)")
    Return editor
  End Function
End Class

◎サンプルコード(C#)
protected void Page_Load(object sender, EventArgs e)
{
  if (IsPostBack)
  {
    return;
  }

  // セル型と値の設定
  CustomTextCell tc = new CustomTextCell();
  FpSpread1.ActiveSheetView.Columns[0].CellType = tc;
  FpSpread1.ActiveSheetView.SetValue(0, 0, "abcdefg");

  // スクリプトの設定
  System.Text.StringBuilder script = new System.Text.StringBuilder();
  script.Append("function onFocus(tx) {");
  script.Append("  var range = tx.createTextRange();");
  script.Append("  range.move('character', tx.value.length);");
  script.Append("  range.select();");
  script.Append("}");
  ClientScript.RegisterStartupScript(this.GetType(), "", script.ToString(), true);
}

[Serializable]
private class CustomTextCell : FarPoint.Web.Spread.TextCellType
{
  public override Control GetEditorControl(string id, TableCell parent, FarPoint.Web.Spread.Appearance style, FarPoint.Web.Spread.Inset margin, object value, bool upperLevel)
  {
    TextBox editor = (TextBox)base.GetEditorControl(id, parent, style, margin, value, upperLevel);
    editor.Attributes.Add("onfocus", "onFocus(this)");
    return editor;
  }
}