ページ 11

オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月20日(水) 22:36
by もみじ
下記で質問したものです。
http://dixq.net/forum/viewtopic.php?f=3 ... xml#p66304

ご連絡が遅くなってしまい申し訳ありませんでした。
クラス化してみたのですが初期化がおかしい様でエラーが取ることができません。

\multieditor\Form1.h(41) : error C2143: 構文エラー : ')' が ';' の前にありません。

また保存の仕方もよくわからないのですが宜しければソースを今一度みては頂けないでしょうか。

コード:

#pragma once

#define MAX 256;


namespace MultiEditor {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	
	// オリジナルクラス
	ref class OBJECT_DATA{
		String^ title;
		String^ body;
	};
	
	/// <summary>
	/// Form1 の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//

			int index = -1;
			stringData = gcnew array<OBJECT_DATA^>(MAX);
			
			// 読み込み
			System::IO::StreamReader^ sr = 
				gcnew System::IO::StreamReader("XML.txt",System::Text::Encoding::GetEncoding(932));
		
			sr->ReadLine();
			String^ Item;
			while((Item = sr->ReadLine()) != nullptr){
				String^ str = Item;
				if(str->IndexOf("//") >= 0) index++;//->Substring(0,2)
				
				// 値を挿入する
				if( index != -1 ){
					if( str->IndexOf("//") >= 0 ) this->listBoxTitle->Items->Add(Item->Replace("//",""));
					else stringData->title[index] += Item + "\n";
				}				
			}
			sr->Close();
			
		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListBox^  listBoxTitle;
	private: System::Windows::Forms::RichTextBox^  richTextBoxBody;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  ファイルFToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  menuSave;
	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
	private: System::Windows::Forms::TextBox^  textBoxTitle;
	private: System::Windows::Forms::Button^  buttonNew;

	private: array<OBJECT_DATA^>^ stringData;
			// 保存用の文字列を作成する
			private: String^ SaveTextFileName(void){
				return "aaa";
			}
			// データを保存する
			private: void SaveTextFileName(String^ SaveString, String^ FileName){
				System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(FileName,false,System::Text::Encoding::GetEncoding(932)); // タブごとに確認
				sw->Write(SaveString);
				sw->Close();
			}	

	protected: 


	protected: 

	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->listBoxTitle = (gcnew System::Windows::Forms::ListBox());
			this->richTextBoxBody = (gcnew System::Windows::Forms::RichTextBox());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->ファイルFToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->menuSave = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->textBoxTitle = (gcnew System::Windows::Forms::TextBox());
			this->buttonNew = (gcnew System::Windows::Forms::Button());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// listBoxTitle
			// 
			this->listBoxTitle->FormattingEnabled = true;
			this->listBoxTitle->HorizontalScrollbar = true;
			this->listBoxTitle->ItemHeight = 12;
			this->listBoxTitle->Location = System::Drawing::Point(12, 36);
			this->listBoxTitle->Name = L"listBoxTitle";
			this->listBoxTitle->Size = System::Drawing::Size(241, 376);
			this->listBoxTitle->TabIndex = 0;
			this->listBoxTitle->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBoxTitle_SelectedIndexChanged);
			// 
			// richTextBoxBody
			// 
			this->richTextBoxBody->Location = System::Drawing::Point(259, 59);
			this->richTextBoxBody->Name = L"richTextBoxBody";
			this->richTextBoxBody->Size = System::Drawing::Size(361, 377);
			this->richTextBoxBody->TabIndex = 1;
			this->richTextBoxBody->Text = L"";
			this->richTextBoxBody->TextChanged += gcnew System::EventHandler(this, &Form1::richTextBoxBody_TextChanged);
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->ファイルFToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(632, 24);
			this->menuStrip1->TabIndex = 2;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// ファイルFToolStripMenuItem
			// 
			this->ファイルFToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->menuSave});
			this->ファイルFToolStripMenuItem->Name = L"ファイルFToolStripMenuItem";
			this->ファイルFToolStripMenuItem->Size = System::Drawing::Size(66, 20);
			this->ファイルFToolStripMenuItem->Text = L"ファイル(&F)";
			// 
			// menuSave
			// 
			this->menuSave->Name = L"menuSave";
			this->menuSave->Size = System::Drawing::Size(109, 22);
			this->menuSave->Text = L"保存(&S)";
			this->menuSave->Click += gcnew System::EventHandler(this, &Form1::menuSave_Click);
			// 
			// saveFileDialog1
			// 
			this->saveFileDialog1->Filter = L"テキストファイル(*.txt)|*.txt|すべてのファイル(*.*)|*.*";
			// 
			// textBoxTitle
			// 
			this->textBoxTitle->Location = System::Drawing::Point(259, 36);
			this->textBoxTitle->Name = L"textBoxTitle";
			this->textBoxTitle->Size = System::Drawing::Size(361, 19);
			this->textBoxTitle->TabIndex = 3;
			this->textBoxTitle->TextChanged += gcnew System::EventHandler(this, &Form1::textBoxTitle_TextChanged);
			// 
			// buttonNew
			// 
			this->buttonNew->Location = System::Drawing::Point(12, 418);
			this->buttonNew->Name = L"buttonNew";
			this->buttonNew->Size = System::Drawing::Size(241, 23);
			this->buttonNew->TabIndex = 4;
			this->buttonNew->Text = L"新規追加";
			this->buttonNew->UseVisualStyleBackColor = true;
			this->buttonNew->Click += gcnew System::EventHandler(this, &Form1::buttonNew_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(632, 446);
			this->Controls->Add(this->buttonNew);
			this->Controls->Add(this->textBoxTitle);
			this->Controls->Add(this->richTextBoxBody);
			this->Controls->Add(this->listBoxTitle);
			this->Controls->Add(this->menuStrip1);
			this->KeyPreview = true;
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"MultiEditor";
			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();
			
		}
#pragma endregion

		// 保存を押したら
