473,394 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Windows Forms - Control Arrays such as textboxes, buttons, and labels

iknc4miles
Hi all,

How would I go about making an array of controls such as textboxes for Windows Forms Programming using C++ on VC++ .NET 2003? I've tried porting the C# code from this site, but either I'm not a very good programmer or it doesn't work.

Do I need to create a class for it? I don't need to dynamically alter the number of textboxes, I just need to be able to reference each text box by an index number.

- iknc4miles
Mar 2 '07 #1
5 9358
RedSon
5,000 Expert 4TB
That how-to is very thorough. Did you read through it or did you just cut and paste the code into your application? If you read through it was there a particular section that was confusing? That article will get you where you need to be but you have to go through it carefully.
Mar 2 '07 #2
If you read through it was there a particular section that was confusing? That article will get you where you need to be but you have to go through it carefully.
Thanks for the quick reply.

I guess my biggest issue is converting C# syntax into managed C++. Many things, such as declaring a readonly instance of a form, I don't know how to do in C++. Instead, I just made and instance that points to a form:

Expand|Select|Wrap|Line Numbers
  1. private: System::Windows::Forms::Form *HostForm;
I will go through again and come back with some more details on syntax issues.

- iknc4miles
Mar 2 '07 #3
Here's a step by step of my process in imitating the procedure found on that site. This should allow someone else to view what mistakes I'm making.

1. Open a new project under VC++ -->.NET-->>.NET Empty Project, name application ButtonArrayProject.


2. Add class by going to the Project Menu and selecting VC++ -->Generic-->Generic C++ Class
name = ButtonArray
base class = System::Collections::CollectionBase
access = public

I currently only have a header file "ButtonArray.h."


3. Add code to start class
Expand|Select|Wrap|Line Numbers
  1. public __gc class ButtonArray : public System::Collections::CollectionBase
  2. {
  3.     // Public Class ButtonArray
  4. }
Build solution real quick to make sure everything is ok.


