GrapeCity CalendarGrid for Windows Forms 2.0J > CalendarGridの使い方 > セル型 > ComboBox型セル(CalendarComboCellType) > 値の変更を検出する |
GcCalendarGrid.CellEditingValueChangedイベントを使用すると、ComboBox型セルで選択されている値の変更を簡単に検出できます。
Imports GrapeCity.Win.CalendarGrid Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim today As DateTime = DateTime.Today Dim comboBoxCellType As New CalendarComboBoxCellType() comboBoxCellType.Items.Add("項目A") comboBoxCellType.Items.Add("項目B") GcCalendarGrid1.Content(today).Rows(1).Cells(0).Value = True GcCalendarGrid1.Content(today).Rows(1).Cells(0).CellType = comboBoxCellType.Clone() GcCalendarGrid1.ScrollIntoView(today) AddHandler GcCalendarGrid1.CellEditingValueChanged, AddressOf GcCalendarGrid1_CellEditingValueChanged End Sub Private Sub GcCalendarGrid1_CellEditingValueChanged(sender As Object, e As GrapeCity.Win.CalendarGrid.CalendarCellEditingValueChangedEventArgs) Dim today As DateTime = DateTime.Today Dim gcCalendarGrid As GcCalendarGrid = DirectCast(sender, GcCalendarGrid) Dim targetCellPosition As New CalendarCellPosition(today, 1, 0) If e.CellPosition = targetCellPosition Then Dim currentCell As CalendarCell = gcCalendarGrid.Content(e.CellPosition.Date).Rows(e.CellPosition.RowIndex).Cells(e.CellPosition.ColumnIndex) Console.WriteLine("編集中のセルの値:{0}", e.Value) Console.WriteLine("セルの値:{0}", currentCell.Value) End If End Sub
using GrapeCity.Win.CalendarGrid; private void Form1_Load(object sender, EventArgs e) { var today = DateTime.Today; var comboBoxCellType = new CalendarComboBoxCellType(); comboBoxCellType.Items.Add("項目A"); comboBoxCellType.Items.Add("項目B"); gcCalendarGrid1.Content[today].Rows[1].Cells[0].Value = true; gcCalendarGrid1.Content[today].Rows[1].Cells[0].CellType = comboBoxCellType.Clone(); gcCalendarGrid1.ScrollIntoView(today); gcCalendarGrid1.CellEditingValueChanged += gcCalendarGrid1_CellEditingValueChanged; } private void gcCalendarGrid1_CellEditingValueChanged(object sender, CalendarCellEditingValueChangedEventArgs e) { var today = DateTime.Today; var gcCalendarGrid = sender as GcCalendarGrid; var targetCellPosition = new CalendarCellPosition(today, 1, 0); if (e.CellPosition == targetCellPosition) { CalendarCell currentCell = gcCalendarGrid.Content[e.CellPosition.Date].Rows[e.CellPosition.RowIndex].Cells[e.CellPosition.ColumnIndex]; Console.WriteLine("編集中のセルの値:{0}", e.Value); Console.WriteLine("セルの値:{0}", currentCell.Value); } }
次のように、ComboBox.SelectedIndexChangedイベントを使って検出する方法も利用可能です。
Imports GrapeCity.Win.CalendarGrid Public Class Form1 Private _comboBox As ComboBox = Nothing Private _gcCalendarGrid As GcCalendarGrid = Nothing Private _cellPosition As CalendarCellPosition = CalendarCellPosition.Empty Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim today As DateTime = DateTime.Today Dim comboBoxCellType As New CalendarComboBoxCellType() comboBoxCellType.Items.Add("項目A") comboBoxCellType.Items.Add("項目B") GcCalendarGrid1.Content(today).Rows(1).Cells(0).CellType = comboBoxCellType.Clone() AddHandler GcCalendarGrid1.EditingControlShowing, AddressOf GcCalendarGrid1_EditingControlShowing End Sub Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As GrapeCity.Win.CalendarGrid.CalendarEditingControlShowingEventArgs) If TypeOf e.Control Is ComboBox Then _comboBox = DirectCast(e.Control, ComboBox) AddHandler _comboBox.SelectedIndexChanged, AddressOf comboBox_SelectedIndexChanged _gcCalendarGrid = DirectCast(sender, GcCalendarGrid) _cellPosition = _gcCalendarGrid.CurrentCellPosition End If End Sub Private Sub comboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Dim comboBox As ComboBox = DirectCast(sender, ComboBox) Console.WriteLine("Selected index:{0}", comboBox.SelectedIndex) _gcCalendarGrid.Content(_cellPosition.Date).Rows(_cellPosition.RowIndex + 1).Cells(_cellPosition.ColumnIndex).Value = comboBox.SelectedIndex.ToString() End Sub Private Sub GcCalendarGrid1_CellEndEdit(sender As Object, e As CalendarCellEndEditEventArgs) Handles GcCalendarGrid1.CellEndEdit If TypeOf _comboBox Is ComboBox Then RemoveHandler _comboBox.SelectedIndexChanged, AddressOf comboBox_SelectedIndexChanged _comboBox = Nothing _gcCalendarGrid = Nothing _cellPosition = CalendarCellPosition.Empty End If End Sub End Class
using GrapeCity.Win.CalendarGrid; private ComboBox _comboBox = null; private GcCalendarGrid _gcCalendarGrid = null; private CalendarCellPosition _cellPosition = CalendarCellPosition.Empty; private void Form1_Load(object sender, EventArgs e) { var today = DateTime.Today; var comboBoxCellType = new CalendarComboBoxCellType(); comboBoxCellType.Items.Add("項目A"); comboBoxCellType.Items.Add("項目B"); gcCalendarGrid1.Content[today].Rows[1].Cells[0].CellType = comboBoxCellType.Clone(); gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing; gcCalendarGrid1.CellEndEdit += gcCalendarGrid1_CellEndEdit; } private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e) { if (e.Control is ComboBox) { _comboBox = e.Control as ComboBox; _comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged; _gcCalendarGrid = sender as GcCalendarGrid; _cellPosition = _gcCalendarGrid.CurrentCellPosition; } } private void comboBox_SelectedIndexChanged(object sender, EventArgs e) { var comboBox = sender as ComboBox; Console.WriteLine("Selected index:{0}", comboBox.SelectedIndex); _gcCalendarGrid.Content[_cellPosition.Date].Rows[_cellPosition.RowIndex + 1].Cells[_cellPosition.ColumnIndex].Value = comboBox.SelectedIndex.ToString(); } private void gcCalendarGrid1_CellEndEdit(object sender, CalendarCellEndEditEventArgs e) { if (_comboBox is ComboBox) { _comboBox.SelectedIndexChanged -= comboBox_SelectedIndexChanged; _comboBox = null; _gcCalendarGrid = null; _cellPosition = CalendarCellPosition.Empty; } }