private: System::Void menuSave_Click(System::Object^  sender, System::EventArgs^  e) {

			 /*
			 String^ save = "";
			 for(int i=0; i<this->listBoxTitle->Items->Count; i++){
				save += "//" + this->listBoxTitle->Items[i] + "\r\n" + body[i] + "\r\n";
			 }
			 if(saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK){
				System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(saveFileDialog1->FileName,false,System::Text::Encoding::GetEncoding(932));
				sw->Write( save );
				sw->Close();
			}
			*/
		 }

		 // リストボックス選択番号が変わったら
private: System::Void listBoxTitle_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 this->richTextBoxBody->Text = stringData[this->listBoxTitle->SelectedIndex];
			 this->textBoxTitle->Text = this->listBoxTitle->Text;
		 }
private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
			 
			 switch(e->KeyCode){
				// アプリケーションの終了
				case System::Windows::Forms::Keys::Escape:
					Application::Exit();
					break;
				
				// リストボックスの削除
				case System::Windows::Forms::Keys::Delete:
					if( this->listBoxTitle->SelectedIndex != -1 )
						this->listBoxTitle->Items->RemoveAt( this->listBoxTitle->SelectedIndex );
					break;
				
			 }
			 
			 // 新規追加
			 if (e->Control == true){
				 switch (e->KeyCode){
					case System::Windows::Forms::Keys::N:
						int i = this->listBoxTitle->SelectedIndex;
						if( i != -1 ) this->listBoxTitle->Items[i] = i.ToString();
						break;
				 }
			 }
		 }
private: System::Void textBoxTitle_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 int i = this->listBoxTitle->SelectedIndex;
			 if( i != -1 ) this->listBoxTitle->Items[i] = this->textBoxTitle->Text;
		 }
private: System::Void buttonNew_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->listBoxTitle->Items->Add("New");
		 }
private: System::Void richTextBoxBody_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			  stringData->body[this->listBoxTitle->SelectedIndex] = this->richTextBoxBody->Text;
		 }
};
}

// 0309 0630~0715

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月21日(木) 02:41
by YuO
もみじ さんが書きました:\multieditor\Form1.h(41) : error C2143: 構文エラー : ')' が ';' の前にありません。
というわけなので,41行目を抜粋。
もみじ さんが書きました:

コード:

			stringData = gcnew array<OBJECT_DATA^>(MAX);
何もおかしくなさそうですが,MAXの定義を引っ張ってくると……
もみじ さんが書きました:

コード:

#define MAX 256;
MAXの定義に;が付いています。つまり,先の41行目は

コード:

			stringData = gcnew array<OBJECT_DATA^>(256;);
と展開されるため,エラーになります。

C++にしろC++/CLIにしろ,#defineはプリプロセッサマクロで使う場合を除いて,使わないで済みます。
const変数やinline関数 (場合によっては関数template) にすることで,通常は置き換えが可能です。

まず,4行目の#define文を削除して,
Form1の定義の先頭に,

コード:

private: const static int MAX = 256;
を追加するか,C++/CLIなので

コード:

private: literal int MAX = 256;
として追加すれば良いでしょう。
# C#やVisual Basicとの相互運用を考えるならば後者。前者はIL上定数ではないから。

MSDN: literal (Visual C++)
http://msdn.microsoft.com/en-us/library/5yzft952.aspx

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月21日(木) 07:55
by もみじ
そういうことだったのですか(汗)
エラーが一つ消えました!!
ありがとうございます。

ただ、下記の様なエラーが出てしまい困っております。
良く見たらstringDataの配列へのアクセス方法が逆だったので修正してみたのですが・・・。

error C2248: 'MultiEditor::OBJECT_DATA::title' : private メンバ (クラス 'MultiEditor::OBJECT_DATA' で宣言されている) にアクセスできません。
'MultiEditor::OBJECT_DATA::title' の宣言を確認してください。
'MultiEditor::OBJECT_DATA' の宣言を確認してください。
error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : 1 番目の引数を 'MultiEditor::OBJECT_DATA ^' から 'System::String ^' に変換できません。(新しい機能 ; ヘルプを参照)
使用可能なユーザー定義された変換演算子がない、または
指示された型は関連がありません。変換には reinterpret_cast、C スタイル キャストまたは関数スタイルのキャストが必要です。
error C2248: 'MultiEditor::OBJECT_DATA::body' : private メンバ (クラス 'MultiEditor::OBJECT_DATA' で宣言されている) にアクセスできません。
'MultiEditor::OBJECT_DATA::body' の宣言を確認してください。
'MultiEditor::OBJECT_DATA' の宣言を確認してください。

コード:

#pragma once

//#define MAX 256

namespace MultiEditor {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	
	// オリジナルクラス
	ref class OBJECT_DATA{
		String^ title;
		String^ body;
	};
	
	/// <summary>
	/// Form1 の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//

			int index = 0;//-1;
			stringData = gcnew array<OBJECT_DATA^>(MAX);
			
			// 読み込み
			System::IO::StreamReader^ sr = 
				gcnew System::IO::StreamReader("XML.txt",System::Text::Encoding::GetEncoding(932));
		
			sr->ReadLine();
			String^ Item;
			while((Item = sr->ReadLine()) != nullptr){
				String^ str = Item;
				if(str->IndexOf("//") >= 0) index++;//->Substring(0,2)
				
				// 値を挿入する
				if( index != -1 ){
					if( str->IndexOf("//") >= 0 ) this->listBoxTitle->Items->Add(Item->Replace("//",""));
					else stringData[index]->title += Item + "\n";
				}				
			}
			sr->Close();
			
		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListBox^  listBoxTitle;
	private: System::Windows::Forms::RichTextBox^  richTextBoxBody;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  ファイルFToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  menuSave;
	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
	private: System::Windows::Forms::TextBox^  textBoxTitle;
	private: System::Windows::Forms::Button^  buttonNew;

	
	private: const static int MAX = 256; 

