本文介绍了声明表单中的文本框数组,并使其在表单中的所有函数/方法中可访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有阵列声明



Texbox [] tb = new Texbox [] {txtbox1,txtbox2, .........}



我应该在哪里创建这个?它应该在表单加载中创建/声明,以使其在相同表单的所有函数/方法中都可以访问吗?





谢谢你提前得到你的帮助。





谢谢。

Hi
I Have array declarations

Texbox [] tb = new Texbox[]{txtbox1,txtbox2,.........}

where should i create this? should it be created/declared in the form load to make it accessbile in all functions/method of the same form?.


Thank you in advance for your help.


Thank you.

推荐答案


const int textBoxCount = // .. could be even variable, in more complex cases

TextBox[] myTextBoxes = new TextBox[textBoxCount];



现在,您需要初始化元素循环。我们假设您从构造函数中调用它(但您可以在其他地方执行):


Now, you need to initialize the elements in the loop. Let's assume you call it from constructor (but you can do it elsewhere):

void InitializeMyTextBoxes(Control someParent) { // some parent could be form, Panel, TabPage, anything like that
    for (int index = 0; index < textBoxCount; ++index) {
        myTextBoxes[index] = new TextBox();
        myTextBoxes[index].Width = //... some width
        myTextBoxes[index].Left = //... may or may not depend on index and your layout
        myTextBoxes[index].Width = //... may or may not depend on index and your layout
        //... any other arrangements
        myTextBoxes[index].Parent = someParent; // this is the key, the way to add it
        // same as someParent.Controls.Add(myTextBoxes[index]);
    } //loop
} //InitializeMyTextBoxes





这样,你可以通过数组对象访问数组的所有元素, myTextBoxes



-SA


这篇关于声明表单中的文本框数组,并使其在表单中的所有函数/方法中可访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:17