如何发现哪个动态创建的文本框已更改 [英] How do I discover which dynamically created textbox was changed

查看:82
本文介绍了如何发现哪个动态创建的文本框已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表单,我在第二个表单上动态创建文本框。我想知道是否有任何这些框中的文本被更改,以便我可以在XML文件中更改它将文本框文本写入。

更新:

我了解如何从动态创建的文本框中创建和获取文本。它正在弄清楚哪一个已被更改,然后在我无法弄清楚的XML文件中更改它。



我尝试了什么:



i尝试使用foreach语句,但不确定如何找到哪一个被更改

I have 2 forms and i am dynamically creating textboxes on the second form. i want to know if the text in any of those boxes were changed so that i can change it in the XML file it writes the textbox text to.
Update:
I understand how to create and get the text from the dynamically created textboxes.It is figuring out which one was changed and then changing it in the XML file that i cant figure out.

What I have tried:

i tried using a foreach statement but wasnt sure how to find which one was changed

推荐答案

var totalTextBox = 5;

// Create TextBox dynamically
for ( int i = 0 ; i < totalTextBox ; i++ )
{
   TextBox tb = new TextBox();
   tb.ID = "tb" + i;
   tb.TextChanged += TextBox_TextChanged;
}

// All TextBox point to same event
private void TextBox_TextChanged(object sender, EventArgs e)
{
   // sender points to modified TextBox
   // All you need to do is cast it
   TextBox modified = (TextBox)sender;
}


我认为你会覆盖文本框上的.Change事件?我们在谈论哪种语言?假设C#,你会为它创建一个eventhandler,如下所示:



I presume that you would override the .Change event on the textboxes? Which language are we talking about? Presuming C#, you would create an eventhandler for it, like this:

this.textbox.change += new EventHandler (myFunctionName);





然后在你的代码中:





Then later in your code:

void myFunctionName(object Sender, EventArgs e) { do something here; }





这就是我的建议。



That's what I'd recommend.


我实际上想出了这个我放的文本框名称的计数器和textchanged事件处理程序中的bool。然后检查bool是否为真,如果是,那么我检查所有名称以找到哪一个被更改
I actually figured this one out i put a counter for the names of the textboxes and a bool in the textchanged event handler.I then checked if the bool was true and if it was then i checked all the names to find which one was changed


这篇关于如何发现哪个动态创建的文本框已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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