	private: array<OBJECT_DATA^>^ stringData;

	// 保存用の文字列を作成する
	private: String^ SaveTextFileName(void){
		return "aaa";
	}
	// データを保存する
	private: void SaveTextFileName(String^ SaveString, String^ FileName){
		System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(FileName,false,System::Text::Encoding::GetEncoding(932)); // タブごとに確認
		sw->Write(SaveString);
		sw->Close();
	}	

	protected: 


	protected: 

	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->listBoxTitle = (gcnew System::Windows::Forms::ListBox());
			this->richTextBoxBody = (gcnew System::Windows::Forms::RichTextBox());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->ファイルFToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->menuSave = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->textBoxTitle = (gcnew System::Windows::Forms::TextBox());
			this->buttonNew = (gcnew System::Windows::Forms::Button());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// listBoxTitle
			// 
			this->listBoxTitle->FormattingEnabled = true;
			this->listBoxTitle->HorizontalScrollbar = true;
			this->listBoxTitle->ItemHeight = 12;
			this->listBoxTitle->Location = System::Drawing::Point(12, 36);
			this->listBoxTitle->Name = L"listBoxTitle";
			this->listBoxTitle->Size = System::Drawing::Size(241, 376);
			this->listBoxTitle->TabIndex = 0;
			this->listBoxTitle->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBoxTitle_SelectedIndexChanged);
			// 
			// richTextBoxBody
			// 
			this->richTextBoxBody->Location = System::Drawing::Point(259, 59);
			this->richTextBoxBody->Name = L"richTextBoxBody";
			this->richTextBoxBody->Size = System::Drawing::Size(361, 377);
			this->richTextBoxBody->TabIndex = 1;
			this->richTextBoxBody->Text = L"";
			this->richTextBoxBody->TextChanged += gcnew System::EventHandler(this, &Form1::richTextBoxBody_TextChanged);
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->ファイルFToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(632, 24);
			this->menuStrip1->TabIndex = 2;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// ファイルFToolStripMenuItem
			// 
			this->ファイルFToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->menuSave});
			this->ファイルFToolStripMenuItem->Name = L"ファイルFToolStripMenuItem";
			this->ファイルFToolStripMenuItem->Size = System::Drawing::Size(66, 20);
			this->ファイルFToolStripMenuItem->Text = L"ファイル(&F)";
			// 
			// menuSave
			// 
			this->menuSave->Name = L"menuSave";
			this->menuSave->Size = System::Drawing::Size(109, 22);
			this->menuSave->Text = L"保存(&S)";
			this->menuSave->Click += gcnew System::EventHandler(this, &Form1::menuSave_Click);
			// 
			// saveFileDialog1
			// 
			this->saveFileDialog1->Filter = L"テキストファイル(*.txt)|*.txt|すべてのファイル(*.*)|*.*";
			// 
			// textBoxTitle
			// 
			this->textBoxTitle->Location = System::Drawing::Point(259, 36);
			this->textBoxTitle->Name = L"textBoxTitle";
			this->textBoxTitle->Size = System::Drawing::Size(361, 19);
			this->textBoxTitle->TabIndex = 3;
			this->textBoxTitle->TextChanged += gcnew System::EventHandler(this, &Form1::textBoxTitle_TextChanged);
			// 
			// buttonNew
			// 
			this->buttonNew->Location = System::Drawing::Point(12, 418);
			this->buttonNew->Name = L"buttonNew";
			this->buttonNew->Size = System::Drawing::Size(241, 23);
			this->buttonNew->TabIndex = 4;
			this->buttonNew->Text = L"新規追加";
			this->buttonNew->UseVisualStyleBackColor = true;
			this->buttonNew->Click += gcnew System::EventHandler(this, &Form1::buttonNew_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(632, 446);
			this->Controls->Add(this->buttonNew);
			this->Controls->Add(this->textBoxTitle);
			this->Controls->Add(this->richTextBoxBody);
			this->Controls->Add(this->listBoxTitle);
			this->Controls->Add(this->menuStrip1);
			this->KeyPreview = true;
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"MultiEditor";
			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();
			
		}
#pragma endregion

		// 保存を押したら
private: System::Void menuSave_Click(System::Object^  sender, System::EventArgs^  e) {

			 /*
			 String^ save = "";
			 for(int i=0; i<this->listBoxTitle->Items->Count; i++){
				save += "//" + this->listBoxTitle->Items[i] + "\r\n" + body[i] + "\r\n";
			 }
			 if(saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK){
				System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(saveFileDialog1->FileName,false,System::Text::Encoding::GetEncoding(932));
				sw->Write( save );
				sw->Close();
			}
			*/
		 }

		 // リストボックス選択番号が変わったら
private: System::Void listBoxTitle_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 this->richTextBoxBody->Text = stringData[this->listBoxTitle->SelectedIndex];
			 this->textBoxTitle->Text = this->listBoxTitle->Text;
		 }
private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
			 
			 switch(e->KeyCode){
				// アプリケーションの終了
				case System::Windows::Forms::Keys::Escape:
					Application::Exit();
					break;
				
				// リストボックスの削除
				case System::Windows::Forms::Keys::Delete:
					if( this->listBoxTitle->SelectedIndex != -1 )
						this->listBoxTitle->Items->RemoveAt( this->listBoxTitle->SelectedIndex );
					break;
				
			 }
			 
			 // 新規追加
			 if (e->Control == true){
				 switch (e->KeyCode){
					case System::Windows::Forms::Keys::N:
						int i = this->listBoxTitle->SelectedIndex;
						if( i != -1 ) this->listBoxTitle->Items[i] = i.ToString();
						break;
				 }
			 }
		 }
private: System::Void textBoxTitle_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 int i = this->listBoxTitle->SelectedIndex;
			 if( i != -1 ) this->listBoxTitle->Items[i] = this->textBoxTitle->Text;
		 }
