当前位置: 首页 > 知识库问答 >
问题:

组合框项返回索引

倪鹏
2023-03-14

首先,我对我的英语感到抱歉。我是一个使用VS Express 2013的C#初学者程序员。

我认为这是我的简单问题:我有一个组合框(cboMantello),里面有两个项目。然后我有一个按钮,使用所选项目的属性并将它们添加到我的角色统计中。另一个按钮删除该属性。

为了避免用户垃圾邮件,我禁用了第一个按钮,并设置了我的组合框。启用为false。这时问题来了。禁用组合框后,它会返回列表中的第一项,因此如果我选择第二项并按下“装备”按钮,它会添加属性,但组合框会切换到第一项。因此,如果我按下“移除”按钮,代码将移除第一个项目属性。

我不知道如何告诉combobox在启用=假阶段在第二个项目上frezze。

谢谢你的帮助,再次为我的语法“恐怖”感到难过!

这是我的代码:

private void UpdateListaMantelliInventarioUI()
{
    List<Mantello> mantelli = new List<Mantello>();

    foreach (OggettoInventario oggettoInventario in _player.Inventario)
    {
        if (oggettoInventario.Dettagli is Mantello)
        {
            if (oggettoInventario.Quantità > 0)
            {
                mantelli.Add((Mantello)oggettoInventario.Dettagli);
            }
        }
    }

    if (mantelli.Count == 0)
    {
        cboMantello.Enabled = false;
    }
    else
    {
        cboMantello.DataSource = mantelli;
        cboMantello.DisplayMember = "Nome";
        cboMantello.ValueMember = "ID";

        cboMantello.SelectedIndex = 0;
    }
}

private void btMantello_Click(object sender, EventArgs e)
{
    Mantello mantellocorrente = (Mantello)cboMantello.SelectedItem;

    _player.DifesaMagica = (_player.DifesaMagica + mantellocorrente.AggiungeDifesaMagica);
    lblVesteDifesa.Text = "(+" + mantellocorrente.AggiungeDifesaMagica.ToString() + ")";
    toolTip1.SetToolTip(lblVesteDifesa, mantellocorrente.Nome.ToString());

    _player.Mana = (_player.Mana + mantellocorrente.AggiungeMana);
    lblVesteMana.Text = "(+" + mantellocorrente.AggiungeMana.ToString() + ")";
    toolTip1.SetToolTip(lblVesteMana, mantellocorrente.Nome.ToString());

    _player.Evasione = (_player.Evasione + mantellocorrente.AggiungeEvasione);
    lblVesteEvasione.Text = "(+" + mantellocorrente.AggiungeEvasione.ToString() + ")";
    toolTip1.SetToolTip(lblVesteEvasione, mantellocorrente.Nome.ToString());


    btMantello.Enabled = false;
    btTogliMantello.Enabled = true;
    cboMantello.Enabled = false;

    UpdatePlayerStats();
    UpdateListaMantelliInventarioUI();
}

private void btTogliMantello_Click(object sender, EventArgs e)
{
    Mantello mantellocorrente = (Mantello)cboMantello.SelectedItem;
    if (btMantello.Enabled == false)
    {
        btTogliMantello.Enabled = true;
        _player.DifesaMagica = (_player.DifesaMagica - mantellocorrente.AggiungeDifesaMagica);

        lblVesteDifesa.Text = "";
        _player.Mana = (_player.Mana - mantellocorrente.AggiungeMana);

        lblVesteMana.Text = "";
        _player.Evasione = (_player.Evasione - mantellocorrente.AggiungeEvasione);

        lblVesteEvasione.Text = "";

        UpdatePlayerStats();

        btMantello.Enabled = true;
        cboMantello.Enabled = true;
    }

    btTogliMantello.Enabled = false;
}

共有1个答案

贺英悟
2023-03-14

组合框重置的原因是,当您在bntMantello的单击事件中调用UpdateListaMantelliInentarioUI()时,您正在更改其内容,特别是cboMantello. DataSource=mantelli;cboMantello. Seletedindex=0;行。

需要考虑的一些选项:

  • 点击装备/取消装备后,是否需要更新组合框?
  • 如果是,您可以直接从cboMantello. Items添加/删除。
  • 在更新之前,您还可以从cboMantello获取选择索引/项目。更新cboMantello后,您可以遍历项目并更新选择索引/项目

一些代码:

private void UpdateListaMantelliInventarioUI()  
{  
    var previousSelection = cboMantello.SelectedItem;  

    ...

    else
    {
        ...

        if (cboMantello.Items.Contains(previousSelection))
            cboMantello.SelectedItem = previousSelection;
        else
            cboMantello.SelectedIndex = 0;
    }
}
 类似资料:
  • 我有一个带有2个ComboBox的应用程序,我想将用户的选择返回到一个变量中。我该怎么做?这是我的控制器类: 我需要使用这个变量来改变应用程序的工作方式。在“methode”组合框中,我想要一个包含大量文本字段的新窗口。例如,如果用户选择3,它将打开一个包含3个文本字段的新窗口,或者(如果可能,只需在组合框下方添加3个测试字段)谢谢

  • 我有两个组合框。我正在填充这样的两个组合框。组合框1和2的名称是cmbpartyName和cmbprefPT2。 在这里,我从combox1中选择一个值,在它所选的索引更改事件中,如下所示。 由于两个组合框具有相同的值,如何从第二个组合框cmbPrefPT2中删除第一个组合框的选定值?

  • 我想用Javascript实现合并排序作为一种学习经验。我有mergeSort(unsortedArray)函数,它接受一个未经排序的数组,并使用合并排序策略对其进行排序。mergeSort()调用merge(leftArray,rightArray),后者将两个数组合并在一起,得到一个数组。 我认为问题出在merge()函数上。在数组[8,8,7,5,4,6,3,2,1,5,9,8,7,6,5,

  • 说到编程或java,我只是一个完全的初学者。所以一开始,我的计划是使用JavaFX(结合场景构建器)创建一个窗口,在那里我有一个按钮,可以引导我进入另一个窗口,在那里我有一个组合框。我谷歌了几个小时,想找到一种方法来填充组合框,但我找到的所有解决方案都不适合我。这就是为什么我认为我在这里犯了一些错误,我希望你能以某种方式帮助我。或者在列表中给我一个提示,我应该学习/阅读什么才能自己找到解决方案。首

  • 问题内容: 我们正在使用Selenium WebDriver和JBehave在我们的Web应用程序上运行“集成”测试。我有一种方法,可以在表单输入中输入一个值。 但是,当我尝试使用它在下拉列表中选择一个项目时,它(毫无疑问)失败了 java.lang.UnsupportedOperationException:您只能设置作为输入元素的元素的值 如何在组合中选择一个值? 问题答案: 这是怎么做的:

  • 我在后台有< code>ViewModel(实现< code > INotifyPropertyChanged )和类< code>Category,它只有一个< code>string类型的属性。我的ComboBox SelectedItem绑定到类别的实例。当我更改instance的值时,SelectedItem没有更新,Combobox也没有更改。 编辑:代码 组合框: 物业: 我尝试的是: