Wrox Programmer Forums
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 20th, 2005, 01:49 AM
Registered User
 
Join Date: Oct 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Forms designer

I used the designer to create a 9x9 "array" of textboxes... but it's not really an array, as each had a "unique" name and could not be accessed like an array..

Eventually, I figured out how to create an actual array of textboxes. BUT, it doesn't show up in designer (it shows up when I run it through debug).

Is it possible to use designer to create an actual array of textboxes, or make a created array show up on designer?

 
Old November 2nd, 2005, 03:56 PM
Authorized User
 
Join Date: May 2004
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

I can guess from your post that you've had your hands dirty with VB6 and were a big fan of control arrays.

Any way, the VS.NET designer doesn't support control arrays! In case you're concerned with the performance benefit you could get with control arrays in VB6, there's no performance degradation because of the fact that each control has its independent existence in .NET.

One of the benefits of control arrays in VB6 is that you can write a single event handler to handle events of all members of the control array. You can acheive the same effect by writing an event handler and associating each control with that event in .NET.

In C#, for example, you can do it like this:

//
// Event handler to handle TextBox control's TextChanged event
//
private void textBox_TextChanged(object sender, EventArgs e) {
   // Implementation...
}

// Code to wireup TextChanged event of three of the TextBox controls
// to the preceeding event handler. This code should go into the
// form's constructor.
this.textBox1.TextChanged += new EventHandler(textBox_TextChanged);
this.textBox2.TextChanged += new EventHandler(textBox_TextChanged);
this.textBox3.TextChanged += new EventHandler(textBox_TextChanged);


In the event handler, you can identify which control fired the event like this:

...
if (sender.Equals(textBox1)) {
    // textBox1 fired the event.
}
else if (sender.Equals(textBox2)) {
    // textBox2 fired the event.
}
else if (sender.Equals(textBox3)) {
    // textBox3 fired the event.
}


This way you get all the benefits of control arrays.

If you wish, you can create control arrays in code like this:
...
TextBox[] textBoxes = new TextBox[] {new TextBox(),
                                     new TextBox(),
                                     new TextBox()};

Controls created in this way, however, don't show up on the form until you add them to the Controls collection of the Form. One way of doing this is:

...
this.Controls.AddRange(textBoxes);

I hope this helps.


ejan
 
Old November 2nd, 2005, 04:09 PM
Authorized User
 
Join Date: May 2004
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can access controls like an array through the Controls collection of the Form like this:

// Write name of each control on to the Console output window.
for (int i = 0; i < this.Controls.Count; i++) {
    Console.WriteLine(this.Controls[i].Name);
}

You can also use the foreach construct like this:

// Write name of each control on to the Console output window.
foreach (Control control in this.Controls) {
    Console.WriteLine(control.Name);
}

Regards,


ejan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Web Designer to UI Designer jitesh82 BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 April 16th, 2011 01:04 AM
Rehosting WorkFlow Designer on Web Forms venkateswari Windows Workflow 2 November 13th, 2008 02:02 PM
How to add Windows Forms with Class Designer? chobo2 C# 2008 aka C# 3.0 3 September 15th, 2008 11:00 AM
Article- Rapidly Develop GUI with Forms Designer sarosh Classic ASP Databases 0 July 23rd, 2007 09:11 AM
VC++ .NET 2003 Windows Forms Designer Problem alphatrak General .NET 0 February 15th, 2006 08:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.