2009-06-07 6 views
0

Ich habe den Code zu arbeiten, so dass, wenn ich den Knopf "Add More" drücken, fügt es weitere Kombinationsfelder zu dem Formular mit den Namen TagBox1, DayBox2 und so weiter.Drucken von Daten aus dynamisch hinzugefügten Kombinationsfeldern?

Dies ist der Code dafür:

 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; 
    } 

Was ist der beste Weg, um das ausgewählte Element der Boxen mit dem Button Ereignisse hinzugefügt drucken?

Die Art, wie ich die Informationen Druck wurde ich vor in einer Datei wie dies wollte (aus nur einer 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"); 
      } 
     } 

Ich möchte für jede Box diese in der Lage sein zu tun, die jeweils auf einer neuen Linie. So würde es wie sein: Tag: (der Text von box1) Tag: (der Text von box2) Tag: (der Text von box3) und so weiter ... Danke!

Antwort

2

Zwei Möglichkeiten sehe ich sofort, dass Sie dies tun könnte:

Das ist zunächst eine Liste von Verweisen auf diese Kontrollen zu erhalten, wenn Sie sie hinzufügen.

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); 

... 

Dann wird Ihr saveToFile() Methode würde eine Zeile enthalten, die etwa wie folgt aussah:

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"); 
     } 
} 

Die zweite Methode ist zu tun, wie Mike aufgezeigt und die Steuerungsliste gehen für Comboboxen suchen.

2

die Kontrollen Unter der Annahme, leben auf dem Formular, zu Fuß durch alle Steuerelemente auf dem Formular:

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 Ansatz:

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

Es ist ein wenig hacky auf der Controls-Auflistung verlassen , jedoch; vor allem, wenn Sie beginnen, Panels und andere Gruppierungen zu haben. Sie könnten die Steuerelemente zu Ihrer eigenen Sammlung hinzufügen, wie Josh vorschlägt.

Verwandte Themen