views:

58

answers:

2

Hi there, I have gotten the code to work so that when I press the button, "Add More", it adds more combo boxes to the form with the names dayBox1, dayBox2, and so on.

This is the code for that:

        private void addMoreBtn_Click(object sender, EventArgs e)
    {
        //Keep track of how many dayBoxes we have
        howMany++;

        //Make a new instance of ComboBox
        ComboBox dayBox = new System.Windows.Forms.ComboBox();

        //Make a new instance of Point to set the location
        dayBox.Location = new System.Drawing.Point(Left, Top);
        dayBox.Left = 13;
        dayBox.Top = 75 + dayLastTop;

        //Set the name of the box to dayBoxWhateverNumberBoxItIs
        dayBox.Name = "dayBox" + howMany.ToString();

        //The TabIndex = the number of the box
        dayBox.TabIndex = howMany;

        //Make it visible
        dayBox.Visible = true;

        //Set the default text
        dayBox.Text = "Pick One";

        //Copy the items of the original dayBox to the new dayBox
        for (int i = 0; i < dayBoxO.Items.Count; i++)
        {
            dayBox.Items.Add(dayBoxO.Items[i]);
        }

        //Add the control to the form
        this.Controls.Add(dayBox);

        //The location of the last box's top with padding
        dayLastTop = dayBox.Top - 49;
    }

What is the best way to print the selected member of the boxes added with the button event?

The way I was printing the information I wanted to a file before was like this (from only one box):

public void saveToFile()
        {     
            FileInfo t = new FileInfo("Workout Log.txt");
            StreamWriter Tex = t.CreateText();
            Tex.WriteLine("---------------Workout Buddy Log---------------");
            Tex.WriteLine("------------------- " + DateTime.Now.ToShortDateString() + " ------------------");
            Tex.Write(Tex.NewLine);
            if (dayBoxO.Text != "Pick One")
            {
                Tex.WriteLine("Day: " + dayBoxO.Text);
            }
            else
            {
                Tex.WriteLine("Day: N/A");
            }
        }

I want to be able to do this for every box, each on a new line. So, it would be like: Day: (the text of box1) Day: (the text of box2) Day: (the text of box3) and so on... Thanks!

+2  A: 

Assuming the controls live on the form, walk through all the controls on the form:

var boxes =  from Control c in this.Controls
             where c.GetType() == typeof(System.Windows.Forms.ComboBox)
             select c;

StringBuilder sb = new StringBuilder();
foreach (Control c in boxes)
{
    sb.AppendLine(c.Text); // ...
}

Non-Linq approach:

StringBuilder sb = new StringBuilder();
foreach (Control c in this.Controls)
{
    if (c.GetType() == typeof(System.Windows.Forms.ComboBox))
    {
     sb.AppendLine(c.Text); // ...
    }
}

It's a little hacky to rely on the Controls collection, however; especially when you start to have panels and other groupings. You might consider adding the controls to your own collection, as Josh suggests.

Michael Petrotta
+2  A: 

Two ways I see immediately that you could do this:

The first is to maintain a list of references to those controls when you are adding them.

private List<ComboBox> _comboBoxList = new List<ComboBox>();

...

//Make a new instance of ComboBox
ComboBox dayBox = new System.Windows.Forms.ComboBox();

//Add your combobox to the master list
_comboBoxList.Add(dayBox);

...

Then your saveToFile() method would contain a line that looked something like this:

foreach(var box in _comboBoxList)
{
      Tex.Write(Tex.NewLine);
      if (box.Text != "Pick One")
      {
          Tex.WriteLine("Day: " + box.Text);
      }
      else
      {
          Tex.WriteLine("Day: N/A");
      }
}

The second method is to do as Mike pointed out and walk the controls list looking for your ComboBoxes.

Josh