3 Replies - 7034 Views - Last Post: 05 September 2007 - 12:44 PM

#1 BrainStew  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 154
  • Joined: 02-August 07

How to declare a vector in a windows form

Posted 04 September 2007 - 02:45 PM

Why won't it let me declare a vector in this windows app in public space?

#pragma once
#include <iostream>
#include <vector>

namespace Drafterv10 {

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

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///		  'Resource File Name' property for the managed resource compiler tool
	///		  associated with all .resx files this class depends on.  Otherwise,
	///		  the designers will not be able to interact properly with localized
	///		  resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		vector <String> v_sAvailible;

		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::ListBox^  ui_Chosen;
	private: System::Windows::Forms::Label^  ui_lChosen;

	private: System::Windows::Forms::ListBox^  ui_Availible;
	private: System::Windows::Forms::Label^  ui_lAvailible;
	private: System::Windows::Forms::Button^  ui_bAdd;
	private: System::Windows::Forms::Button^  ui_bRemove;
	private: System::Windows::Forms::TextBox^  ui_Search;
	private: System::Windows::Forms::Label^  ui_lSearch;
	private: System::Windows::Forms::TextBox^  ui_Suggested;
	private: System::Windows::Forms::Label^  ui_lSuggested;
	private: System::Windows::Forms::Label^  ui_lChosen_Count;
	private: System::Windows::Forms::Label^  ui_lAvailible_Count;

	protected: 

	protected: 

	protected: 

	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->ui_Chosen = (gcnew System::Windows::Forms::ListBox());
			this->ui_lChosen = (gcnew System::Windows::Forms::Label());
			this->ui_Availible = (gcnew System::Windows::Forms::ListBox());
			this->ui_lAvailible = (gcnew System::Windows::Forms::Label());
			this->ui_bAdd = (gcnew System::Windows::Forms::Button());
			this->ui_bRemove = (gcnew System::Windows::Forms::Button());
			this->ui_Search = (gcnew System::Windows::Forms::TextBox());
			this->ui_lSearch = (gcnew System::Windows::Forms::Label());
			this->ui_Suggested = (gcnew System::Windows::Forms::TextBox());
			this->ui_lSuggested = (gcnew System::Windows::Forms::Label());
			this->ui_lChosen_Count = (gcnew System::Windows::Forms::Label());
			this->ui_lAvailible_Count = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// ui_Chosen
			// 
			this->ui_Chosen->FormattingEnabled = true;
			this->ui_Chosen->Location = System::Drawing::Point(13, 39);
			this->ui_Chosen->Name = L"ui_Chosen";
			this->ui_Chosen->Size = System::Drawing::Size(175, 316);
			this->ui_Chosen->TabIndex = 0;
			// 
			// ui_lChosen
			// 
			this->ui_lChosen->AutoSize = true;
			this->ui_lChosen->Location = System::Drawing::Point(13, 15);
			this->ui_lChosen->Name = L"ui_lChosen";
			this->ui_lChosen->Size = System::Drawing::Size(80, 13);
			this->ui_lChosen->TabIndex = 1;
			this->ui_lChosen->Text = L"Chosen Players";
			// 
			// ui_Availible
			// 
			this->ui_Availible->FormattingEnabled = true;
			this->ui_Availible->Location = System::Drawing::Point(292, 39);
			this->ui_Availible->Name = L"ui_Availible";
			this->ui_Availible->Size = System::Drawing::Size(175, 316);
			this->ui_Availible->TabIndex = 2;
			// 
			// ui_lAvailible
			// 
			this->ui_lAvailible->AutoSize = true;
			this->ui_lAvailible->Location = System::Drawing::Point(292, 14);
			this->ui_lAvailible->Name = L"ui_lAvailible";
			this->ui_lAvailible->Size = System::Drawing::Size(83, 13);
			this->ui_lAvailible->TabIndex = 3;
			this->ui_lAvailible->Text = L"Availible Players";
			// 
			// ui_bAdd
			// 
			this->ui_bAdd->Location = System::Drawing::Point(194, 144);
			this->ui_bAdd->Name = L"ui_bAdd";
			this->ui_bAdd->Size = System::Drawing::Size(91, 23);
			this->ui_bAdd->TabIndex = 4;
			this->ui_bAdd->Text = L"<- Add";
			this->ui_bAdd->UseVisualStyleBackColor = true;
			this->ui_bAdd->Click += gcnew System::EventHandler(this, &Form1::ui_bAdd_Click);
			// 
			// ui_bRemove
			// 
			this->ui_bRemove->Location = System::Drawing::Point(194, 173);
			this->ui_bRemove->Name = L"ui_bRemove";
			this->ui_bRemove->Size = System::Drawing::Size(91, 23);
			this->ui_bRemove->TabIndex = 5;
			this->ui_bRemove->Text = L"Remove ->";
			this->ui_bRemove->UseVisualStyleBackColor = true;
			// 
			// ui_Search
			// 
			this->ui_Search->Location = System::Drawing::Point(486, 39);
			this->ui_Search->Name = L"ui_Search";
			this->ui_Search->Size = System::Drawing::Size(160, 20);
			this->ui_Search->TabIndex = 6;
			// 
			// ui_lSearch
			// 
			this->ui_lSearch->AutoSize = true;
			this->ui_lSearch->Location = System::Drawing::Point(486, 14);
			this->ui_lSearch->Name = L"ui_lSearch";
			this->ui_lSearch->Size = System::Drawing::Size(41, 13);
			this->ui_lSearch->TabIndex = 7;
			this->ui_lSearch->Text = L"Search";
			// 
			// ui_Suggested
			// 
			this->ui_Suggested->Location = System::Drawing::Point(486, 146);
			this->ui_Suggested->Name = L"ui_Suggested";
			this->ui_Suggested->Size = System::Drawing::Size(160, 20);
			this->ui_Suggested->TabIndex = 8;
			// 
			// ui_lSuggested
			// 
			this->ui_lSuggested->AutoSize = true;
			this->ui_lSuggested->Location = System::Drawing::Point(486, 122);
			this->ui_lSuggested->Name = L"ui_lSuggested";
			this->ui_lSuggested->Size = System::Drawing::Size(90, 13);
			this->ui_lSuggested->TabIndex = 9;
			this->ui_lSuggested->Text = L"Suggested Player";
			// 
			// ui_lChosen_Count
			// 
			this->ui_lChosen_Count->AutoSize = true;
			this->ui_lChosen_Count->Location = System::Drawing::Point(141, 15);
			this->ui_lChosen_Count->Name = L"ui_lChosen_Count";
			this->ui_lChosen_Count->Size = System::Drawing::Size(47, 13);
			this->ui_lChosen_Count->TabIndex = 10;
			this->ui_lChosen_Count->Text = L"Count: 0";
			// 
			// ui_lAvailible_Count
			// 
			this->ui_lAvailible_Count->AutoSize = true;
			this->ui_lAvailible_Count->Location = System::Drawing::Point(420, 15);
			this->ui_lAvailible_Count->Name = L"ui_lAvailible_Count";
			this->ui_lAvailible_Count->Size = System::Drawing::Size(47, 13);
			this->ui_lAvailible_Count->TabIndex = 11;
			this->ui_lAvailible_Count->Text = L"Count: 0";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(667, 366);
			this->Controls->Add(this->ui_lAvailible_Count);
			this->Controls->Add(this->ui_lChosen_Count);
			this->Controls->Add(this->ui_lSuggested);
			this->Controls->Add(this->ui_Suggested);
			this->Controls->Add(this->ui_lSearch);
			this->Controls->Add(this->ui_Search);
			this->Controls->Add(this->ui_bRemove);
			this->Controls->Add(this->ui_bAdd);
			this->Controls->Add(this->ui_lAvailible);
			this->Controls->Add(this->ui_Availible);
			this->Controls->Add(this->ui_lChosen);
			this->Controls->Add(this->ui_Chosen);
			this->Name = L"Form1";
			this->Text = L"Draver v1.0";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
			 {
				 try
				 {
					 String ^ textFile = ("Availible.txt");
					 StreamReader ^ reader = gcnew StreamReader(textFile);
					 do
					 {
						 this->ui_Availible->Items->Add(reader->ReadLine());
					 }
					 while(reader->Peek() != -1);
				 }
				 catch (System::Exception ^e)
				 {
					 this->ui_Availible->Items->Add(e);
				 }

				 try
				 {
					 String ^ textFile = ("Chosen.txt");
					 StreamReader ^ reader = gcnew StreamReader(textFile);
					 do
					 {
						 this->ui_Chosen->Items->Add(reader->ReadLine());
					 }
					 while(reader->Peek() != -1);
				 }
				 catch (System::Exception ^e)
				 {
					 this->ui_Chosen->Items->Add(e);
				 }
			 }
private: System::Void ui_bAdd_Click(System::Object^  sender, System::EventArgs^  e)
		 {
			 if(this->ui_Availible->SelectedItem)
			 {
			 }
		 }
};
}





error:
------ Build started: Project: Drafter v1.0, Configuration: Debug Win32 ------
Compiling...
Drafter v1.0.cpp
c:\documents and settings\james\desktop\drafter v1.0\drafter v1.0\Form1.h(27) : error C2143: syntax error : missing ';' before '<'
c:\documents and settings\james\desktop\drafter v1.0\drafter v1.0\Form1.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\james\desktop\drafter v1.0\drafter v1.0\Form1.h(27) : error C2238: unexpected token(s) preceding ';'
Build log was saved at "file://c:\Documents and Settings\James\Desktop\Drafter v1.0\Drafter v1.0\Debug\BuildLog.htm"
Drafter v1.0 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



ps: I know I haven't initialize it yet.

Is This A Good Question/Topic? 0
  • +

Replies To: How to declare a vector in a windows form

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4193
  • View blog
  • Posts: 11,868
  • Joined: 18-April 07

Re: How to declare a vector in a windows form

Posted 04 September 2007 - 04:44 PM

First of all you are going to have to add the std namespace, vector is defined in that namespace. Second of all apparently VC++ 2005 says that it is a mixed type and thus can't be used as a member of the form. Third, if you are using managed code, you have to use the managed string type for your vector.

1) Include the std namespace
2) Remove it from the public members of the form and put it in an event instead
3) Use a managed string type vector<String^> myvector;

After you fix these three things then you should be up and running with the vector. And as for the not allowing mixed types in the form members, who knows why that is, it just is. Don't you just love the CLI? I sure do! *sarcasm*

:)
Was This Post Helpful? 0
  • +
  • -

#3 BrainStew  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 154
  • Joined: 02-August 07

Re: How to declare a vector in a windows form

Posted 04 September 2007 - 10:49 PM

I need to use the vector in other events, so is there no way to put it somewhere in the class?

I've tried to add the std namespace, but creates a number of errors related to handles/pointers in the form class...

Can you simply not use a stl container in the way I'd like?

ps: I DO love the CLI.... *sarcasm*
Was This Post Helpful? 0
  • +
  • -

#4 BrainStew  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 154
  • Joined: 02-August 07

Re: How to declare a vector in a windows form

Posted 05 September 2007 - 12:44 PM

I see what you mean now. I guess alternatively I'll have to use a simple array. Why does VC++ 2005 not allow "mixed" member types (ie: stl containers?).

I also could make some kind of class with the same push/pop functions... argh... would have been simpler to just use the stl.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1