如何编写foreach方法来清除文本框? [英] how to write foreach method for clear the textbox ?

查看:96
本文介绍了如何编写foreach方法来清除文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   void  clear()
{
// GetFarmer();
txtLiftingdate.Text = ;
txtBirds.Text = ;
txtAvgweight.Text = ;
txtTweight.Text = ;
txtRate.Text = ;
txtTamount.Text = ;
txtAmountRec.Text = ;
txtAmountdue.Text = ;
txtCheque.Text = ;
GetTraderDetails();
// txttrader.Text =;
txtrecdate.Text = < span class =code-string> ;
txtmobile.Text = ;
txtvhno.Text = ;
txtdcno.Text = ;
txtremark.Text = ;
txttrno.Text = ;
txtcashrecby.Text = ;

}







我写这个逻辑没有:时间..但我想用foreach循环编写简单的代码..pleaes帮助我

解决方案

  void  ClearAllControlsRecursive(控件容器)
{
foreach var 在 container.Controls中控制
{



if (control TextBox)
{
((TextBox)control).Text = 字符串 .Empty;
}


}
}



ClearAllControlsRecursive(panel1);





和你这个方法一样,你应该将所有文本框放在面板控件中。另外明智的是它不起作用


您需要编写创建和插入代码的代码,而不是使用设计器创建这么多文本框。你可以使用循环。要初始化和设置它们,您可以使用一些资源文件(嵌入在可执行模块中,* .resx)。类似

  int  TextBoxCount =  //   ...  
TextBox [] myTextBoxes = new TextBox [TextBoxCount];
Label [] myLabels = new 标签[TextBoxCount];
控制父= // ...某些控件在哪里插入

for int index = 0 ; index < myTextBoxes.Count; ++ index){
TextBox [index] = new TextBox();
TextBox [index] .Left = // ...
TextBox [ index] .Top = // ...取决于索引和文本框的高度,如果是垂直列的话
// ...
TextBox [index]。宽度= // 一些常量
// 使用标签做同样的事情,从某处读取标签文本
// ...
// and this是如何插入:
TextBox [index] .Parent = control;
// 或者,同样的事情,或者:
// parent.Controls.Add(TextBox [index]);
// ...
}



然后 - 瞧! - 你可以在循环中做很多操作:

  foreach  var  textBox  in  TextBoxes)
textBox.Text = string .Empty; // please,string.Empty,not not



你明白了吗?



在最坏的情况下,你可以用设计师创建的参考文献创建一个数组,只需一次:

 TextBox [] myTextBoxes =  new  TextBox [] {txtLiftingdate,txtBirds, / *   ... * /  txtcashrecby,}; 



然后使用相同的东西 foreach 循环。



-SA


< blockquote>阅读: http:// stackoverflow .com / questions / 4811229 /如何清除文本中的所有文本框 [ ^ ]


public void clear()
    {
        //GetFarmer();
        txtLiftingdate.Text = "";
        txtBirds.Text = "";
        txtAvgweight.Text = "";
        txtTweight.Text = "";
        txtRate.Text = "";
        txtTamount.Text = "";
        txtAmountRec.Text = "";
        txtAmountdue.Text = "";
        txtCheque.Text = "";
        GetTraderDetails();
        //txttrader.Text = "";
        txtrecdate.Text = "";
        txtmobile.Text = "";
        txtvhno.Text = "";
        txtdcno.Text = "";
        txtremark.Text = "";
        txttrno.Text = "";
        txtcashrecby.Text = "";
       
    }




I write this logic no:of times..but i want to write simple code using foreach loop ..pleaes help me

解决方案

void ClearAllControlsRecursive(Control container)
{
    foreach (var control in container.Controls)
    {



        if (control is TextBox)
        {
            ((TextBox)control).Text = String.Empty;
        }


    }
}



     ClearAllControlsRecursive(panel1);



and u cal this method like ,you should place all the textboxes in panel control.other wise it's not work


Instead of using the designer creating so many text boxes, you need to write code creating and inserting them. The you would be able to use the loop. To initialize and setup them, you could use some resource file (embedded in the executable module, *.resx), for example. Something like

int TextBoxCount = //...
TextBox[] myTextBoxes = new TextBox[TextBoxCount];
Label[] myLabels = new Label[TextBoxCount];
Control parent = //... some control where you insert them

for (int index = 0; index < myTextBoxes.Count; ++index) {
    TextBox[index] = new TextBox();
    TextBox[index].Left = //...
    TextBox[index].Top = //... something depending on index and Text box's height, in case of vertical column of them
    //...
    TextBox[index].Width = // some constant
    // do same thing with a label, read the label text from somewhere
    //...
    // and this is how to insert:
    TextBox[index].Parent = control;
    // or, same thing, alternatively:
    // parent.Controls.Add(TextBox[index]);
    //...
}


Then — voila! — you can do many operations in loops:

foreach(var textBox in TextBoxes)
    textBox.Text = string.Empty; // please, string.Empty, not ""


Are you getting the idea?

In worst case, you can create an array from your designer-created references, just once:

TextBox[] myTextBoxes = new TextBox[] { txtLiftingdate, txtBirds, /* ... */ txtcashrecby, };


and then do the same thing using foreach loops.

—SA


Read this : http://stackoverflow.com/questions/4811229/how-to-clear-the-text-of-all-textboxes-in-the-form[^]


这篇关于如何编写foreach方法来清除文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