private: System::Void buttonNew_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->listBoxTitle->Items->Add("New");
		 }
private: System::Void richTextBoxBody_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			  stringData[this->listBoxTitle->SelectedIndex]->body = this->richTextBoxBody->Text;
		 }
};
}

// 0309 0630~0715

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月21日(木) 12:31
by ookami
とりあえず、
// オリジナルクラス
ref class OBJECT_DATA{
String^ title;
String^ body;
};
を、
// オリジナルクラス
ref class OBJECT_DATA{
public:
String^ title;
String^ body;
};
とすれば、エラーのひとつは解消しますが、
http://wisdom.sakura.ne.jp/programming/cpp/cpp21.html
このあたりを見て アクセス制御 について勉強して、
目的に応じてアクセサなどを作成する方がよいと思います。

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月21日(木) 23:56
by もみじ
エラーが全て取れました!!
ありがとうございます。
但し、コンパイルは通るようになったのですが
if( str->IndexOf("//") >= 0 ) stringData[index]->title = Item->Replace("//","");
else stringData[index]->body = Item + "\n";
の部分で強制的に落ちてしまいます。
indexの値が常に0しか入らないような形にしても落ちたので入れ方?が悪いのでしょうか。

どうしてもこのツールを完成させたいです・・・。
このやり方が悪いようでしたら修正しますので何とかアドバイスを頂けないでしょうか。

コード:

#pragma once

//#define MAX 256

namespace MultiEditor {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	
	// オリジナルクラス
	ref class OBJECT_DATA{
	public:
		String^ title;
		String^ body;
	};
	
	/// <summary>
	/// Form1 の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//

			int index = -1;
			stringData = gcnew array<OBJECT_DATA^>(MAX);
			
			// 読み込み
			System::IO::StreamReader^ sr = 
				gcnew System::IO::StreamReader("XML.txt",System::Text::Encoding::GetEncoding(932));
		
			sr->ReadLine();
			String^ Item;
			while((Item = sr->ReadLine()) != nullptr){
				String^ str = Item;
				if(str->IndexOf("//") >= 0) index++;//->Substring(0,2)
				
				// 値を挿入する
				if( index != -1 ){
					if( str->IndexOf("//") >= 0 ) stringData[index]->title = Item->Replace("//","");
					else stringData[index]->body = Item + "\n";
					//if( str->IndexOf("//") >= 0 ) this->listBoxTitle->Items->Add(Item->Replace("//",""));
					//else stringData[index]->title += Item + "\n";
				}				
			}
			sr->Close();

		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListBox^  listBoxTitle;
	private: System::Windows::Forms::RichTextBox^  richTextBoxBody;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  ファイルFToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  menuSave;
	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
	private: System::Windows::Forms::TextBox^  textBoxTitle;
	private: System::Windows::Forms::Button^  buttonNew;

	
	private: const static int MAX = 256; 

	private: array<OBJECT_DATA^>^ stringData;

	// 保存用の文字列を作成する
	private: String^ SaveTextFileName(void){
		return "aaa";
	}
	// データを保存する
	private: void SaveTextFileName(String^ SaveString, String^ FileName){
		System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(FileName,false,System::Text::Encoding::GetEncoding(932)); // タブごとに確認
		sw->Write(SaveString);
		sw->Close();
	}	

	protected: 


	protected: 

	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->listBoxTitle = (gcnew System::Windows::Forms::ListBox());
			this->richTextBoxBody = (gcnew System::Windows::Forms::RichTextBox());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->ファイルFToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->menuSave = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->textBoxTitle = (gcnew System::Windows::Forms::TextBox());
			this->buttonNew = (gcnew System::Windows::Forms::Button());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// listBoxTitle
			// 
			this->listBoxTitle->FormattingEnabled = true;
			this->listBoxTitle->HorizontalScrollbar = true;
			this->listBoxTitle->ItemHeight = 12;
			this->listBoxTitle->Location = System::Drawing::Point(12, 36);
			this->listBoxTitle->Name = L"listBoxTitle";
			this->listBoxTitle->Size = System::Drawing::Size(241, 376);
			this->listBoxTitle->TabIndex = 0;
			this->listBoxTitle->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBoxTitle_SelectedIndexChanged);
			// 
			// richTextBoxBody
			// 
			this->richTextBoxBody->Location = System::Drawing::Point(259, 59);
			this->richTextBoxBody->Name = L"richTextBoxBody";
			this->richTextBoxBody->Size = System::Drawing::Size(361, 377);
			this->richTextBoxBody->TabIndex = 1;
			this->richTextBoxBody->Text = L"";
			this->richTextBoxBody->TextChanged += gcnew System::EventHandler(this, &Form1::richTextBoxBody_TextChanged);
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->ファイルFToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(632, 24);
			this->menuStrip1->TabIndex = 2;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// ファイルFToolStripMenuItem
			// 
			this->ファイルFToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->menuSave});
			this->ファイルFToolStripMenuItem->Name = L"ファイルFToolStripMenuItem";
			this->ファイルFToolStripMenuItem->Size = System::Drawing::Size(66, 20);
			this->ファイルFToolStripMenuItem->Text = L"ファイル(&F)";
			// 
			// menuSave
			// 
			this->menuSave->Name = L"menuSave";
			this->menuSave->Size = System::Drawing::Size(109, 22);
			this->menuSave->Text = L"保存(&S)";
			this->menuSave->Click += gcnew System::EventHandler(this, &Form1::menuSave_Click);
			// 
			// saveFileDialog1
			// 
			this->saveFileDialog1->Filter = L"テキストファイル(*.txt)|*.txt|すべてのファイル(*.*)|*.*";
			// 
			// textBoxTitle
			// 
			this->textBoxTitle->Location = System::Drawing::Point(259, 36);
			this->textBoxTitle->Name = L"textBoxTitle";
			this->textBoxTitle->Size = System::Drawing::Size(361, 19);
			this->textBoxTitle->TabIndex = 3;
			this->textBoxTitle->TextChanged += gcnew System::EventHandler(this, &Form1::textBoxTitle_TextChanged);
			// 
			// buttonNew
			// 
			this->buttonNew->Location = System::Drawing::Point(12, 418);
			this->buttonNew->Name = L"buttonNew";
			this->buttonNew->Size = System::Drawing::Size(241, 23);
			this->buttonNew->TabIndex = 4;
			this->buttonNew->Text = L"新規追加";
			this->buttonNew->UseVisualStyleBackColor = true;
			this->buttonNew->Click += gcnew System::EventHandler(this, &Form1::buttonNew_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(632, 446);
			this->Controls->Add(this->buttonNew);
			this->Controls->Add(this->textBoxTitle);
			this->Controls->Add(this->richTextBoxBody);
			this->Controls->Add(this->listBoxTitle);
			this->Controls->Add(this->menuStrip1);
			this->KeyPreview = true;
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"MultiEditor";
			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();
			
		}
#pragma endregion

		// 保存を押したら
private: System::Void menuSave_Click(System::Object^  sender, System::EventArgs^  e) {

			 /*
			 String^ save = "";
			 for(int i=0; i<this->listBoxTitle->Items->Count; i++){
				save += "//" + this->listBoxTitle->Items[i] + "\r\n" + body[i] + "\r\n";
			 }
			 if(saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK){
				System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(saveFileDialog1->FileName,false,System::Text::Encoding::GetEncoding(932));
				sw->Write( save );
				sw->Close();
			}
			*/
		 }

		 // リストボックス選択番号が変わったら
private: System::Void listBoxTitle_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 this->richTextBoxBody->Text = stringData[this->listBoxTitle->SelectedIndex]->body;
			 this->textBoxTitle->Text = this->listBoxTitle->Text;
		 }
private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
			 
			 switch(e->KeyCode){
				// アプリケーションの終了
				case System::Windows::Forms::Keys::Escape:
					Application::Exit();
					break;
				
				// リストボックスの削除
				case System::Windows::Forms::Keys::Delete:
					if( this->listBoxTitle->SelectedIndex != -1 )
						this->listBoxTitle->Items->RemoveAt( this->listBoxTitle->SelectedIndex );
					break;
				
			 }
			 
			 // 新規追加
			 if (e->Control == true){
				 switch (e->KeyCode){
					case System::Windows::Forms::Keys::N:
						int i = this->listBoxTitle->SelectedIndex;
						if( i != -1 ) this->listBoxTitle->Items[i] = i.ToString();
						break;
				 }
			 }
		 }
private: System::Void textBoxTitle_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 int i = this->listBoxTitle->SelectedIndex;
			 if( i != -1 ) this->listBoxTitle->Items[i] = this->textBoxTitle->Text;
		 }
private: System::Void buttonNew_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->listBoxTitle->Items->Add("New");
		 }
private: System::Void richTextBoxBody_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			  stringData[this->listBoxTitle->SelectedIndex]->body = this->richTextBoxBody->Text;
		 }
};
}

// 0309 0630~0715

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月22日(金) 00:32
by YuO
もみじ さんが書きました:但し、コンパイルは通るようになったのですが

コード:

					if( str->IndexOf("//") >= 0 ) stringData[index]->title = Item->Replace("//","");
					else stringData[index]->body = Item + "\n";
の部分で強制的に落ちてしまいます。
indexの値が常に0しか入らないような形にしても落ちたので入れ方?が悪いのでしょうか。
デバッグしましょう,というのがとりあえずの答えになりそうですが……。
VC++であれば,例外が発生した時点でブレイクするはずです。
ウォッチ等を使えば,その時点での変数等の値を見ることができます。

あと,「落ちた」ではなく,「○○という例外が発生した」ということをきちんと報告してください。
マネージコードであれば,System::IndexOutOfRangeExceptionなのかSystem::NullReferenceExceptionなのか,
そういった情報は解決の基礎的な手がかりになります。


で,NullReferenceExceptionと仮定して……OBJECT_DATAはref classですから参照型です。
そのため,stringData自体はgcnewで(OBJECT_DATAの配列型を持つ)オブジェクトを確保していますが,
stringData[0]等にオブジェクトが割り当てられていません。
ちゃんとstringData[0]等にオブジェクトを割り当ててください。

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月22日(金) 05:25
by もみじ
じつは独学で学んでまして変数などをどう初期化すればよいかが良く分かってないんですよね・・・。
このツールを作って学んでいこうと思っておりました。
配列にする場合、各要素も初期化しなければならないのですか・・・。
下記のように変更したら問題なく動きました。
ありがとうございました!!

動作も私が作りたいものに近づいてきました。
後、2点だけ問題がありそうです。
1点目 System::Void textBoxTitle_TextChanged関数が原因で落ちる。
デバッガを見ると配列の範囲外にアクセスしていることは明白なのですが上手くエラーが取れません。
int i = this->listBoxTitle->SelectedIndex;の値の取り方が悪きがするのですが修正できませんでした・・・。

2点目 読み込み時、保存時に改行コードが変に入ってしまう為
毎回、保存して起動する度に改行の数が変わってきてしまいます。
こちらもどうにか修正したいのです。

質問ばかりで申し訳ないです。
下記が修正しましたソースとなります。
御手隙の時で構いませんので見て頂けますと助かります。

コード:

#pragma once

//#define MAX 256

namespace MultiEditor {

	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>
	/// Form1 の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//

			int index = -1;

			stringData = gcnew array<OBJECT_DATA^>(MAX);
			for(int i=0; i<MAX; i++){
				stringData[i] = gcnew OBJECT_DATA;
			}
			
			// 読み込み
			System::IO::StreamReader^ sr = 
				gcnew System::IO::StreamReader("XML.txt",System::Text::Encoding::GetEncoding(932));
		
			sr->ReadLine();
			String^ Item;
			while((Item = sr->ReadLine()) != nullptr){
				String^ str = Item;
				if(str->IndexOf("//") >= 0) index++;//->Substring(0,2)
				
				// 値を挿入する
				if( index != -1 ){
					if( str->IndexOf("//") >= 0 ) stringData[index]->title = Item->Replace("//","");
					else stringData[index]->body += Item + "\n";
				}				
			}
			sr->Close();

			// 要素を格納する
			for(int i=0; i<index; i++){
				this->listBoxTitle->Items->Add(stringData[i]->title);
			}

		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListBox^  listBoxTitle;
	private: System::Windows::Forms::RichTextBox^  richTextBoxBody;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  ファイルFToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  menuSave;
	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
	private: System::Windows::Forms::TextBox^  textBoxTitle;
	private: System::Windows::Forms::Button^  buttonNew;
	
			 
	// オリジナルクラス
	ref class OBJECT_DATA{
	public:
		String^ title;
		String^ body;
	};
	
	private: const static int MAX = 256; 

	private: array<OBJECT_DATA^>^ stringData;

	// 保存用の文字列を作成する
	private: String^ SaveTextFileName(void){
		return "aaa";
	}
	// データを保存する
	private: void SaveTextFileName(String^ SaveString, String^ FileName){
		System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(FileName,false,System::Text::Encoding::GetEncoding(932)); // タブごとに確認
		sw->Write(SaveString);
		sw->Close();
	}	

	protected: 


	protected: 

	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->listBoxTitle = (gcnew System::Windows::Forms::ListBox());
			this->richTextBoxBody = (gcnew System::Windows::Forms::RichTextBox());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->ファイルFToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->menuSave = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->textBoxTitle = (gcnew System::Windows::Forms::TextBox());
			this->buttonNew = (gcnew System::Windows::Forms::Button());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// listBoxTitle
			// 
			this->listBoxTitle->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
				| System::Windows::Forms::AnchorStyles::Left));
			this->listBoxTitle->FormattingEnabled = true;
			this->listBoxTitle->HorizontalScrollbar = true;
			this->listBoxTitle->ItemHeight = 12;
			this->listBoxTitle->Location = System::Drawing::Point(12, 36);
			this->listBoxTitle->Name = L"listBoxTitle";
			this->listBoxTitle->Size = System::Drawing::Size(241, 376);
			this->listBoxTitle->TabIndex = 0;
			this->listBoxTitle->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBoxTitle_SelectedIndexChanged);
			// 
			// richTextBoxBody
			// 
			this->richTextBoxBody->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
				| System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->richTextBoxBody->Location = System::Drawing::Point(259, 59);
			this->richTextBoxBody->Name = L"richTextBoxBody";
			this->richTextBoxBody->Size = System::Drawing::Size(361, 377);
			this->richTextBoxBody->TabIndex = 1;
			this->richTextBoxBody->Text = L"";
			this->richTextBoxBody->TextChanged += gcnew System::EventHandler(this, &Form1::richTextBoxBody_TextChanged);
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->ファイルFToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(632, 24);
			this->menuStrip1->TabIndex = 2;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// ファイルFToolStripMenuItem
			// 
			this->ファイルFToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->menuSave});
			this->ファイルFToolStripMenuItem->Name = L"ファイルFToolStripMenuItem";
			this->ファイルFToolStripMenuItem->Size = System::Drawing::Size(66, 20);
			this->ファイルFToolStripMenuItem->Text = L"ファイル(&F)";
			// 
			// menuSave
			// 
			this->menuSave->Name = L"menuSave";
			this->menuSave->Size = System::Drawing::Size(109, 22);
			this->menuSave->Text = L"保存(&S)";
			this->menuSave->Click += gcnew System::EventHandler(this, &Form1::menuSave_Click);
			// 
			// saveFileDialog1
			// 
			this->saveFileDialog1->Filter = L"テキストファイル(*.txt)|*.txt|すべてのファイル(*.*)|*.*";
			// 
			// textBoxTitle
			// 
			this->textBoxTitle->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->textBoxTitle->Location = System::Drawing::Point(259, 36);
			this->textBoxTitle->Name = L"textBoxTitle";
			this->textBoxTitle->Size = System::Drawing::Size(361, 19);
			this->textBoxTitle->TabIndex = 3;
			this->textBoxTitle->TextChanged += gcnew System::EventHandler(this, &Form1::textBoxTitle_TextChanged);
			// 
			// buttonNew
			// 
			this->buttonNew->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
			this->buttonNew->Location = System::Drawing::Point(12, 418);
			this->buttonNew->Name = L"buttonNew";
			this->buttonNew->Size = System::Drawing::Size(241, 23);
			this->buttonNew->TabIndex = 4;
			this->buttonNew->Text = L"新規追加";
			this->buttonNew->UseVisualStyleBackColor = true;
			this->buttonNew->Click += gcnew System::EventHandler(this, &Form1::buttonNew_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(632, 446);
			this->Controls->Add(this->buttonNew);
			this->Controls->Add(this->textBoxTitle);
			this->Controls->Add(this->richTextBoxBody);
			this->Controls->Add(this->listBoxTitle);
			this->Controls->Add(this->menuStrip1);
			this->KeyPreview = true;
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"MultiEditor";
			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		// 保存を押したら
private: System::Void menuSave_Click(System::Object^  sender, System::EventArgs^  e) {

			 String^ save = "";
			 for(int i=0; i<this->listBoxTitle->Items->Count; i++){
				save += "//" + stringData[i]->title+ "\r\n" + stringData[i]->body;
			 }
			 if(saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK){
				System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(saveFileDialog1->FileName,false,System::Text::Encoding::GetEncoding(932));
				sw->Write( save );
				sw->Close();
			}
			
		 }

		 // リストボックス選択番号が変わったら
private: System::Void listBoxTitle_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 this->richTextBoxBody->Text = stringData[this->listBoxTitle->SelectedIndex]->body;
			 this->textBoxTitle->Text = this->listBoxTitle->Text;
		 }
