简体   繁体   English

for循环调用textbox.text,名称中包含变量

[英]for-loop call textbox.text with variable inside the name

How can I call textbox.text with the "I" variable from a for-loop? 如何在for循环中使用“ I”变量调用textbox.text? I have 13 Textboxes and do exactly 13 times the same thing (see my sourcecode). 我有13个文本框,并且做相同的事情正好13次(请参阅我的源代码)。 How can I do it simpler with a for-loop? 如何使用for循环更简单? TempRechnung.ReplaceText("{1_ANR}", "txtArtikel_" + i + "_Nr.Text"); Does actually not work... 实际上不起作用...

My current Sourcecode: 我当前的源代码:

// #1
if (txtArtikel_1_Epreis.Text != string.Empty)
{
    decimal Artikel_1_GP = Convert.ToDecimal(txtArtikel_1_Epreis.Text) * Convert.ToInt32(txtArtikel_1_Menge.Text);
    decimal Artikel_1_EP = Convert.ToDecimal(txtArtikel_1_Epreis.Text);
    TempRechnung.ReplaceText("{1_ANR}", txtArtikel_1_Nr.Text);
    TempRechnung.ReplaceText("{1_BEZ}", txtArtikel_1_Bez.Text);
    TempRechnung.ReplaceText("{1_ME}", txtArtikel_1_Menge.Text);
    TempRechnung.ReplaceText("{1_EP}", Artikel_1_EP.ToString("C"));
    TempRechnung.ReplaceText("{1_GP}", Artikel_1_GP.ToString("C"));
}
else
{
    TempRechnung.ReplaceText("{2_ANR}", "");
    TempRechnung.ReplaceText("{2_BEZ}", "");
    TempRechnung.ReplaceText("{2_ME}", "");
    TempRechnung.ReplaceText("{2_EP}", "");
    TempRechnung.ReplaceText("{2_GP}", "");
}

// #2
if (txtArtikel_2_Epreis.Text != string.Empty)
{
    decimal Artikel_2_GP = Convert.ToDecimal(txtArtikel_2_Epreis.Text) * Convert.ToInt32(txtArtikel_2_Menge.Text);
    decimal Artikel_2_EP = Convert.ToDecimal(txtArtikel_2_Epreis.Text);
    TempRechnung.ReplaceText("{2_ANR}", txtArtikel_2_Nr.Text);
    TempRechnung.ReplaceText("{2_BEZ}", txtArtikel_2_Bez.Text);
    TempRechnung.ReplaceText("{2_ME}", txtArtikel_2_Menge.Text);
    TempRechnung.ReplaceText("{2_EP}", Artikel_2_EP.ToString("C"));
    TempRechnung.ReplaceText("{2_GP}", Artikel_2_GP.ToString("C"));
}
else
{
    TempRechnung.ReplaceText("{2_ANR}", "");
    TempRechnung.ReplaceText("{2_BEZ}", "");
    TempRechnung.ReplaceText("{2_ME}", "");
    TempRechnung.ReplaceText("{2_EP}", "");
    TempRechnung.ReplaceText("{2_GP}", "");
}

I would suggest creating Artikel user control, which will have following textboxes inside: Epreis , Nr , Bez and Menge . 我建议创建Artikel用户控件,其中将包含以下文本框: EpreisNrBezMenge Then place 13 of these user controls on your form and loop them. 然后将这些用户控件中的13个放在您的窗体上并循环。 You can create collection of artikel controls manually, or you can query for them: 您可以手动创建artikel控件的集合,也可以查询它们:

var artikels = Controls.OfType<ArtikelControl>();

Here is WinForms sample of ArtikelControl: 这是ArtikelControl的WinForms示例:

public partial class ArtikelControl : UserControl
{
    public ArtikelControl()
    {
        InitializeComponent();
    }

    public decimal GP 
    {
        get { return Decimal.Parse(epreisTextBox.Text) *
                     Int32.Parse(mengeTextBox.Text); } // add validation
    }
    public decimal EP { get { return Decimal.Parse(epreisTextBox.Text); } }
    public string Nr { get { return nrTextBox.Text; } }
    public string Bez { get { return bezTextBox.Text; } }
    public string Menge { get { return mengeTextBox.Text; } }
    public bool HasEpreis
    { 
        get { return !String.IsNullOrEmpty(epreisTextBox.Text); } 
    }
}

Now if you have collection of these controls, you can loop it and get values from each control: 现在,如果您拥有这些控件的集合,则可以循环它并从每个控件中获取值:

foreach(var artikel in artikels)
{
    if (artikel.HasEpreis)
    {
        TempRechnung.ReplaceText("{2_ANR}", artikel.Nr);
        TempRechnung.ReplaceText("{2_BEZ}", artikel.Bez);
        // ...
    }
    else
    {
        // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM