组合框选择值返回之前的值

问题描述 投票:0回答:1

我有一个问题,基本上我有一个Windows窗体应用程序,在那里我创建了一个主题和一个主题有一个来自Atelier的Id。这个Id是用一个 ComboBox 在我的数据库的Atelier表中显示了Atelier的Id。

基本上,我选择了一个Atelier id的组合框,就像这样填充。

foreach (ListeAteliers listeAt in ListeAteliers.listeAteliers())
{
    cbCreaThemeAt.Items.Add(listeAt.idAt);
}

然后用 SelectedIndexCombobox:

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将是之前的那个。

例如:如果我有三个项目:"1","2 "和 "3",我选择了 "3",则 SelectedIndex 将是 "2"

我的问题是,如何才能让我的 SelectedIndex 返回我在 ComboBox 为什么会发生这种情况?

c# winforms combobox selectedindex
1个回答
1
投票

SelectedIndex 返回所选项目的基于0的索引。第一个项目 "1" 有指数0等。你要看的是 SelectedItem 属性,将返回实际选择的项目 "3" 而不是它的指数,它的指数确实是2。

© www.soinside.com 2019 - 2024. All rights reserved.