private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
			 
			 switch(e->KeyCode){
				// アプリケーションの終了
				case System::Windows::Forms::Keys::Escape:
					Application::Exit();
					break;
				
				// リストボックスの削除
				case System::Windows::Forms::Keys::Delete:
					if( this->listBoxTitle->SelectedIndex != -1 )
						this->listBoxTitle->Items->RemoveAt( this->listBoxTitle->SelectedIndex );
					break;
				
			 }
			 
			 // 新規追加
			 if (e->Control == true){
				 switch (e->KeyCode){
					case System::Windows::Forms::Keys::N:
						int i = this->listBoxTitle->SelectedIndex;
						if( i != -1 ) this->listBoxTitle->Items[i] = i.ToString();
						break;
				 }
			 }
		 }
		 // 右上テキストが変わったら
private: System::Void textBoxTitle_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 int i = this->listBoxTitle->SelectedIndex;
			 this->richTextBoxBody->Text = "" + i;
			 if( i >= 0 && i < MAX ){
				 stringData[i]->title = this->textBoxTitle->Text;
				 //this->listBoxTitle->Items[1]->ToString() = "aa";
				 this->listBoxTitle->Items[1] = stringData[i]->title + i;
			 }

			/*
			try{
			}catch(Exception^){
			}
			*/

		 }
private: System::Void buttonNew_Click(System::Object^  sender, System::EventArgs^  e) {
			 int i = this->listBoxTitle->Items->Count;
			 stringData[i]->title = "New";
			 stringData[i]->body = "";
			 this->listBoxTitle->Items->Add("New");
		 }
private: System::Void richTextBoxBody_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			  stringData[this->listBoxTitle->SelectedIndex]->body = this->richTextBoxBody->Text;
		 }
};
}

// 0309 0630~0715

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月23日(土) 19:04
by もみじ
1点目はやはり改行コードがおかしいみたいです。
何とか治したいのですが修正できません・・・。

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月24日(日) 22:30
by もみじ
修正して強制終了することがなくなりました!!
しかし、読み込みの時に改行コードが2行入ってしまいます。
また、読み込みがおかしいのかも知れないのですが
最初の要素の(//)一つ目の行と最終行だけ拾ってこないことに気がつきました。

どうしても原因がわからないのですがアドバイスを再度頂けないでしょうか・・・。


コード:

#pragma once

//#define MAX 256

namespace MultiEditor {

	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>
	/// Form1 の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//

			int index = -1;

			stringData = gcnew array<OBJECT_DATA^>(MAX);
			for(int i=0; i<MAX; i++){
				stringData[i] = gcnew OBJECT_DATA;
			}
			
			// 読み込み
			System::IO::StreamReader^ sr = 
				gcnew System::IO::StreamReader("XML.txt",System::Text::Encoding::GetEncoding(932));
		
			sr->ReadLine();
			String^ Item;
			while((Item = sr->ReadLine()) != nullptr){
				String^ str = Item;
				if(str->IndexOf("//") >= 0) index++;//->Substring(0,2)
				
				// 値を挿入する
				if( index != -1 ){
					if( str->IndexOf("//") >= 0 ) stringData[index]->title = Item->Replace("//","");
					else stringData[index]->body += Item + "\n";
				}				
			}
			sr->Close();

			// 要素を格納する
			for(int i=0; i<index; i++){
				this->listBoxTitle->Items->Add(stringData[i]->title);
			}

		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListBox^  listBoxTitle;
	private: System::Windows::Forms::RichTextBox^  richTextBoxBody;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  ファイルFToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  menuSave;
	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
	private: System::Windows::Forms::TextBox^  textBoxTitle;
	private: System::Windows::Forms::Button^  buttonNew;
	
			 
	// オリジナルクラス
	ref class OBJECT_DATA{
	public:
		String^ title;
		String^ body;
	};
	
	private: const static int MAX = 256; 

	private: array<OBJECT_DATA^>^ stringData;

	// 保存用の文字列を作成する
	private: String^ SaveTextFileName(void){
		return "aaa";
	}
	// データを保存する
	private: void SaveTextFileName(String^ SaveString, String^ FileName){
		System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(FileName,false,System::Text::Encoding::GetEncoding(932)); // タブごとに確認
		sw->Write(SaveString);
		sw->Close();
	}	

	protected: 


	protected: 

	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->listBoxTitle = (gcnew System::Windows::Forms::ListBox());
			this->richTextBoxBody = (gcnew System::Windows::Forms::RichTextBox());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->ファイルFToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->menuSave = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->textBoxTitle = (gcnew System::Windows::Forms::TextBox());
			this->buttonNew = (gcnew System::Windows::Forms::Button());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// listBoxTitle
			// 
			this->listBoxTitle->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
				| System::Windows::Forms::AnchorStyles::Left));
			this->listBoxTitle->FormattingEnabled = true;
			this->listBoxTitle->HorizontalScrollbar = true;
			this->listBoxTitle->ItemHeight = 12;
			this->listBoxTitle->Location = System::Drawing::Point(12, 36);
			this->listBoxTitle->Name = L"listBoxTitle";
			this->listBoxTitle->Size = System::Drawing::Size(241, 376);
			this->listBoxTitle->TabIndex = 0;
			this->listBoxTitle->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBoxTitle_SelectedIndexChanged);
			// 
			// richTextBoxBody
			// 
			this->richTextBoxBody->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
				| System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->richTextBoxBody->Location = System::Drawing::Point(259, 59);
			this->richTextBoxBody->Name = L"richTextBoxBody";
			this->richTextBoxBody->Size = System::Drawing::Size(361, 377);
			this->richTextBoxBody->TabIndex = 1;
			this->richTextBoxBody->Text = L"";
			this->richTextBoxBody->TextChanged += gcnew System::EventHandler(this, &Form1::richTextBoxBody_TextChanged);
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->ファイルFToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(632, 24);
			this->menuStrip1->TabIndex = 2;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// ファイルFToolStripMenuItem
			// 
			this->ファイルFToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->menuSave});
			this->ファイルFToolStripMenuItem->Name = L"ファイルFToolStripMenuItem";
			this->ファイルFToolStripMenuItem->Size = System::Drawing::Size(66, 20);
			this->ファイルFToolStripMenuItem->Text = L"ファイル(&F)";
			// 
			// menuSave
			// 
			this->menuSave->Name = L"menuSave";
			this->menuSave->Size = System::Drawing::Size(109, 22);
			this->menuSave->Text = L"保存(&S)";
			this->menuSave->Click += gcnew System::EventHandler(this, &Form1::menuSave_Click);
			// 
			// saveFileDialog1
			// 
			this->saveFileDialog1->Filter = L"テキストファイル(*.txt)|*.txt|すべてのファイル(*.*)|*.*";
			// 
			// textBoxTitle
			// 
			this->textBoxTitle->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->textBoxTitle->Location = System::Drawing::Point(259, 36);
			this->textBoxTitle->Name = L"textBoxTitle";
			this->textBoxTitle->Size = System::Drawing::Size(361, 19);
			this->textBoxTitle->TabIndex = 3;
			this->textBoxTitle->TextChanged += gcnew System::EventHandler(this, &Form1::textBoxTitle_TextChanged);
			// 
			// buttonNew
			// 
			this->buttonNew->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
			this->buttonNew->Location = System::Drawing::Point(12, 418);
			this->buttonNew->Name = L"buttonNew";
			this->buttonNew->Size = System::Drawing::Size(241, 23);
			this->buttonNew->TabIndex = 4;
			this->buttonNew->Text = L"新規追加";
			this->buttonNew->UseVisualStyleBackColor = true;
			this->buttonNew->Click += gcnew System::EventHandler(this, &Form1::buttonNew_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(632, 446);
			this->Controls->Add(this->buttonNew);
			this->Controls->Add(this->textBoxTitle);
			this->Controls->Add(this->richTextBoxBody);
			this->Controls->Add(this->listBoxTitle);
			this->Controls->Add(this->menuStrip1);
			this->KeyPreview = true;
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"MultiEditor";
			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		// 保存を押したら
private: System::Void menuSave_Click(System::Object^  sender, System::EventArgs^  e) {

			 String^ save = "";
			 for(int i=0; i<this->listBoxTitle->Items->Count; i++){
				save += "//" + stringData[i]->title+ "\r\n" + stringData[i]->body;
			 }
			 if(saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK){
				System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(saveFileDialog1->FileName,false,System::Text::Encoding::GetEncoding(932));
				sw->Write( save );
				sw->Close();
			}
			
		 }

		 // リストボックス選択番号が変わったら
private: System::Void listBoxTitle_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			  int i = this->listBoxTitle->SelectedIndex;
			  if( i != -1 ){
				  this->richTextBoxBody->Text = stringData[i]->body;
				  this->textBoxTitle->Text = this->listBoxTitle->Text;
			  }
		 }
