Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You shouldn't be storing a <code>Form4</code> object in your <code>food</code> class. Your code as it is creates a brand new <code>Form4</code> every time the <code>food</code> object is created. As shown in the line:</p> <pre><code>public Form4 form = new Form4(); </code></pre> <p>This won't actually be shown on screen though as you do nothing else with the form except add items to the <code>ComboBox</code>, which also wouldn't show on the screen.</p> <p>Even if you were to get it shown on the screen, you will still get an error similar to:</p> <pre><code>Form4.comboBox1 is inaccessible due to its protection level </code></pre> <p>This is due to the fact that the <code>ComboBox</code> is created internally with private access modifier. (See <a href="http://msdn.microsoft.com/en-us/library/ms173121.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms173121.aspx</a> for more details).</p> <p>What you need to do is get your existing <code>Form4</code> to ask the <code>food</code> object to populate it's <code>ComboBox</code> by passing the <code>ComboBox</code> to a method on the <code>food</code> object similar to this example (in your <code>Form4</code> code not your <code>food</code> code):</p> <pre><code>private void Form4_Load(object sender, EventArgs e) { food f = new food(); //or however you wanted to create the object f.AddTypesToComboBox(this.comboBox1); } </code></pre> <p>The <code>AddTypesToComboBox</code> method would be defined like this in your <code>food</code> object:</p> <pre><code>public void AddTypesToComboBox(ComboBox box) { for (int i = 0; i &lt; type.Length; i++) { box.Items.Add(type[i]); } } </code></pre> <p>Also, at the moment the function won't actually add anything to the <code>ComboBox</code> as your <code>type</code> array is not being filled with data. You need to call <code>settypes();</code> in the <code>food</code> object's constructors like this:</p> <pre><code>public food(string[] Name1, string[] Name, string[] Type1, int[] kcal) { settypes(); name1 = Name1; name = Name; type = Type1; calories_ref = kcal; } public food() { settypes(); } </code></pre> <p>You will need to remove <code>public Form4 form = new Form4();</code> from your variable declaration section as well as removing the following from your <code>settypes()</code> method:</p> <pre><code>for (int i = 0; i &lt; type.Length; i++) { form.comboBox1.Items.Add(type[i]); } </code></pre> <p>Your <code>settypes()</code> should only fill the data into the array, and not try and add it to the <code>ComboBox</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload