By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Write to struct, Read struct into textBox2 using Microsoft Visual C++ 2008 Express Wi

P: 1
Expand|Select|Wrap|Line Numbers
  1.  
  2. // 12 lines of code to work with windows
  3. // What I added is in red, the dark red far left 
  4. // is needed the blue is what it wrote   
  5.       #pragma once
  6.             namespace WriteRead {
  7.             using namespace System;
  8.             using namespace System::ComponentModel;
  9.             using namespace System::Collections;
  10.             using namespace System::Windows::Forms;
  11.             using namespace System::Data;
  12.             using namespace System::Drawing;
  13.    // I moved what the program wrote over to the left
  14.    // avoid more confusion
  15.    // >>>|
  16.    // <<<what I add is on the right side 
  17.    // this is where I put the struct not in the .cpp
  18.    // file
  19. public value struct Book{
  20.    // I found an example that is using struct at
  21.    // http://www.functionx.com/vccli/applications
  22.    // /carinventory.htm
  23.    // that example is using Visual C++ 2005, I am using
  24.    // Visual C++ 2008 Express Edition
  25. String ^ tx; // I will trying to do direct access
  26.    // to a text file, to load a page[].tx into textBox2
  27.    // (later)
  28.    // using String pointers? I feel no control and all
  29.    // would be lost if not saved to file
  30.    // char tx[100]; // I would like to do it this way
  31.    // do you have some idea how?
  32. }; // Will I be able to do direct access in a file
  33.    // using "String ^ tx;" ? 
  34.    // If I log the end of the file each time I add a
  35.    // structs, how does it know to only read one back?
  36.    // Using pointers?
  37.    // How does it know the end of one struct? 
  38.    // Book page[10]; // I can not used it this way and
  39.    // not needed I will need a array for index location
  40.  
  41.             /// <summary>
  42.             /// Summary for Form1
  43.             ///
  44.             /// WARNING: If you change the name of
  45.                 /// this class, you will need to change
  46.                 /// the 'Resource File Name' property 
  47.                 /// for the managed resource compiler 
  48.                 /// tool associated with all .resx 
  49.                 /// files this class depends on. 
  50.                 /// Otherwise, the designers will not 
  51.                 /// be able to interact properly with 
  52.                 /// localized resources associated with 
  53.                 /// this form.
  54.             /// </summary>
  55.             public ref class Form1 : public 
  56.                 System::Windows::Forms::Form
  57.             {
  58.             public:
  59.             Form1(void)
  60.             {
  61.                 InitializeComponent();
  62.                 //
  63.                 //TODO: Add the constructor code here
  64.                 //
  65.             }
  66.  
  67.             protected:
  68.             /// <summary>
  69.             /// Clean up any resources being used.
  70.             /// </summary>
  71.             ~Form1()
  72.             {
  73.                 if (components)
  74.                 {
  75.                 delete components;
  76.                 }
  77.             }
  78.             private: 
  79.                 System::Windows::Forms::TextBox^
  80.                 textBox1;
  81.             protected: 
  82.             private: 
  83.                 System::Windows::Forms::Button^
  84.                 button1;
  85.             private: 
  86.                 System::Windows::Forms::TextBox^
  87.                 textBox2;
  88.  
  89.             private:
  90.             /// <summary>
  91.             /// Required designer variable.
  92.             /// </summary>
  93.             System::ComponentModel::Container
  94.             ^components;
  95.             private: 
  96.                 System::Windows::Forms::Button^ 
  97.                 button2;
  98.             private: 
  99.                 System::Windows::Forms::Label^  label1;
  100. static array<Book ^> ^ page = gcnew array<Book ^>(10);     
  101.   // will need just one // how do you read this?
  102.   // I found in a book they could read this, I will
  103.   // try to post it
  104.   // If I say someting in human language like: "need 10
  105.   // Book struck" the program should ask some questions
  106.   // and refine my word and help me write human language
  107.   // or/and in c++ or c# and it would be teaching me
  108.   // and I am sure it would know about gc in "gcnew".
  109.  
  110.         #pragma region Windows Form Designer 
  111.         generated code
  112.             /// <summary>
  113.             /// Required method for Designer support -  
  114.             /// do not modify
  115.             /// the contents of this method with the 
  116.             /// code editor.
  117.             /// </summary>
  118.             void InitializeComponent(void)
  119.             {
  120.                 this->textBox1 = (gcnew 
  121.                 System::Windows::Forms::TextBox());
  122.                 this->button1 = (gcnew 
  123.                 System::Windows::Forms::Button());
  124.                 this->textBox2 = (gcnew 
  125.                 System::Windows::Forms::TextBox());
  126.                 this->button2 = (gcnew 
  127.                 System::Windows::Forms::Button());
  128.                 this->label1 = (gcnew 
  129.                 System::Windows::Forms::Label());
  130.                 this->SuspendLayout();
  131.                 // 
  132.                 // textBox1
  133.                 // 
  134.                 this->textBox1->Location = 
  135.                 System::Drawing::Point(30, 28);
  136.                 this->textBox1->Name = L"textBox1";
  137.                 this->textBox1->Size = 
  138.                 System::Drawing::Size(100, 20);
  139.                 this->textBox1->TabIndex = 0;
  140.                 // 
  141.                 // button1
  142.                 // 
  143.                 this->button1->Location = 
  144.                 System::Drawing::Point(30, 64);
  145.                 this->button1->Name = L"button1";
  146.                 this->button1->Size = 
  147.                 System::Drawing::Size(100, 25);
  148.                 this->button1->TabIndex = 1;
  149.                 this->button1->Text = L"button1";
  150.                 this->button1->UseVisualStyleBackColor
  151.                 = true;
  152.                 this->button1->Click += gcnew 
  153.                 System::EventHandler(this,
  154.                 &Form1::button1_Click);
  155.                 // 
  156.                 // textBox2
  157.                 // 
  158.                 this->textBox2->Location =
  159.                 System::Drawing::Point(30, 108);
  160.                 this->textBox2->Name = L"textBox2";
  161.                 this->textBox2->Size = 
  162.                 System::Drawing::Size(100, 20);
  163.                 this->textBox2->TabIndex = 2;
  164.                 this->textBox2->Text = 
  165.                 this->textBox1->Text;
  166.                 // 
  167.                 // button2
  168.                 // 
  169.                 this->button2->Location = 
  170.                 System::Drawing::Point(30, 146);
  171.                 this->button2->Name = L"button2";
  172.                 this->button2->Size = 
  173.                 System::Drawing::Size(100, 23);
  174.                 this->button2->TabIndex = 3;
  175.                 this->button2->Text = L"Close";
  176.                 this->button2->UseVisualStyleBackColor
  177.                 = true;
  178.                 this->button2->Click += gcnew 
  179.                 System::EventHandler(this,
  180.                 &Form1::button2_Click);
  181.                 // 
  182.                 // label1
  183.                 // 
  184.                 this->label1->AutoSize = true;
  185.                 this->label1->Location = 
  186.                 System::Drawing::Point(30, 13);
  187.                 this->label1->Name = L"label1";
  188.                 this->label1->Size = 
  189.                 System::Drawing::Size(86, 13);
  190.                 this->label1->TabIndex = 4;
  191.                 this->label1->Text = L"Type in 
  192.                 textBox1";
  193.                 // 
  194.                 // Form1
  195.                 // 
  196.                 this->AutoScaleDimensions = 
  197.                 System::Drawing::SizeF(6, 13);
  198.                 this->AutoScaleMode = 
  199.             System::Windows::Forms::AutoScaleMode::Font;
  200.                 this->ClientSize = 
  201.                 System::Drawing::Size(166, 194);
  202.                 this->Controls->Add(this->label1);
  203.                 this->Controls->Add(this->button2);
  204.                 this->Controls->Add(this->textBox2);
  205.                 this->Controls->Add(this->button1);
  206.                  this->Controls->Add(this->textBox1);
  207.                 this->Name = L"Form1";
  208.                 this->Text = L"Form1";
  209.                 this->Load += gcnew 
  210.                 System::EventHandler(this,
  211.                 &Form1::Form1_Load);
  212.                 this->ResumeLayout(false);
  213.                 this->PerformLayout();
  214.             }
  215.         #pragma endregion
  216.                   private: System::Void 
  217.                   Form1_Load(System::Object^  sender,
  218.                   System::EventArgs^  e) 
  219.                   {  
  220.    // This is not the way I want to write to the array 
  221.    // of structs for a 1000 pages of text 
  222. page[0] = gcnew Book; // all this is to test some ideas
  223. page[1] = gcnew Book; // later will write to just one  
  224.    // struct not 4
  225. page[2] = gcnew Book; // then save it to file and log 
  226.    // location in index array
  227. page[3] = gcnew Book; // then retrive from text file  
  228.    // with direct random access
  229.                   }                          
  230.               private: System::Void 
  231.                   button1_Click(System::Object^ 
  232.                   sender, System::EventArgs^  e) 
  233.                   {
  234. this->page[3]->tx = this->textBox1->Text; // copy 
  235.   // textBox1 to struct[3] this works good
  236.   // this->textBox2->Text = this->textBox1->Text;  
  237.   // copy box1 to box2, this works good use to help 
  238.   // test 
  239. this->textBox1->Text = " "; // clear textBox1 this 
  240.   // works good
  241. this->textBox2->Text = this->page[3]->tx; // From 
  242.   // struct to textBox2 ok it is working good
  243.                  }
  244.                  private: System::Void 
  245.                  button2_Click(System::Object^  sender, 
  246.                  System::EventArgs^  e) 
  247.                  {
  248.   // Close();//or you could say
  249. Application::Exit();
  250.                  }
  251.          };
  252. }
  253.  
Sep 10 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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