private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
			 
			 switch(e->KeyCode){
				// アプリケーションの終了
				case System::Windows::Forms::Keys::Escape:
					Application::Exit();
					break;
				
				// リストボックスの削除
				case System::Windows::Forms::Keys::Delete:
					if( this->listBoxTitle->SelectedIndex != -1 )
						this->listBoxTitle->Items->RemoveAt( this->listBoxTitle->SelectedIndex );
					break;
				
			 }
			 
			 // 新規追加
			 if (e->Control == true){
				 switch (e->KeyCode){
					case System::Windows::Forms::Keys::N:
						int i = this->listBoxTitle->SelectedIndex;
						if( i != -1 ) this->listBoxTitle->Items[i] = i.ToString();
						break;
				 }
			 }
		 }
		 // 右上テキストが変わったら
private: System::Void textBoxTitle_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 int i = this->listBoxTitle->SelectedIndex;
			 //this->richTextBoxBody->Text = "" + i;
			 if( i != -1 ){
				 stringData[i]->title = this->textBoxTitle->Text;
				 this->listBoxTitle->Items[i] = stringData[i]->title;
			 }

			/*
			try{
			}catch(Exception^){
			}
			*/

		 }
private: System::Void buttonNew_Click(System::Object^  sender, System::EventArgs^  e) {
			 int i = this->listBoxTitle->Items->Count;
			 stringData[i]->title = "New";
			 stringData[i]->body = "";
			 this->listBoxTitle->Items->Add("New");
		 }
private: System::Void richTextBoxBody_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			  stringData[this->listBoxTitle->SelectedIndex]->body = this->richTextBoxBody->Text;
		 }
};
}

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月25日(月) 12:31
by ookami
ソースが長いのでもうちょっと説明がほしいところですが、

コード:

            sr->ReadLine();
            String^ Item;
            while((Item = sr->ReadLine()) != nullptr){
                String^ str = Item;
                if(str->IndexOf("//") >= 0) index++;//->Substring(0,2)
                
                // 値を挿入する
                if( index != -1 ){
                    if( str->IndexOf("//") >= 0 ) stringData[index]->title = Item->Replace("//","");
                    else stringData[index]->body += Item + "\n";
                }               
            }
            sr->Close();
 
            // 要素を格納する
            for(int i=0; i<index; i++){
                this->listBoxTitle->Items->Add(stringData[i]->title);
            }
の、最初の
sr->ReadLine();
を消すのと、
for(int i=0; i<index; i++){

for(int i=0; i<=index; i++){
ではないでしょうか?

Re: オリジナルのまとめて管理できるメモ帳を作りたい

Posted: 2011年4月27日(水) 00:36
by もみじ
ookami様
修正した所、読み込めました。
コメントが少なくて申し訳ありませんでした。
長いソースなのにありがとうございました!!