2016-11-22 2 views
0

In meinem Hauptformular habe ich eine Textbox nämlich textBoxTotalTotal und ich möchte alle dynamisch hinzugefügt Textfeld hinzugefügt/Summe in meinem textBoxTotalTotal Wie kann ich das erreichen?
In meiner Hauptform habe ich dies: textBoxTotalTotal
Da ist in meinem User Control habe ich dieseFügen Sie dynamisch hinzugefügte Textbox Wert von Benutzersteuerung zu Hauptformular

public void textBoxTranspo_TextChanged(object sender, EventArgs e) 
    { 
    int intTranspo = 0, intBoxDaily = 0; 
    if (int.TryParse(textBoxTranspo.Text, out intTranspo) && int.TryParse(textBoxDaily.Text, out intBoxDaily)) 
    textBoxTotalAmount.Text = (intTranspo + intBoxDaily).ToString(); 
    } 

und

public void textBoxDaily_TextChanged(object sender, EventArgs e) 
     { 
     int intTranspo = 0, intBoxDaily = 0; 
     if (int.TryParse(textBoxTranspo.Text, out intTranspo) && int.TryParse(textBoxDaily.Text, out intBoxDaily)) 
     textBoxTotalAmount.Text = (intTranspo + intBoxDaily).ToString(); 
     } 

Beachten Sie, dass ich dynamisch, dass die User Control in der per Taste am Hinzufügen Hauptform, wie oft ich mag. Also die textBoxTotalTotal sollte nur hinzufügen, ob eine neue 2 Textbox auftaucht.

Antwort

0

Unter der Annahme, dass Benutzersteuerelemente Eltern sind Form1 und Benutzerkontrolle Typ MyUserControl: (Sie die richtig Eltern setzen sollen, und Benutzersteuertyp)

MyUserControl[] controls = Form1.Controls.OfType<MyUserControl>().ToArray(); 
int Total = 0; 
for(int i=0;i<controls.Length;i++){ 
    controls[i].Controls.OfType<TextBox>().ToList() 
    .ForEach(txt => Total += int.Parse(txt.Text)); 
} 

Wenn Sie es von dem Usercontrol tun sich dann:

MyUserControl[] controls = Parent.Controls.OfType<MyUserControl>().ToArray(); 
int Total = 0; 
for(int i=0;i<controls.Length;i++){ 
    controls[i].Controls.OfType<TextBox>().ToList() 
    .ForEach(txt => Total += int.Parse(txt.Text)); 
} 
+0

Hallo, wie kann ich feststellen, welche Eltern von UserControl ist? – Noobster

+0

usercontrol.Parent? – TaW

Verwandte Themen