文字列「test1」があり、comboBox にはtest1
、test2
、およびが含まれていますtest3
。選択した項目を「test1」に設定するにはどうすればよいですか? つまり、文字列をコンボボックス項目の 1 つに一致させるにはどうすればよいでしょうか?
下の行を考えていましたが、これはうまくいきません。
comboBox1.SelectedText = "test1";
これでうまくいくはずです:
Combox1.SelectedIndex = Combox1.FindStringExact("test1")
Textプロパティを試しましたか? わたしにはできる。
ComboBox1.Text = "test1";
SelectedText プロパティは、コンボ ボックスのテキスト ボックス部分で編集可能なテキストの選択された部分用です。
コンボボックスがデータバインドされていないと仮定すると、フォームの「items」コレクションでオブジェクトのインデックスを見つけ、「selectedindex」プロパティを適切なインデックスに設定する必要があります。
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");
項目が見つからない場合、IndexOf 関数は引数の例外をスローする可能性があることに注意してください。
ComboBox 内の項目が文字列の場合は、次を試すことができます。
comboBox1.SelectedItem = "test1";
私にとってこれはうまくいきました:
foreach (ComboBoxItem cbi in someComboBox.Items)
{
if (cbi.Content as String == "sometextIntheComboBox")
{
someComboBox.SelectedItem = cbi;
break;
}
}
MOD:コンボボックスに設定されたアイテムとして独自のオブジェクトがある場合は、ComboBoxItemを次のようなオブジェクトのいずれかに置き換えます。
foreach (Debitor d in debitorCombo.Items)
{
if (d.Name == "Chuck Norris")
{
debitorCombo.SelectedItem = d;
break;
}
}
SelectedText は、ここに記載されているように、コンボボックスで選択されたアイテムの文字列エディターで実際のテキストを取得または設定します。次のように設定すると、これは編集できなくなります。
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
使用する:
comboBox1.SelectedItem = "test1";
また:
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");
comboBox1.SelectedItem.Text = "test1";
データベースから埋められた een DataTable で ComboBox を埋めました。次に、DisplayMember と ValueMember を設定しました。そして、このコードを使用して、選択したアイテムを設定します。
foreach (DataRowView Row in ComboBox1.Items)
{
if (Row["ColumnName"].ToString() == "Value") ComboBox1.SelectedItem = Row;
}
このソリューションは、私が行ったいくつかの変更を加えたMSDNに基づいています。
文字列の正確または一部を見つけて設定します。
private int lastMatch = 0;
private void textBoxSearch_TextChanged(object sender, EventArgs e)
{
// Set our intial index variable to -1.
int x = 0;
string match = textBoxSearch.Text;
// If the search string is empty set to begining of textBox
if (textBoxSearch.Text.Length != 0)
{
bool found = true;
while (found)
{
if (comboBoxSelect.Items.Count == x)
{
comboBoxSelect.SelectedIndex = lastMatch;
found = false;
}
else
{
comboBoxSelect.SelectedIndex = x;
match = comboBoxSelect.SelectedValue.ToString();
if (match.Contains(textBoxSearch.Text))
{
lastMatch = x;
found = false;
}
x++;
}
}
}
else
comboBoxSelect.SelectedIndex = 0;
}
お役に立てば幸いです!
test1、test2、test3がcomboBox1コレクションに属していると仮定すると、次のステートメントが機能します。
comboBox1.SelectedIndex = 0;
しかし、コードレビューアーとしてそのようなコードを見たら、すべてのメソッドアルゴリズムを再考することをお勧めします.
ComboBox にそのプロパティがありません。SelectedItem または SelectedIndex があります。コンボ ボックスを埋めるために使用したオブジェクトがある場合は、SelectedItem を使用できます。
そうでない場合は、アイテムのコレクション (プロパティ アイテム) を取得し、必要な値が得られるまでそれを繰り返し、それを他のプロパティで使用できます。
それが役に立てば幸い。
_cmbTemplates.SelectedText = "test1"
または多分
_cmbTemplates.SelectedItem= _cmbTemplates.Items.Equals("test1");
ComboBox データバインドにKeyValuePairを使用し、値 で項目を検索したかったので、これは私の場合に機能しました:
comboBox.SelectedItem = comboBox.Items.Cast<KeyValuePair<string,string>>().First(item=> item.Value == "value to match");
値のインデックスを返す関数を作成しました
public static int SelectByValue(ComboBox comboBox, string value)
{
int i = 0;
for (i = 0; i <= comboBox.Items.Count - 1; i++)
{
DataRowView cb;
cb = (DataRowView)comboBox.Items[i];
if (cb.Row.ItemArray[0].ToString() == value)// Change the 0 index if your want to Select by Text as 1 Index
{
return i;
}
}
return -1;
}
combo.Items.FindByValue("1").Selected = true;
ListItem li = DropDownList.Items.FindByValue("13001");
DropDownList.SelectedIndex = ddlCostCenter.Items.IndexOf(li);
あなたのケースでは、使用できます
DropDownList.Items.FindByText("Text");
この方法を試してください、それは私のために働きます:
Combobox1.items[Combobox1.selectedIndex] = "replaced text";
それはうまくいくはずです
Yourcomboboxname.setselecteditem("yourstring");
データベース文字列を設定する場合は、これを使用します
Comboboxname.setselecteditem(ps.get string("databasestring"));