2016-10-15 6 views
-1

Ich erstelle eine Reihe von ComboBoxes basierend auf Benutzereingaben. Ich erstelle die Boxen ganz gut, aber wenn es darum geht, den Text in ihnen zu überprüfen, kämpfe ich.C# Zugriff auf dynamische Steuerelemente Windows Forms

Ich dachte daran, sie vielleicht in einer IList zu speichern, aber das schien bisher nicht zu funktionieren. Das Ziel ist es, den Text von allen auf Knopfdruck zu ändern, aber nach mehreren Versuchen werde ich frustriert.

IList<ComboBox> comboBoxes = new List<ComboBox>(); 


private void AddComboBox(int i) 

    { 
     var comboBoxStudentAttendance = new ComboBox();   
     comboBoxStudentAttendance.Top = TopMarginDistance(i); 

     comboBoxStudentAttendance.Items.Add(""); 
     comboBoxStudentAttendance.Items.Add("Present"); 
     comboBoxStudentAttendance.Items.Add("Absent"); 
     comboBoxStudentAttendance.Items.Add("Late"); 
     comboBoxStudentAttendance.Items.Add("Sick"); 
     comboBoxStudentAttendance.Items.Add("Excused"); 

     comboBoxes.Add(comboBoxStudentAttendance); 
     this.Controls.Add(comboBoxStudentAttendance); 
    } 

Ich versuchte das folgende, aber ohne Erfolg.

private void DistributeAttendanceButton_Click(object sender, EventArgs e) 
    { 

     for (int i = 0; i < sampleNum; i++) 
     { 
      switch (MasterComboBox.Text) 
      { 

      case "Present": 


        comboBoxes.ElementAt(i).Text = "Present"; 
        break; 
       } 

      } 
    } 
+1

Warum Sie eine IList brauchen. Nur eine Liste sollte funktionieren. Das mache ich die ganze Zeit. Dann benutze einfach comboBoxes [i] .Text = "Present"; – jdweng

+0

Ich versuchte diese Änderungen und hatte keinen Erfolg, nicht sicher, ob ich sie falsch implementiere. – ghh1415

Antwort

0

Versuchen Sie, diese

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     const int TOP_MARGIN = 10; 
     const int LEFT_MARGIN = 10; 
     const int WIDTH = 200; 
     const int HEIGHT = 10; 
     const int SPACE = 15; 
     const int NUMBER_OF_BOXES = 10; 
     public Form1() 
     { 
      InitializeComponent(); 
      MasterComboBox.Text = "Present"; 
      for (int i = 0; i < NUMBER_OF_BOXES; i++) 
      { 
       AddComboBox(i); 
      } 
     } 

     List<ComboBox> comboBoxes = new List<ComboBox>(); 


     private void AddComboBox(int i) 
     { 
      var comboBoxStudentAttendance = new ComboBox(); 
      comboBoxStudentAttendance.Top = TOP_MARGIN + i * (SPACE + HEIGHT); 
      comboBoxStudentAttendance.Left = LEFT_MARGIN; 
      comboBoxStudentAttendance.Width = WIDTH; 
      comboBoxStudentAttendance.Height = HEIGHT; 

      comboBoxStudentAttendance.Items.Add(""); 
      comboBoxStudentAttendance.Items.Add("Present"); 
      comboBoxStudentAttendance.Items.Add("Absent"); 
      comboBoxStudentAttendance.Items.Add("Late"); 
      comboBoxStudentAttendance.Items.Add("Sick"); 
      comboBoxStudentAttendance.Items.Add("Excused"); 

      comboBoxes.Add(comboBoxStudentAttendance); 
      this.Controls.Add(comboBoxStudentAttendance); 

     } 

     private void DistributeAttendanceButton_Click(object sender, EventArgs e) 
     { 

      for (int i = 0; i < comboBoxes.Count; i++) 
      { 
       switch (MasterComboBox.Text) 
       { 

        case "Present": 


         comboBoxes[i].Text = "Present"; 
         break; 
       } 

      } 
     } 
    } 
} 
+0

Das funktioniert, muss es nur in das Projekt implementieren, was hat das anders gemacht? – ghh1415

+0

Und vielen Dank für Ihre Hilfe! – ghh1415

+0

kann ich nicht sagen. Die einzigen Dinge, die ich änderte, waren die beiden for loops und comboBoxes [i] – jdweng

Verwandte Themen