2017-02-24 65 views
-1

我有2個列表,其中包含文本框和組合框。創建 這裏的文本框和組合框:如何在1個foreach語句中列出2個列表

System.Windows.Controls.TextBox newTxt = new TextBox(); 
System.Windows.Controls.ComboBox newcombo = new ComboBox(); 
// Add Textbox 
newTxt.Text = col.Field<String>("ColumnName"); 
newTxt.Name = col.Field<String>("ColumnName"); 
newTxt.Width = 110; 

// Add Combobox 
newcombo.Items.Add(myReader.GetDataTypeName(n)); 
newcombo.Items.Add("INT"); 
newcombo.SelectedItem = myReader.GetDataTypeName(n); 
newcombo.Name = myReader.GetDataTypeName(n); 
newcombo.Width = 90; 

在這裏,我創建了兩個列表:

List<TextBox> textboxes = sp.Children.OfType<TextBox>().ToList(); 
List<ComboBox> comboboxes = sp.Children.OfType<ComboBox>().ToList(); 

現在我想要一個MySQL查詢編輯列。所以,我需要的ColumnName(textbox.Name)和數據類型(combobox.SelectedItem) 我需要這一個foreach:

foreach (var count in textboxes) 
{ 
    var selected = "i dont know"; 
    config conf = new config(); 
    db_connection(); 
    MySqlCommand cmd = new MySqlCommand(); 
    cmd.CommandText = "ALTER TABLE firmenkunden CHANGE COLUMN " + count.Name + " " + count.Text + " " + selected + " NOT NULL"; 
    MessageBox.Show(cmd.CommandText); 
} 

的文本框,我可以count.Name獲取名稱和的文本帶有count.Text的文本框。但是如何在這個foreach中從組合框中獲取.SelectedItem?

回答

1

,如果您有相同數量的文本框和組合框,可以直通名單使用索引,這樣的循環:

for (int i=0; i< textboxes.Count; i++) 
{ 
    //with i you can access specific textbox from one list and combo from another 
    var tb = textboxes[i]; 
    var cb = comboboxes[i]; 
} 
+0

我得到一個錯誤:使用非特定變量「i」。 – xKushGene

+0

對不起,我已經編輯了我的答案,它應該是'for(int i = 0; i Nino

+0

是的,我看到了。 thx爲你的幫助:)它解決了我的問題 – xKushGene

0

你可以嘗試一個列表類型用戶控件。 Sinds Textbox和Combobox從UserControl繼承。

而您使用的唯一屬性是:名稱和文本這些屬性來自基本UserControl。

// Add Textbox 
TextBox newTxt = new TextBox(); 
newTxt.Text = col.Field<String>("ColumnName"); 
newTxt.Name = col.Field<String>("ColumnName"); 
newTxt.Width = 110; 

// Add Combobox 
ComboBox newcombo = new ComboBox(); 
newcombo.Items.Add(myReader.GetDataTypeName(n)); 
newcombo.Items.Add("INT"); 
newcombo.SelectedItem = myReader.GetDataTypeName(n); 
newcombo.Name = myReader.GetDataTypeName(n); 
newcombo.Width = 90; 

controls.Add(newcombo); 
controls.Add(newTxt); 


var controls = sp.Children.OfType<UserControl>().ToList(); 

foreach (var control in controls) 
{ 
    var selected = "i dont know"; 
    config conf = new config(); 
    db_connection(); 
    MySqlCommand cmd = new MySqlCommand(); 
    cmd.CommandText = "ALTER TABLE firmenkunden CHANGE COLUMN " + control.Name + " " + control .Text + " " + selected + " NOT NULL"; 
    MessageBox.Show(cmd.CommandText); 
} 
0

既然你有一個1:TextBoxesComboBoxes 1的關係,你應該反映在你的代碼關係,以確保你得到你所期望的值。無論是與像一個Dictionary,如果你知道該文本框的值是唯一的:

Dictionary<string, object> d = new Dictionary<string, string>(); 

其中您Value應與combobox值類型替換,和你的textbox字符串應該是Key,那麼你的循環是這樣的:

foreach (var de in d) 
       { 
        var selected = "i dont know"; 
        config conf = new config(); 
        db_connection(); 
        MySqlCommand cmd = new MySqlCommand(); 
        cmd.CommandText = "ALTER TABLE firmenkunden CHANGE COLUMN " 
        + d.Key + " " + d.Value + " " + selected + " NOT NULL"; 
        MessageBox.Show(cmd.CommandText); 
       } 

或者由處理的關係的privateclass嵌套:

private class Strings 
{ 
    public string A {get;set;} 
    public string B {get;set;} 
    public Strings(TextBox a, ComboBox b) 
    { 
     this.A = a.Name; this.B = b.SelectedValue.ToString(); 
    } 
} 

,然後設置是這樣的:

List<Strings> x = new List<strings>(); 
x.add(new Strings(textboxcontrol, combocontrol)) 

,然後通過檢索:

foreach (var o in x) 
       { 
        var selected = "i dont know"; 
        config conf = new config(); 
        db_connection(); 
        MySqlCommand cmd = new MySqlCommand(); 
        cmd.CommandText = "ALTER TABLE firmenkunden CHANGE COLUMN " 
        + o.A + " " + o.B + " " + selected + " NOT NULL"; 
        MessageBox.Show(cmd.CommandText); 
       } 

我會推薦更好的變量和類名,我只是迅速作出反應:)