I have the following code :
public partial class ModificarAlimento : Form
{
private Alimento alim;
private Dictionary<string, Nutriente> nutrientes;
public ModificarAlimento(Alimento a, Dictionary<string, Nutriente> nut)
{
InitializeComponent();
this.nutrientes = nut;
alim = a;
int i = 0;
foreach (KeyValuePair<string, CantidadNutrientes> x in alim.Nutrientes)
{
ComboBox n = new ComboBox();
n.DropDownStyle = ComboBoxStyle.DropDownList;
n.Location = new Point(12, 25 * (i + 1) + 80);
n.DataSource = new BindingSource(nutrientes, null);
n.DisplayMember = "Key";
n.ValueMember = "Value";
TextBox cNuts = new TextBox();
cNuts.Location = new Point(150, 25 * (i + 1) + 80);
cNuts.Size = new Size(50, cNuts.Size.Height);
cNuts.Text = x.Value.Cantidad.ToString();
this.Controls.Add(n);
this.Controls.Add(cNuts);
i++;
n.SelectedValue = x.Value.Nutriente;
}
}
private void ModificarAlimento_Load(object sender, EventArgs e)
{
}
}
Now. The problem is here:
n.SelectedValue = x.Value.Nutriente;
Each Alimento (Food) has a dictionary set of CantidadNutrientes, which stores a double value and a Nutriente (Nutrient), which in turn stores a name. So, calling
x.Value.Nutriente
will retrieve the Nutriente in the CantidadNutrientes stored in x.
Why isn’t this working? Any help is appreciated.
EDIT: I’m also trying this
n.SelectedIndex = n.FindStringExact(x.Key);
//and
n.SelectedValue = n.FindStringExact(x.Value.Nutriente.Nombre);
However for some weird reason it works while I debug, but if I don’t go through line for line it doesn’t work at all.
Try putting
before this.Controls.Add(), and put
after call to this.Controls.Add()