4. Add code for private form reference
Expand|Select|Wrap|Line Numbers
  1. public __gc class ButtonArray : public System::Collections::CollectionBase
  2. {
  3.     // Public Class ButtonArray
  4.  
  5. private: System::Windows::Forms::Form HostForm;

5. Add code for AddNewButton method
Expand|Select|Wrap|Line Numbers
  1. public __gc class ButtonArray : public System::Collections::CollectionBase
  2. {
  3.     // Public Class ButtonArray
  4.  
  5. private: System::Windows::Forms::Form *HostForm;
  6.  
  7. public: System::Windows::Forms::Button *AddNewButton(void)
  8.         {
  9.             //Create New Instance of Button Class
  10.             System::Windows::Forms::Button *aButton = new System::Windows::Forms::Button();
  11.  
  12.             //Add new button to collection's list
  13.             this->List->Add(aButton);
  14.  
  15.             //Add the button to the controls collection of the form referenced by the HostForm field.
  16.             HostForm->Controls->Add(aButton);
  17.  
  18.             //Set Initial properties for the button object.
  19.             aButton->Top = Count + 25;
  20.             aButton->Left = 100;
  21.             aButton->Tag = this->Count;
  22.             aButton->Text = String.Concat("Button ", this->Count.ToString());
  23.  
  24.             return aButton;
  25.         }
  26. }
I'll stop here, because I obviously have some syntax errors already. The code builds fine, but that means nothing because nothing's using it yet.

- iknc4miles
Mar 2 '07 #4
If you read through it was there a particular section that was confusing?
Ok, here's a prt that has been giving me lots of problems. Creating the index property similar to their version.

How would I go about rewriting:
Expand|Select|Wrap|Line Numbers
  1.  public System.Windows.Forms.Button this [int Index]
  2. {
  3. get
  4. {
  5. return (System.Windows.Forms.Button) this.List(Index);
  6. }
  7. }
in C++?

- iknc4miles
Mar 2 '07 #5
DONE! Took me all day but this code should work for any control you want to use in Windows Forms Programming in Managed Visual C++. Just replace TextBox with whatever control you want to use and set the control properties as well.

Expand|Select|Wrap|Line Numbers
  1. public __gc class textBoxArray : public System::Collections::CollectionBase
  2. {
  3. private: System::Windows::Forms::Form *HostForm;
  4.  
  5. public: System::Windows::Forms::TextBox *AddNewTextBox(void)
  6.         {
  7.  
  8.             //Create a new instance of the TextBox class.
  9.             System::Windows::Forms::TextBox *newTextBox = new System::Windows::Forms::TextBox();
  10. // Add the textbox to the controls collection of the form referenced by the HostForm field.
  11.             HostForm->Controls->Add(newTextBox);
  12.  
  13.             // Add the textbox to the collection's internal list.
  14.             this->List->Add(newTextBox);
  15.  
  16.             // Set intial properties for textbox object.
  17. // TODO: Rember to change these properties for your control!!!!!
  18.             newTextBox->Top = this->get_Count() * 25;
  19.             newTextBox->Left = 100;
  20.             return newTextBox;
  21.         }
  22. public: textBoxArray(System::Windows::Forms::Form *host)
  23.         {
  24.             HostForm = host;
  25.             this->AddNewTextBox();
  26.         }
  27. public: System::Windows::Forms::TextBox *get_TextBox(int index)
  28.         {
  29.             if(index <= this->Count && index > -1)
  30.             {
  31.                 return dynamic_cast<System::Windows::Forms::TextBox*> (this->List->get_Item(index));
  32.             }
  33.             return dynamic_cast<System::Windows::Forms::TextBox*> (this->List->get_Item(this->get_Count() - 1));
  34.         }
  35. public: void Remove()
  36.         {
  37.             // Check to be sure there is a button to remove.
  38.             if (this->get_Count() > 0)
  39.             {
  40.                 // Remove the last button added to the array from the host from controls collection.  Note the use of the indexer in accessing the array.
  41.                 HostForm->Controls->Remove(this->get_TextBox(this->get_Count() - 1));
  42.                 this->List->RemoveAt(this->get_Count() - 1);
  43.             }
  44.         }
  45.  
  46. };
To use this class on a form, make sure to initialize it before adding or removing from the array.
Expand|Select|Wrap|Line Numbers
  1. private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
  2.          {
  3.              MyTBoxes = new textBoxArray(this);
  4.          }
Enjoy and feel free to improve and repost this code.

- iknc4miles
Mar 2 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Simon Prince | last post by:
Hi I have created a .NET "Windows Custom Control" which is used an ASPX Web Form. It is referenced with the code, from a ASPX page. **************************************************...
5
by: John Smith | last post by:
Hi folks, I'm embedding a Windows Forms User Control into an aspx web page. I've created the class library in C# and added the user control to it. If the control just has simple Windows Forms...
2
by: Peter Stojkovic | last post by:
I have some windows, which have a lot of Textboxes, labels and buttons. How can i - in a simple way- make a print out of all these textboxes ( "input" of textboxes of course ), labels and...
0
by: BodeColander | last post by:
Hello, I'm working on a user control (control A) that contains many controls within it (textboxes, a radio button, listboxes, comboboxes and a button - as well as a whack of labels). During a...
3
by: eBob.com | last post by:
I have several applications which mine web sites for personal information which they publish. They publish the info in one form, I transform the info into Excel spreadsheets. So all these...
3
by: rn5a | last post by:
I already have a user control, named Address.ascx, which displays 4 TextBoxes. As such, there's no problem in rendering these 4 TextBoxes in a ASPX page. The ASPX page renders the 4 TextBoxes twice...
9
by: aaronluna | last post by:
Hi All, I was wondering if it is possible to easily convert an asp.net user control (.ascx) into an equivalent windows app. I plan on simply duplicating the user control in a c# windows app...
3
by: =?Utf-8?B?VG90eSBTYW50YW5h?= | last post by:
I am using textboxes, buttons labels and drop down lists on a web page, but i noticed that textboxes and buttons font size doesn't change when i change the font size in IE ( from IE menu:...
4
by: adam_kroger | last post by:
BRIEF EXPLANATION: I have 6 TextBoxes named LIS1, LIS2, LIS3, ... LIS6. I want to be able to reference them using a For/Next loop and either read ot write to them. In VBA I would use something...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.