locked
GetEnumerator error RRS feed

  • Question

  • hi guys,

    I am unfamilar with Enumerators. I did some searching but none of what I found seemed releveant to my error

    foreach statement cannot operate on variables of type 'System.Windows.Forms.GroupBox' because 'System.Windows.Forms.GroupBox' does not contain a public definition for 'GetEnumerator' 

    here is my code

    public void savedata() 
            { 
                SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
                Stream myStream;  
                saveFileDialog1.Filter = "All files (*.*)|*.*|txt files (*.txt)|*.txt"
                saveFileDialog1.FilterIndex = 2; 
                saveFileDialog1.RestoreDirectory = true
                saveFileDialog1.AddExtension = true;// Adds a extension if the user does not 
                saveFileDialog1.Title = "Where do you want to save the script?"
     
                List<string> textbox_values1 = new List<string>(); 
                List<string> textbox_values2 = new List<string>();         
     
                foreach(TextBox txt in groupBox1) ERROR OCCURS HERE
                { 
                    textbox_values1.Add(txt.Text.ToString()); 
                } 
     
                foreach (TextBox txt in groupBox2) AND HERE
                { 
                    textbox_values2.Add(txt.Text.ToString()); 
                } 
     
                if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
                { 
                    string str = string.Empty; 
                    if ((myStream = saveFileDialog1.OpenFile()) != null
                    { 
                        StreamWriter wText = new StreamWriter(myStream); 
                         
                        for (int i = 0; i < textbox_values1.Count; i++) 
                        { 
                            str = textbox_values1[i]; 
                            wText.WriteLine(str); 
                        } 
     
                        for (int i = 0; i < textbox_values2.Count; i++) 
                        { 
                            str = textbox_values2[i]; 
                            wText.WriteLine(str); 
                        } 
     
                        wText.Close(); 
                        myStream.Close(); 
                    } 
     
                } 
     
     
     
            } 

    Friday, March 27, 2009 7:50 PM

Answers

  • Your foreach is incorrect. If you have textboxes in the groupboxes you should do this:

    foreach (TextBox tb in groupBox1.Controls.OfType<TextBox>())
    {

    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Edited by JohnGrove Friday, March 27, 2009 7:56 PM `
    • Marked as answer by Deadlocke Friday, March 27, 2009 8:00 PM
    Friday, March 27, 2009 7:55 PM

All replies

  • Your foreach is incorrect. If you have textboxes in the groupboxes you should do this:

    foreach (TextBox tb in groupBox1.Controls.OfType<TextBox>())
    {

    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Edited by JohnGrove Friday, March 27, 2009 7:56 PM `
    • Marked as answer by Deadlocke Friday, March 27, 2009 8:00 PM
    Friday, March 27, 2009 7:55 PM
  • Since you are iterating twice, you should refactor that as follows:

    List<StringHolder> list = new List<StringHolder>();
    foreach (GroupBox gb in this.Controls.OfType<GroupBox>())
    {
         foreach (TextBox tb in gb.Controls.OfType<TextBox>())     
               list.Add(new StringHolder(gb.Name, tb.Text);       
    }

    public class StringHolder
    {
          public string GroupBoxName { get; private set; }
          public string TextValue { get; private set; }
          public StringHolder(string groupBoxName, string textValue)
         {
              GroupBoxName = groupBoxName;
              TextValue = textValue;
         }
    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Edited by JohnGrove Friday, March 27, 2009 8:27 PM typo
    Friday, March 27, 2009 8:18 PM