GcComboBoxCell で選択されている値のインデックスを取得する方法
対象製品
MultiRow for Windows Forms 5.0J
詳細
InputManCell に含まれる GcComboBoxCell で選択されている値のインデックスを取得するには、GcComboBoxCell.Items プロパティのコレクションから、セルの値(Cell.Value) を検索します。
[Visual Basic]
[C#]
次のコードは拡張メソッドとして、InputMan for Windows Forms 5.0J と同じインタフェースで実装する例です。拡張メソッドを使用するには、Visual Studio 2008 が必要です。
[Visual Basic]
[C#]
この場合の使用例は次のようになります。
[Visual Basic]
[C#]
次期バージョン InputManCell for Windows Forms 6.0J では、新たに GcComboBox.FindString メソッドが提供されています。
[Visual Basic]
Imports GrapeCity.Win.MultiRow
Imports InputManCell = GrapeCity.Win.MultiRow.InputMan
Dim gcMultiRow As GcMultiRow = Me.GcMultiRow1
If TypeOf gcMultiRow.CurrentCell Is InputManCell.GcComboBoxCell Then
Dim gcComboBoxCell As InputManCell.GcComboBoxCell = TryCast(gcMultiRow.CurrentCell, InputManCell.GcComboBoxCell)
Dim selectedValue As String = DirectCast(gcComboBoxCell.Value, String)
Dim selectedIndex As Integer = -1
If Not String.IsNullOrEmpty(selectedValue) Then
For i As Integer = 0 To gcComboBoxCell.Items.Count - 1
If gcComboBoxCell.Items(i).Text = selectedValue Then
selectedIndex = i
Exit For
End If
Next
End If
MessageBox.Show(String.Format("選択されているインデックス: {0}", selectedIndex))
Else
MessageBox.Show("現在のセルはGcComboBoxCellではありません")
End If
Imports InputManCell = GrapeCity.Win.MultiRow.InputMan
Dim gcMultiRow As GcMultiRow = Me.GcMultiRow1
If TypeOf gcMultiRow.CurrentCell Is InputManCell.GcComboBoxCell Then
Dim gcComboBoxCell As InputManCell.GcComboBoxCell = TryCast(gcMultiRow.CurrentCell, InputManCell.GcComboBoxCell)
Dim selectedValue As String = DirectCast(gcComboBoxCell.Value, String)
Dim selectedIndex As Integer = -1
If Not String.IsNullOrEmpty(selectedValue) Then
For i As Integer = 0 To gcComboBoxCell.Items.Count - 1
If gcComboBoxCell.Items(i).Text = selectedValue Then
selectedIndex = i
Exit For
End If
Next
End If
MessageBox.Show(String.Format("選択されているインデックス: {0}", selectedIndex))
Else
MessageBox.Show("現在のセルはGcComboBoxCellではありません")
End If
[C#]
using GrapeCity.Win.MultiRow;
using InputManCell = GrapeCity.Win.MultiRow.InputMan;
GcMultiRow gcMultiRow = this.gcMultiRow1;
if (gcMultiRow.CurrentCell is InputManCell.GcComboBoxCell)
{
InputManCell.GcComboBoxCell gcComboBoxCell = gcMultiRow.CurrentCell as InputManCell.GcComboBoxCell;
string selectedValue = (string)gcComboBoxCell.Value;
int selectedIndex = -1;
if (!string.IsNullOrEmpty(selectedValue))
{
for (int i = 0; i < gcComboBoxCell.Items.Count; i++)
{
if (gcComboBoxCell.Items[i].Text == selectedValue)
{
selectedIndex = i;
break;
}
}
}
MessageBox.Show(string.Format("選択されているインデックス: {0}", selectedIndex));
}
else
{
MessageBox.Show("現在のセルはGcComboBoxCellではありません");
}
using InputManCell = GrapeCity.Win.MultiRow.InputMan;
GcMultiRow gcMultiRow = this.gcMultiRow1;
if (gcMultiRow.CurrentCell is InputManCell.GcComboBoxCell)
{
InputManCell.GcComboBoxCell gcComboBoxCell = gcMultiRow.CurrentCell as InputManCell.GcComboBoxCell;
string selectedValue = (string)gcComboBoxCell.Value;
int selectedIndex = -1;
if (!string.IsNullOrEmpty(selectedValue))
{
for (int i = 0; i < gcComboBoxCell.Items.Count; i++)
{
if (gcComboBoxCell.Items[i].Text == selectedValue)
{
selectedIndex = i;
break;
}
}
}
MessageBox.Show(string.Format("選択されているインデックス: {0}", selectedIndex));
}
else
{
MessageBox.Show("現在のセルはGcComboBoxCellではありません");
}
次のコードは拡張メソッドとして、InputMan for Windows Forms 5.0J と同じインタフェースで実装する例です。拡張メソッドを使用するには、Visual Studio 2008 が必要です。
[Visual Basic]
Imports System.Runtime.CompilerServices
Imports InputManCell = GrapeCity.Win.MultiRow.InputMan
Public Module InputManCellListItemCollectionExtension
<Extension()> _
Public Function FindString(ByVal items As InputManCell.ListItemCollection, ByVal s As String, ByVal startIndex As Integer, ByVal findTargetColumnIndex As Integer) As Integer
Dim selectedIndex As Integer = -1
If Not String.IsNullOrEmpty(s) Then
For i As Integer = startIndex To items.Count - 1
If DirectCast(items(i).SubItems(findTargetColumnIndex).Value, String) = s Then
selectedIndex = i
Exit For
End If
Next
End If
Return selectedIndex
End Function
End Module
Imports InputManCell = GrapeCity.Win.MultiRow.InputMan
Public Module InputManCellListItemCollectionExtension
<Extension()> _
Public Function FindString(ByVal items As InputManCell.ListItemCollection, ByVal s As String, ByVal startIndex As Integer, ByVal findTargetColumnIndex As Integer) As Integer
Dim selectedIndex As Integer = -1
If Not String.IsNullOrEmpty(s) Then
For i As Integer = startIndex To items.Count - 1
If DirectCast(items(i).SubItems(findTargetColumnIndex).Value, String) = s Then
selectedIndex = i
Exit For
End If
Next
End If
Return selectedIndex
End Function
End Module
[C#]
using InputManCell = GrapeCity.Win.MultiRow.InputMan;
public static class InputManCellListItemCollectionExtension
{
public static int FindString(this InputManCell.ListItemCollection items, string s, int startIndex, int findTargetColumnIndex)
{
int selectedIndex = -1;
if (!string.IsNullOrEmpty(s))
{
for (int i = startIndex; i < items.Count; i++)
{
if ((string)items[i].SubItems[findTargetColumnIndex].Value == s)
{
selectedIndex = i;
break;
}
}
}
return selectedIndex;
}
}
public static class InputManCellListItemCollectionExtension
{
public static int FindString(this InputManCell.ListItemCollection items, string s, int startIndex, int findTargetColumnIndex)
{
int selectedIndex = -1;
if (!string.IsNullOrEmpty(s))
{
for (int i = startIndex; i < items.Count; i++)
{
if ((string)items[i].SubItems[findTargetColumnIndex].Value == s)
{
selectedIndex = i;
break;
}
}
}
return selectedIndex;
}
}
この場合の使用例は次のようになります。
[Visual Basic]
Imports GrapeCity.Win.MultiRow
Imports InputManCell = GrapeCity.Win.MultiRow.InputMan
Dim gcMultiRow As GcMultiRow = Me.GcMultiRow1
If TypeOf gcMultiRow.CurrentCell Is InputManCell.GcComboBoxCell Then
Dim gcComboBoxCell As InputManCell.GcComboBoxCell = TryCast(gcMultiRow.CurrentCell, InputManCell.GcComboBoxCell)
Dim selectedValue As String = DirectCast(gcComboBoxCell.Value, String)
Dim selectedIndex As Integer = gcComboBoxCell.Items.FindString(selectedValue, 0, 0)
MessageBox.Show(String.Format("選択されているインデックス: {0}", selectedIndex))
Else
MessageBox.Show("現在のセルはGcComboBoxCellではありません")
End If
Imports InputManCell = GrapeCity.Win.MultiRow.InputMan
Dim gcMultiRow As GcMultiRow = Me.GcMultiRow1
If TypeOf gcMultiRow.CurrentCell Is InputManCell.GcComboBoxCell Then
Dim gcComboBoxCell As InputManCell.GcComboBoxCell = TryCast(gcMultiRow.CurrentCell, InputManCell.GcComboBoxCell)
Dim selectedValue As String = DirectCast(gcComboBoxCell.Value, String)
Dim selectedIndex As Integer = gcComboBoxCell.Items.FindString(selectedValue, 0, 0)
MessageBox.Show(String.Format("選択されているインデックス: {0}", selectedIndex))
Else
MessageBox.Show("現在のセルはGcComboBoxCellではありません")
End If
[C#]
using GrapeCity.Win.MultiRow;
using InputManCell = GrapeCity.Win.MultiRow.InputMan;
GcMultiRow gcMultiRow = this.gcMultiRow1;
if (gcMultiRow.CurrentCell is InputManCell.GcComboBoxCell)
{
InputManCell.GcComboBoxCell gcComboBoxCell = gcMultiRow.CurrentCell as InputManCell.GcComboBoxCell;
string selectedValue = (string)gcComboBoxCell.Value;
int selectedIndex = gcComboBoxCell.Items.FindString(selectedValue, 0, 0);
MessageBox.Show(string.Format("選択されているインデックス: {0}", selectedIndex));
}
else
{
MessageBox.Show("現在のセルはGcComboBoxCellではありません");
}
using InputManCell = GrapeCity.Win.MultiRow.InputMan;
GcMultiRow gcMultiRow = this.gcMultiRow1;
if (gcMultiRow.CurrentCell is InputManCell.GcComboBoxCell)
{
InputManCell.GcComboBoxCell gcComboBoxCell = gcMultiRow.CurrentCell as InputManCell.GcComboBoxCell;
string selectedValue = (string)gcComboBoxCell.Value;
int selectedIndex = gcComboBoxCell.Items.FindString(selectedValue, 0, 0);
MessageBox.Show(string.Format("選択されているインデックス: {0}", selectedIndex));
}
else
{
MessageBox.Show("現在のセルはGcComboBoxCellではありません");
}
次期バージョン InputManCell for Windows Forms 6.0J では、新たに GcComboBox.FindString メソッドが提供されています。
関連情報
この文書は、以前は次のFAQ IDで公開されていました : 11734