Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8225677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:29:42+00:00 2026-06-07T15:29:42+00:00

I have the following code : public partial class ModificarAlimento : Form { private

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:29:43+00:00Added an answer on June 7, 2026 at 3:29 pm

    Try putting

    n.CreateControl();
    

    before this.Controls.Add(), and put

    n.SelectedItem = 
      n.Items
       .Cast<KeyValuePair<string, Nutriente>>()
       .SingleOrDefault(o => o.Key == x.Key);
    

    after call to this.Controls.Add()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public partial class Main : Form { private delegate
I have the following code: public partial class queryTerm : System.Web.UI.UserControl { private static
I have the following code: public partial class Form1 : Form { public BindingList<Class>
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: public class AppDomainArgs : MarshalByRefObject { public string myString;
I have following code public partial class MainWindow : Window { public MainWindow() {
I have the following code: public partial class NewWindow: Window { public static readonly
I have the following Silverlight code: [ScriptableType] public partial class Page : UserControl {
I have the following code... public partial class DownloadFile : System.Web.UI.Page { protected void
I have a master page with the following code: public partial class MasterPage :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.