[英]ComboBox SelectValue returns the one before
I have a problem, basically I have a Windows Form application where I create a Theme and a Theme has an Id from an Atelier.我有一个问题,基本上我有一个 Windows 表单应用程序,我在其中创建了一个主题,一个主题有一个来自工作室的 ID。 This id is selected with a
ComboBox
that shows the Id of the Atelier from the Atelier table on my database.这个 id 是用
ComboBox
选择的,它显示了我数据库上 Atelier 表中的 Atelier 的 Id。
Basically I choose an Atelier id with the combobox that is filled like that:基本上我选择了一个带有 combobox 的工作室 id,它的填充如下:
foreach (ListeAteliers listeAt in ListeAteliers.listeAteliers())
{
cbCreaThemeAt.Items.Add(listeAt.idAt);
}
And then the Theme is created with the SelectedIndex
of the Combobox
:然后使用
Combobox
的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());
}
The problem that I have is that on the app, when I choose an Id with the combobox, when i create my object Theme, the id selected is going to be the one before.我遇到的问题是,在应用程序上,当我选择带有 combobox 的 ID 时,当我创建 object 主题时,选择的 ID 将是之前的 ID。
Example: if i have three items: "1","2" and "3" and that I choose "3", the SelectedIndex
is going to be "2"示例:如果我有三个项目:“1”、“2”和“3”并且我选择“3”,则
SelectedIndex
将是“2”
My question is how can I make my SelectedIndex
return the exact value I select on the ComboBox
and why is it happening?我的问题是如何让我的
SelectedIndex
返回 ComboBox 上的ComboBox
的确切值,为什么会这样?
SelectedIndex
returns the 0-based index of the selected item. SelectedIndex
返回所选项目的从 0 开始的索引。 The first item "1"
has index 0 etc. You want to look at the SelectedItem
property that will return the actual selected item "3"
and not its index which is indeed 2.第一项
"1"
的索引为 0,等等。您想查看SelectedItem
属性,该属性将返回实际选定的项"3"
,而不是它的索引(实际上是 2)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.