声明表单中的文本框数组,并使其在表单中的所有函数/方法中可访问 [英] Declare array of textbox in a form and make it accessible in all funcions/method with in the form

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

问题描述




我有阵列声明



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.

推荐答案

在表单的顶部声明此数组(或者更确切地说,在表单中的所有方法之外)。

在表单的加载事件中加载数组 [ ^ ]。



编辑:更好的apporach是在表单构造函数中初始化数组。

<$ c当我们使用.Net时,$ c> Form_Load 不是最好的方法。



控件在<$ c中初始化$ c> InitializeComponent()方法,所以避免在该方法中定义数组。

让所有控件初始化然后设置数组。
Declare this array at the top of the form (or rather outside all the methods in the form).
Load the array in the form's load event[^].

A better apporach would be to initialize the array in the form constructor.
Form_Load is not the best way to do things when we are using .Net.

Controls are initialized in the InitializeComponent() method so avoid defining the array in that method.
Let all the controls get initialized and then set the array.


有时候,如果有类似的控件,最好创建一个数组。它不需要具有单独的变量/成员名称,允许在循环中遍历数组,以及更多。使用设计器,如果你有很多控件,通常意味着手动代码,重复相同的愚蠢点击等。这与编程的主要思想相反。设计人员的使用应限于非常特定的ad-hoc编程。使用更多代码创建真正严肃的软件。请参阅我对解决方案1的评论。



因此,首先,请不要在设计器中创建文本框。这不是创建阵列的最佳方法。



放在哪里?这完全取决于你。您应该知道,如果在类中声明一个对象,则该类的所有实例成员(即非静态成员)都可以访问它。在哪里初始化?在许多地方,但是数组可以在声明点初始化,并且元素可以在构造函数调用的某个方法中初始化(以及设计器生成的 InitializeComponents ,如果有的话):

Sometimes, it's a great idea to create an array if similar controls. It eliminates the need of having individual variable/member names, allows for traversing the array in a loop, and a lot more. Using the designer, if you have many controls, often means manual code, repeating the same stupid clicks, etc. This is the opposite to the main idea of programming. The designer use should be limited for very specific ad-hoc programming. Real serious software is created using more of the code. Please see my comment to Solution 1.

So, to start with, don't create your text boxes in the designer. This is not a best way to create an array.

Where to put it? It's totally up to you. You should know that if you declare an object in the class, all instance members (that is, non-static ones) of the class will have access to it. Where to initialize? In many places, but the array could be initialized at the point of declaration, and the elements could be initialized in some method called from the constructor (along with designer-generated InitializeComponents, if any):
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


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

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