作成日: 2022/03/11 最終更新日: 2022/03/30
文書種別
使用方法
詳細
ComboBoxEditingControlのKeyDownとKeyPressの各イベントでキー操作を無効にすることで、実質的なReadOnlyを実現することができます。
[Visual Basic]
[C#]
[Visual Basic]
Imports GrapeCity.Win.MultiRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim comboBoxCell1 As New ComboBoxCell()
comboBoxCell1.Name = "comboBoxCell1"
comboBoxCell1.DropDownStyle = MultiRowComboBoxStyle.DropDown
comboBoxCell1.Items.AddRange(New String() {"AAA", "BBB", "CCC"})
GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {comboBoxCell1})
GcMultiRow1.RowCount = 5
End Sub
Private Sub GcMultiRow1_EditingControlShowing(ByVal sender As Object, ByVal e As EditingControlShowingEventArgs) Handles GcMultiRow1.EditingControlShowing
' コンボボックス型セルのイベント設定
If TypeOf e.Control Is ComboBoxEditingControl Then
RemoveHandler e.Control.KeyDown, AddressOf EditingControl_KeyDown
AddHandler e.Control.KeyDown, AddressOf EditingControl_KeyDown
RemoveHandler e.Control.KeyPress, AddressOf EditingControl_KeyPress
AddHandler e.Control.KeyPress, AddressOf EditingControl_KeyPress
End If
End Sub
Private Sub EditingControl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
' コンボボックス型セルでDeleteキーを禁止する
If e.KeyCode = Keys.Delete Then
e.Handled = True
End If
End Sub
Private Sub EditingControl_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
' コンボボックス型セルで文字入力を禁止する
e.Handled = True
End Sub
[C#]
using GrapeCity.Win.MultiRow;
private void Form1_Load(object sender, EventArgs e)
{
ComboBoxCell comboBoxCell1 = new ComboBoxCell();
comboBoxCell1.Name = "comboBoxCell1";
comboBoxCell1.DropDownStyle = MultiRowComboBoxStyle.DropDown;
comboBoxCell1.Items.AddRange(new string[] { "AAA", "BBB", "CCC" });
gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { comboBoxCell1 });
gcMultiRow1.RowCount = 5;
gcMultiRow1.EditingControlShowing +=new EventHandler(gcMultiRow1_EditingControlShowing);
}
private void gcMultiRow1_EditingControlShowing(object sender, EditingControlShowingEventArgs e)
{
// コンボボックス型セルのイベント設定
if (e.Control is ComboBoxEditingControl)
{
e.Control.KeyDown -= new KeyEventHandler(EditingControl_KeyDown);
e.Control.KeyDown += new KeyEventHandler(EditingControl_KeyDown);
e.Control.KeyPress -= new KeyPressEventHandler(EditingControl_KeyPress);
e.Control.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
}
}
private void EditingControl_KeyDown(object sender, KeyEventArgs e)
{
// コンボボックス型セルでDeleteキーを禁止する
if(e.KeyCode == Keys.Delete)
{
e.Handled = true;
}
}
private void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
{
// コンボボックス型セルで文字入力を禁止する
e.Handled = true;
}