チェックボックス型セルでチェック状態にテキストを連動させるには?
対象製品
El Tabelle for .NET
詳細
チェックボックス型セルで、チェック状態に応じてチェックボックス型セルのテキストを連動させ、「在庫あり」「在庫なし」のような表示を行うには、次のようにコーディングします。
[Visual Basic]
[C#]
ActiveCell プロパティだけを操作している点に注目してください。編集中のセルの内容は ActiveCell プロパティで操作します。CellXXX プロパティや Sheet(,) を操作した場合、編集前の値が操作され、ActiveCell プロパティの値で上書きされてしまいます。
[Visual Basic]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'A1セルをチェックボックス型セルにする
Sheet1.CellPosition = New GrapeCity.Views.ElTabelle.Position("A1")
Sheet1.CellEditor = New GrapeCity.Win.Editors.CheckBoxEditor("Uncheked")
End Sub
Private Sub Sheet1_CellNotify(ByVal sender As Object, ByVal e As GrapeCity.Views.ElTabelle.CellNotifyEventArgs) Handles Sheet1.CellNotify
Select Case e.Name
Case "CheckedChanged"
''チェックボックス型セルの値が変更されたとき
If Sheet1.ActiveCell.Value = 1 Then
''チェックボックスがONのとき
Dim objCheckBoxEditor As GrapeCity.Win.Editors.CheckBoxEditor = Sheet1.ActiveCell.Editor
objCheckBoxEditor.Text = "Checked"
Sheet1.ActiveCell.Editor = objCheckBoxEditor
Else
''チェックボックスがOFFのとき
Dim objCheckBoxEditor As GrapeCity.Win.Editors.CheckBoxEditor = Sheet1.ActiveCell.Editor
objCheckBoxEditor.Text = "Uncheked"
Sheet1.ActiveCell.Editor = objCheckBoxEditor
End If
End Select
End Sub
'A1セルをチェックボックス型セルにする
Sheet1.CellPosition = New GrapeCity.Views.ElTabelle.Position("A1")
Sheet1.CellEditor = New GrapeCity.Win.Editors.CheckBoxEditor("Uncheked")
End Sub
Private Sub Sheet1_CellNotify(ByVal sender As Object, ByVal e As GrapeCity.Views.ElTabelle.CellNotifyEventArgs) Handles Sheet1.CellNotify
Select Case e.Name
Case "CheckedChanged"
''チェックボックス型セルの値が変更されたとき
If Sheet1.ActiveCell.Value = 1 Then
''チェックボックスがONのとき
Dim objCheckBoxEditor As GrapeCity.Win.Editors.CheckBoxEditor = Sheet1.ActiveCell.Editor
objCheckBoxEditor.Text = "Checked"
Sheet1.ActiveCell.Editor = objCheckBoxEditor
Else
''チェックボックスがOFFのとき
Dim objCheckBoxEditor As GrapeCity.Win.Editors.CheckBoxEditor = Sheet1.ActiveCell.Editor
objCheckBoxEditor.Text = "Uncheked"
Sheet1.ActiveCell.Editor = objCheckBoxEditor
End If
End Select
End Sub
[C#]
private void Form1_Load(object sender, System.EventArgs e)
{
//A1セルをチェックボックス型セルにする
sheet1.CellPosition = new GrapeCity.Views.ElTabelle.Position("A1");
sheet1.CellEditor = new GrapeCity.Win.Editors.CheckBoxEditor("Unchecked");
}
private void sheet1_CellNotify(object sender, GrapeCity.Views.ElTabelle.CellNotifyEventArgs e)
{
switch(e.Name)
{
case "CheckedChanged":
//チェックボックス型セルの値が変更されたとき
if((int)sheet1.ActiveCell.Value == 1)
{
//チェックボックスがONのとき
GrapeCity.Win.Editors.CheckBoxEditor objCheckBoxEditor =
(GrapeCity.Win.Editors.CheckBoxEditor)sheet1.ActiveCell.Editor;
objCheckBoxEditor.Text = "Checked";
sheet1.ActiveCell.Editor = objCheckBoxEditor;
}
else
{
//チェックボックスがOFFのとき
GrapeCity.Win.Editors.CheckBoxEditor objCheckBoxEditor =
(GrapeCity.Win.Editors.CheckBoxEditor)sheet1.ActiveCell.Editor;
objCheckBoxEditor.Text = "Unchecked";
sheet1.ActiveCell.Editor = objCheckBoxEditor;
}
break;
}
}
{
//A1セルをチェックボックス型セルにする
sheet1.CellPosition = new GrapeCity.Views.ElTabelle.Position("A1");
sheet1.CellEditor = new GrapeCity.Win.Editors.CheckBoxEditor("Unchecked");
}
private void sheet1_CellNotify(object sender, GrapeCity.Views.ElTabelle.CellNotifyEventArgs e)
{
switch(e.Name)
{
case "CheckedChanged":
//チェックボックス型セルの値が変更されたとき
if((int)sheet1.ActiveCell.Value == 1)
{
//チェックボックスがONのとき
GrapeCity.Win.Editors.CheckBoxEditor objCheckBoxEditor =
(GrapeCity.Win.Editors.CheckBoxEditor)sheet1.ActiveCell.Editor;
objCheckBoxEditor.Text = "Checked";
sheet1.ActiveCell.Editor = objCheckBoxEditor;
}
else
{
//チェックボックスがOFFのとき
GrapeCity.Win.Editors.CheckBoxEditor objCheckBoxEditor =
(GrapeCity.Win.Editors.CheckBoxEditor)sheet1.ActiveCell.Editor;
objCheckBoxEditor.Text = "Unchecked";
sheet1.ActiveCell.Editor = objCheckBoxEditor;
}
break;
}
}
ActiveCell プロパティだけを操作している点に注目してください。編集中のセルの内容は ActiveCell プロパティで操作します。CellXXX プロパティや Sheet(,) を操作した場合、編集前の値が操作され、ActiveCell プロパティの値で上書きされてしまいます。
キーワード
HowTo
この文書は、以前は次のFAQ IDで公開されていました : 3385