我有一个问题,基本上我有一个Windows窗体,我在其中创建一个主题,而一个主题具有来自工作室的ID。通过一个组合框选择了此ID,该组合框显示了数据库中Atelier表中的Atelier的ID。
基本上,我选择带有这样填充的组合框的工作室ID:
foreach (ListeAteliers listeAt in ListeAteliers.listeAteliers())
{
cbCreaThemeAt.Items.Add(listeAt.idAt);
}
然后使用组合框的SelectedIndex创建主题:
try
{
int iBd = cbCreaThemeAt.SelectedIndex;
Themes TH;
if (txbIdThemCrea.Text.Length != 0 && txbNomThemeCrea.Text.Length != 0 && cbCreaThemeAt.SelectedIndex != 0)
{
TH = new Themes(txbIdThemCrea.Text, txbNomThemeCrea.Text.ToString(), cbCreaThemeAt.SelectedIndex.ToString());
monAssoc.LesThemes.Add(TH);
TH.ajouterTheme(txbIdThemCrea.Text, txbNomThemeCrea.Text, cbCreaThemeAt.SelectedIndex.ToString());
}
else
{
lblConfirmCreaThemes.Text = "Erreur dans la création";
}
}
catch (Exception ex)
{
MessageBox.Show("Erreur dans la création : " + ex.ToString());
}
我遇到的问题是,在应用程序上,当我使用组合框选择一个ID时,当我创建对象主题时,所选择的ID将会是以前的ID。
示例:如果我有三个项目:“ 1”,“ 2”和“ 3”,而我选择“ 3”,则selectedindex将为“ 2”
我的问题是如何使我的SelectedIndex返回我在组合框中选择的确切值,为什么会发生?
SelectedIndex
返回所选项目的从0开始的索引。您想查看SelectedItem
属性,该属性将返回实际选择的项目"3"
,而不是其索引实际上为2
。