Sunday, December 18, 2011

10-Tutorial 9: Using Arrays of Strings Using Visual C++.NET 2010

Tutorial 9: Using Arrays of Strings Using Visual C++.NET 2010
        In this tutorial you will see how to create arrays of strings. Click here for the source code to the Arrays of Strings Program in Visual C++.NET for the 2010. Make a form which looks as follows and code what is in bright red:
Arrays of Strings
        Insert the following code:
#pragma once

namespace StringArrays {

       using namespace System;
       using namespace System::ComponentModel;
       using namespace System::Collections;
       using namespace System::Windows::Forms;
       using namespace System::Data;
       using namespace System::Drawing;

       /// <summary>
       /// Summary for Form1
       /// </summary>
       public ref class Form1 : public System::Windows::Forms::Form
       {
              static array<String^>^ first = gcnew array<String^>(100);
              static array<String^>^ last = gcnew array<String^>(100);
           static int i=0;


       public:
              Form1(void)
              {
                     InitializeComponent();
                     //
                     //TODO: Add the constructor code here
                     //
              }

       protected:
              /// <summary>
              /// Clean up any resources being used.
              /// </summary>
              ~Form1()
              {
                     if (components)
                     {
                           delete components;
                     }
              }
       private: System::Windows::Forms::Label^  label1;
       protected:
       private: System::Windows::Forms::TextBox^  textBox1;
       private: System::Windows::Forms::TextBox^  textBox2;
       private: System::Windows::Forms::Label^  label2;
       private: System::Windows::Forms::Button^  button1;
       private: System::Windows::Forms::Button^  button2;
       private: System::Windows::Forms::TextBox^  textBox3;
       private: System::Windows::Forms::ListBox^  listBox1;

       private:
              /// <summary>
              /// Required designer variable.
              /// </summary>
              System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
              /// <summary>
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// </summary>
              void InitializeComponent(void)
              {
                     this->label1 = (gcnew System::Windows::Forms::Label());
                     this->textBox1 = (gcnew System::Windows::Forms::TextBox());
                     this->textBox2 = (gcnew System::Windows::Forms::TextBox());
                     this->label2 = (gcnew System::Windows::Forms::Label());
                     this->button1 = (gcnew System::Windows::Forms::Button());
                     this->button2 = (gcnew System::Windows::Forms::Button());
                     this->textBox3 = (gcnew System::Windows::Forms::TextBox());
                     this->listBox1 = (gcnew System::Windows::Forms::ListBox());
                     this->SuspendLayout();
                     //
                     // label1
                     //
                     this->label1->AutoSize = true;
                     this->label1->Location = System::Drawing::Point(37, 25);
                     this->label1->Name = L"label1";
                     this->label1->Size = System::Drawing::Size(26, 13);
                     this->label1->TabIndex = 0;
                     this->label1->Text = L"First";
                     //
                     // textBox1
                     //
                     this->textBox1->Location = System::Drawing::Point(40, 41);
                     this->textBox1->Name = L"textBox1";
                     this->textBox1->Size = System::Drawing::Size(84, 20);
                     this->textBox1->TabIndex = 1;
                     //
                     // textBox2
                     //
                     this->textBox2->Location = System::Drawing::Point(40, 92);
                     this->textBox2->Name = L"textBox2";
                     this->textBox2->Size = System::Drawing::Size(84, 20);
                     this->textBox2->TabIndex = 3;
                     //
                     // label2
                     //
                     this->label2->AutoSize = true;
                     this->label2->Location = System::Drawing::Point(37, 76);
                     this->label2->Name = L"label2";
                     this->label2->Size = System::Drawing::Size(27, 13);
                     this->label2->TabIndex = 2;
                     this->label2->Text = L"Last";
                     //
                     // button1
                     //
                     this->button1->Location = System::Drawing::Point(147, 34);
                     this->button1->Name = L"button1";
                     this->button1->Size = System::Drawing::Size(91, 26);
                     this->button1->TabIndex = 4;
                     this->button1->Text = L"Enter Names";
                     this->button1->UseVisualStyleBackColor = true;
                     this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
                     //
                     // button2
                     //
                     this->button2->Location = System::Drawing::Point(147, 69);
                     this->button2->Name = L"button2";
                     this->button2->Size = System::Drawing::Size(91, 26);
                     this->button2->TabIndex = 5;
                     this->button2->Text = L"Enter Index";
                     this->button2->UseVisualStyleBackColor = true;
                     this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
                     //
                     // textBox3
                     //
                     this->textBox3->Location = System::Drawing::Point(154, 124);
                     this->textBox3->Name = L"textBox3";
                     this->textBox3->Size = System::Drawing::Size(84, 20);
                     this->textBox3->TabIndex = 6;
                     //
                     // listBox1
                     //
                     this->listBox1->FormattingEnabled = true;
                     this->listBox1->Location = System::Drawing::Point(272, 24);
                     this->listBox1->Name = L"listBox1";
                     this->listBox1->Size = System::Drawing::Size(120, 147);
                     this->listBox1->TabIndex = 7;
                     //
                     // Form1
                     //
                     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                     this->ClientSize = System::Drawing::Size(420, 208);
                     this->Controls->Add(this->listBox1);
                     this->Controls->Add(this->textBox3);
                     this->Controls->Add(this->button2);
                     this->Controls->Add(this->button1);
                     this->Controls->Add(this->textBox2);
                     this->Controls->Add(this->label2);
                     this->Controls->Add(this->textBox1);
                     this->Controls->Add(this->label1);
                     this->Name = L"Form1";
                     this->Text = L"Form1";
                     this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
                     this->ResumeLayout(false);
                     this->PerformLayout();

              }
#pragma endregion
       private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
                      {
                            first[i]=textBox1->Text;
                            last[i]=textBox2->Text;
                       listBox1->Items->Add(first[i]);
                            listBox1->Items->Add(last[i]);
                            textBox1->Text="";
                            textBox2->Text="";
                       textBox1->Focus();
                       i++;
                      }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)
               {
                      int index;
                      String ^temp;
                      temp=textBox3->Text;
                      index=Int32::Parse(temp);


                 textBox1->Text=first[index];
                 textBox2->Text=last[index];

                i++;
               }
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
               }
};
}
 (Back)

0 comments:

Post a Comment