Reputation: 21
First of all im sorry for my English. Im a beginner programmer in C# using VS Express 2013.
This is my simple problem i think: I have a combobox (cboMantello) with two items inside. Then i have a button that uses the attributes of the selected item and add them into my character stats. Another button removes that attributes.
To avoid Users spam the first button i disabled it and also set my combobox.enabled to false. At this point comes up the problem. On disabling the combobox, it returns on the first item in list, so if i select the second item and press the "equipe" buttom it adds the attributes, but then the combobox switch to the first item.So if i push the "remove" button the code remove the first item attributes.
I dont know how tell combobox to frezze on second item during the enabled = false phase.
Thanks for help, and sry again for my grammatical "horrors" !
Heres my code:
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;
}
Upvotes: 0
Views: 108
Reputation: 795
The reason the Combo Box is resetting is because you are changing its contents when you call UpdateListaMantelliInventarioUI()
in the click event for bntMantello
, specifically the lines cboMantello.DataSource = mantelli;
and cboMantello.SelectedIndex = 0;
.
Some options to consider:
cboMantello.Items
directly. SelectedIndex
/Item
from cboMantello
before you update it. After you update cboMantello
, you could loop through the items and update the SelectedIndex
/Item
.Some code:
private void UpdateListaMantelliInventarioUI()
{
var previousSelection = cboMantello.SelectedItem;
...
else
{
...
if (cboMantello.Items.Contains(previousSelection))
cboMantello.SelectedItem = previousSelection;
else
cboMantello.SelectedIndex = 0;
}
}
Upvotes: 1