VS C++ Windows Form HELP!

Greetings this is my first post i've searched long and hard on the web for an answer so i'm hoping you guys can help. I'm going to do my best to describe my problem.

Program: Visual Studio 2010 Ultimate . C++ Windows Form

Error: System Cannot find the file specified.


What my program does. It opens a Form .

1) Form loads and Reads the FilePath that I saved in a line of text in example.txt
2) Stores the FilePath from example.txt into textBox1.
3) To change FilePath .. Press button1 and it will OpenFileDialog and allow user to navigate and find the file they want.
4) This New FilePath is saved in example.txt and textBox1 (overwrites the old)
5) By Pressing Button2 I can execute the file thats listed in textBox1

NOW This works all beautiful but the reason I even want to store the FilePath in a text file is because I don't want the user to continue changing paths for a file. So normally the steps would be in most times.

1) Reads the FilePath that I saved in a line of text in example.txt
2) Stores the FilePath from example.txt into textBox1
3) By Pressing Button2 I execute the file that's listed in textBox1

So what happens when I do this. Instead of executing whatever filepath is in textBox1 it gives me an error on Button2

String^ filePath = textBox1->Text;
Process::Start(filePath)

Is there another way to launch whatever is in textBox1??? Any help would be appreciated I hope I explained my problem well. In BOLD are the changes to the program that I wrote.






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  #pragma once

#include <iostream>
#include <fstream>
#include <string>


namespace test2 {

	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 namespace std;
	using namespace System::Diagnostics;
	using namespace System::IO;
	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			textBox1->Text = System::IO::File::ReadAllText("example.txt");
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TextBox^  textBox1;
	protected: 


	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;

	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->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(58, 117);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 20);
			this->textBox1->TabIndex = 0;
			this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(197, 117);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 3;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(197, 146);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(75, 23);
			this->button2->TabIndex = 4;
			this->button2->Text = L"button2";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 261);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->textBox1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 }


	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->ShowDialog();
textBox1->Text=openFileDialog1->FileName;

String^ fileName = "example.txt";
StreamWriter^ sw = gcnew StreamWriter(fileName);
sw->WriteLine(textBox1->Text) 

	;
sw->Close();

			 }
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			 
				 String^ filePath = textBox1->Text;
				Process::Start(filePath);
			 }
};
}
Last edited on
closed account (z05DSL3A)
Your problem is that you will have a carriage return at the end of the file name in the text file, from sw->WriteLine (). If you replace that with just sw->Write() to test it, then you can decide the best way to fix it for your actual code.

Or you could remove the carriage return from the text file to test.
Last edited on by Canis lupus
Wow unbelievable I spent so long trying to figure this out thanks for your help i'll be here quite shortly lol. I'm guaranteed to run into more issues here is what my old line and new line. Works great!! I find it hard finding support for windows form C++

sw->WriteLine(textBox1->Text);

is now:

sw->Write(textBox1->Text);



closed account (z05DSL3A)
"windows form C++" is actually called C++/CLI not to be confused with C++. see http://en.wikipedia.org/wiki/C%2B%2B/CLI.

Not many of the member here use it... probably less even like it.
Topic archived. No new replies allowed.