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.
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.
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.
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With