可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I recently moved from Visual Basic 6 to C# 2010 .NET.
In Visual Basic 6 there was an option to put how many control arrays you would like to use by changing the "index" on it.
I am wondering if this is possible in C#, if so how would I go about doing it with a class like:
func fc = new func();
But with more than just one array in fc, is this possible?
And to be more clarified,
Visual Basic 6 when you load a control like a text box or user control it has in the properties window a option for "Index" and if you change that to 0, 1, etc... it'll allow you to use all of those indexes, without loading multiple controls 50 times.
I think it might have something to do with an arraylist but I'm not entirely sure.
Thanks for any help.
回答1:
That code snippet isn't going to get you very far. Creating a control array is no problem, just initialize it in the form constructor. You can then expose it as a property, although that's generally a bad idea since you don't want to expose implementation details. Something like this:
public partial class Form1 : Form {
private TextBox[] textBoxes;
public Form1() {
InitializeComponent();
textBoxes = new TextBox[] { textBox1, textBox2, textBox3 };
}
public ICollection<TextBox> TextBoxes {
get { return textBoxes; }
}
}
Which then lets you write:
var form = new Form1();
form.TextBoxes[0].Text = "hello";
form.Show();
But don't, let the form manage its own text boxes.
回答2:
In .NET you would create an array of controls, then you would instance a TextBox control for each element of the array, setting the properties of the control and positioning it on the form:
TextBox[] txtArray = new TextBox[500];
for (int i = 0; i < txtArray.length; i++)
{
// instance the control
txtArray[i] = new TextBox();
// set some initial properties
txtArray[i].Name = "txt" + i.ToString();
txtArray[i].Text = "";
// add to form
Form1.Controls.Add(txtArray[i]);
txtArray[i].Parent = Form1;
// set position and size
txtArray[i].Location = new Point(50, 50);
txtArray[i].Size = new Size(200, 25);
}
.
.
.
Form1.txt1.text = "Hello World!";
Unless your layout is more simplistic (i.e. rows and columns of textboxes) you may find using the designer to be easier, less time consuming and more maintainable.
回答3:
Not exactly like VB6, but it is quite easy to write the code your self in c#.
If you create a control, like a Button
in the designer you can copy the code from the *.Designer.cs
file
It typically looks like this
private System.Windows.Forms.Button button1;
...
this.button1.Location = new System.Drawing.Point(40, 294);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 14;
this.button1.Text = "Button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
...
this.Controls.Add(this.button1);
Cut that code out and paste in a method instead, returning the Button
private Button CreateButton()
{
private System.Windows.Forms.Button button1;
this.button1.Location = new System.Drawing.Point(40, 294); // <-- change location for each
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 14; // <-- increase tab index or remove this line
this.button1.Text = "Button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.Controls.Add(this.button1);
return button;
}
then call this method like this
List<Button> buttons = new List<Button>();
for(int i = 0; i < 10; i++)
{
buttons.Add(CreateButton());
}
回答4:
Read this transition guide from MS: http://msdn.microsoft.com/en-us/library/kxt4418a%28v=vs.80%29.aspx
回答5:
Based on Trigo's template:
Here an example handling 2 dimensional textBox array
panel1 has to be created with the designer
(I had Autoscroll=true, Size=858;525)
public partial class Form1 : Form
{
TextBox[,] txtBoxArray = new TextBox[2,100];
public Form1()
{
InitializeComponent();
for (int i = 0; i < txtBoxArray.GetLength(0); i++)
{
for (int j = 0; j < txtBoxArray.GetLength(1); j++)
{
// instance the control
txtBoxArray[i, j] = new TextBox();
// set some initial properties
txtBoxArray[i, j].Name = "txtBox_" + i.ToString() + "_" + j.ToString();
txtBoxArray[i, j].Text = txtBoxArray[i, j].Name; //"";
// add to form
this.Controls.Add(txtBoxArray[i,j]);
txtBoxArray[i, j].Parent = panel1;
// set position and size
txtBoxArray[i, j].Location = new Point(50+i*333, 50 + j * 25);
txtBoxArray[i, j].Size = new Size(200, 25);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
//...
}