Как использовать переменную в которой лежит путь к файлу?
09.12.2023, 13:57. Показов 854. Ответов 2
Перерыл весь интернет, не нашел ответа на свой вопрос, делаю приложение в winform на языке c++, в котором две формы, MyForm и MyForm1, из MyForm передаю в MyForm1 переменную в которой лежит путь к файлу, его потом использую в StreamWriter, но после запуска Visual Studio ругается, мол "Данный формат пути не поддерживается", именно этот путь из переменной мне нужен для того что бы сохранить данные из textBox(ов).
Помогите пожалуйста, уже два дня пытаюсь реализовать это, но ничего не получается
Вот код
Первая форма:
| C++ | 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
| #pragma once
#include "MyForm1.h"
namespace Testi {
using namespace System;
using namespace System::IO;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Сводка для MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm()
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ savePathTextBox;
protected:
protected:
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->savePathTextBox = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button2
//
this->button2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(51)), static_cast<System::Int32>(static_cast<System::Byte>(150)),
static_cast<System::Int32>(static_cast<System::Byte>(255)));
this->button2->Font = (gcnew System::Drawing::Font(L"Times New Roman", 13.8F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->button2->Location = System::Drawing::Point(12, 257);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(471, 67);
this->button2->TabIndex = 1;
this->button2->Text = L"Пройти тест";
this->button2->UseVisualStyleBackColor = false;
//
// button3
//
this->button3->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(51)), static_cast<System::Int32>(static_cast<System::Byte>(150)),
static_cast<System::Int32>(static_cast<System::Byte>(255)));
this->button3->Font = (gcnew System::Drawing::Font(L"Times New Roman", 13.8F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->button3->Location = System::Drawing::Point(12, 330);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(471, 67);
this->button3->TabIndex = 2;
this->button3->Text = L"Редактировать тест";
this->button3->UseVisualStyleBackColor = false;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Times New Roman", 16.2F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->label1->Location = System::Drawing::Point(44, 9);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(407, 99);
this->label1->TabIndex = 3;
this->label1->Text = L"Добро пожаловать!\r\n\r\nВыбирите нужное вам действие: \r\n";
this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Times New Roman", 9, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->label2->Location = System::Drawing::Point(66, 404);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(427, 17);
this->label2->TabIndex = 4;
this->label2->Text = L"Разработано специально для МБОУ \"Усть-Удинска СОШ №2\"";
//
// button1
//
this->button1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(51)), static_cast<System::Int32>(static_cast<System::Byte>(150)),
static_cast<System::Int32>(static_cast<System::Byte>(255)));
this->button1->Font = (gcnew System::Drawing::Font(L"Times New Roman", 13.8F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->button1->Location = System::Drawing::Point(12, 184);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(471, 67);
this->button1->TabIndex = 5;
this->button1->Text = L"Создать тест";
this->button1->UseVisualStyleBackColor = false;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
//
// savePathTextBox
//
this->savePathTextBox->Location = System::Drawing::Point(2, 404);
this->savePathTextBox->Multiline = true;
this->savePathTextBox->Name = L"savePathTextBox";
this->savePathTextBox->Size = System::Drawing::Size(10, 10);
this->savePathTextBox->TabIndex = 6;
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::Gainsboro;
this->ClientSize = System::Drawing::Size(495, 422);
this->Controls->Add(this->savePathTextBox);
this->Controls->Add(this->button1);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->ForeColor = System::Drawing::Color::Black;
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
this->Name = L"MyForm";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"Программа для создания тестов";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog();
saveFileDialog1->Filter = "TextFiles (*.txt)|*.txt|All files(*.*)|*.*";
saveFileDialog1->FilterIndex = 1;
saveFileDialog1->RestoreDirectory = true;
saveFileDialog1->CreatePrompt = true;
saveFileDialog1->OverwritePrompt = true;
saveFileDialog1->FileName = "VoprosKtestu";
saveFileDialog1->InitialDirectory = "C://Users//Msi//Desktop//VoprosKtestu.txt";
if (saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
System::IO::File::WriteAllText(saveFileDialog1->FileName, L"");
}
MyForm1^ openForm1 = gcnew MyForm1();
savePathTextBox->Text = Convert::ToString(saveFileDialog1);
openForm1->path = savePathTextBox->Text;
openForm1->Owner = this;
openForm1->Show();
this->Hide();
}
};
} |
|
Вторая форма:
| C++ | 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
| #pragma once
#include "MyForm.h"
namespace Testi {
using namespace System;
using namespace System::IO;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Сводка для MyForm1
/// </summary>
public ref class MyForm1 : public System::Windows::Forms::Form
{
public:
MyForm1(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
// Остальной код формы
public:
String^ path;
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~MyForm1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ Vopros;
protected:
private: System::Windows::Forms::Button^ BackButton;
private: System::Windows::Forms::Button^ Zavershit;
private: System::Windows::Forms::Button^ ButtonNext;
private: System::Windows::Forms::CheckBox^ OTV1;
private: System::Windows::Forms::CheckBox^ OTV2;
private: System::Windows::Forms::CheckBox^ OTV3;
private: System::Windows::Forms::CheckBox^ OTV4;
private: System::Windows::Forms::TextBox^ VoprosPole;
private: System::Windows::Forms::TextBox^ OTVpole1;
private: System::Windows::Forms::TextBox^ OTVpole2;
private: System::Windows::Forms::TextBox^ OTVpole3;
private: System::Windows::Forms::TextBox^ OTVpole4;
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->Vopros = (gcnew System::Windows::Forms::TextBox());
this->BackButton = (gcnew System::Windows::Forms::Button());
this->Zavershit = (gcnew System::Windows::Forms::Button());
this->ButtonNext = (gcnew System::Windows::Forms::Button());
this->OTV1 = (gcnew System::Windows::Forms::CheckBox());
this->OTV2 = (gcnew System::Windows::Forms::CheckBox());
this->OTV3 = (gcnew System::Windows::Forms::CheckBox());
this->OTV4 = (gcnew System::Windows::Forms::CheckBox());
this->VoprosPole = (gcnew System::Windows::Forms::TextBox());
this->OTVpole1 = (gcnew System::Windows::Forms::TextBox());
this->OTVpole2 = (gcnew System::Windows::Forms::TextBox());
this->OTVpole3 = (gcnew System::Windows::Forms::TextBox());
this->OTVpole4 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// Vopros
//
this->Vopros->BackColor = System::Drawing::Color::Silver;
this->Vopros->BorderStyle = System::Windows::Forms::BorderStyle::None;
this->Vopros->Font = (gcnew System::Drawing::Font(L"Times New Roman", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->Vopros->Location = System::Drawing::Point(138, 36);
this->Vopros->Name = L"Vopros";
this->Vopros->Size = System::Drawing::Size(228, 23);
this->Vopros->TabIndex = 0;
//
// BackButton
//
this->BackButton->Font = (gcnew System::Drawing::Font(L"Times New Roman", 10.8F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->BackButton->Location = System::Drawing::Point(12, 368);
this->BackButton->Name = L"BackButton";
this->BackButton->Size = System::Drawing::Size(115, 46);
this->BackButton->TabIndex = 1;
this->BackButton->Text = L"<-Назад";
this->BackButton->UseVisualStyleBackColor = true;
this->BackButton->Click += gcnew System::EventHandler(this, &MyForm1::BackButton_Click);
//
// Zavershit
//
this->Zavershit->Font = (gcnew System::Drawing::Font(L"Times New Roman", 10.8F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->Zavershit->Location = System::Drawing::Point(138, 368);
this->Zavershit->Name = L"Zavershit";
this->Zavershit->Size = System::Drawing::Size(228, 46);
this->Zavershit->TabIndex = 2;
this->Zavershit->Text = L"Завершить";
this->Zavershit->UseVisualStyleBackColor = true;
this->Zavershit->Click += gcnew System::EventHandler(this, &MyForm1::Zavershit_Click);
//
// ButtonNext
//
this->ButtonNext->Font = (gcnew System::Drawing::Font(L"Times New Roman", 10.8F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->ButtonNext->Location = System::Drawing::Point(373, 368);
this->ButtonNext->Name = L"ButtonNext";
this->ButtonNext->Size = System::Drawing::Size(115, 46);
this->ButtonNext->TabIndex = 3;
this->ButtonNext->Text = L"Далее->";
this->ButtonNext->UseVisualStyleBackColor = true;
this->ButtonNext->Click += gcnew System::EventHandler(this, &MyForm1::ButtonNext_Click);
//
// OTV1
//
this->OTV1->AutoSize = true;
this->OTV1->Location = System::Drawing::Point(17, 145);
this->OTV1->Name = L"OTV1";
this->OTV1->Size = System::Drawing::Size(18, 17);
this->OTV1->TabIndex = 5;
this->OTV1->UseVisualStyleBackColor = true;
//
// OTV2
//
this->OTV2->AutoSize = true;
this->OTV2->Location = System::Drawing::Point(17, 199);
this->OTV2->Name = L"OTV2";
this->OTV2->Size = System::Drawing::Size(18, 17);
this->OTV2->TabIndex = 6;
this->OTV2->UseVisualStyleBackColor = true;
//
// OTV3
//
this->OTV3->AutoSize = true;
this->OTV3->Location = System::Drawing::Point(17, 259);
this->OTV3->Name = L"OTV3";
this->OTV3->Size = System::Drawing::Size(18, 17);
this->OTV3->TabIndex = 7;
this->OTV3->UseVisualStyleBackColor = true;
//
// OTV4
//
this->OTV4->AutoSize = true;
this->OTV4->Location = System::Drawing::Point(17, 317);
this->OTV4->Name = L"OTV4";
this->OTV4->Size = System::Drawing::Size(18, 17);
this->OTV4->TabIndex = 8;
this->OTV4->UseVisualStyleBackColor = true;
//
// VoprosPole
//
this->VoprosPole->Location = System::Drawing::Point(17, 12);
this->VoprosPole->Multiline = true;
this->VoprosPole->Name = L"VoprosPole";
this->VoprosPole->Size = System::Drawing::Size(470, 86);
this->VoprosPole->TabIndex = 13;
this->VoprosPole->Click += gcnew System::EventHandler(this, &MyForm1::VoprosPole_Click);
this->VoprosPole->TextChanged += gcnew System::EventHandler(this, &MyForm1::VoprosPole_TextChanged);
//
// OTVpole1
//
this->OTVpole1->Location = System::Drawing::Point(60, 123);
this->OTVpole1->Multiline = true;
this->OTVpole1->Name = L"OTVpole1";
this->OTVpole1->Size = System::Drawing::Size(400, 50);
this->OTVpole1->TabIndex = 14;
this->OTVpole1->TextChanged += gcnew System::EventHandler(this, &MyForm1::OTVpole1_TextChanged);
//
// OTVpole2
//
this->OTVpole2->Location = System::Drawing::Point(59, 182);
this->OTVpole2->Multiline = true;
this->OTVpole2->Name = L"OTVpole2";
this->OTVpole2->Size = System::Drawing::Size(400, 50);
this->OTVpole2->TabIndex = 15;
this->OTVpole2->TextChanged += gcnew System::EventHandler(this, &MyForm1::OTVpole2_TextChanged);
//
// OTVpole3
//
this->OTVpole3->Location = System::Drawing::Point(60, 242);
this->OTVpole3->Multiline = true;
this->OTVpole3->Name = L"OTVpole3";
this->OTVpole3->Size = System::Drawing::Size(400, 50);
this->OTVpole3->TabIndex = 16;
this->OTVpole3->TextChanged += gcnew System::EventHandler(this, &MyForm1::OTVpole3_TextChanged);
//
// OTVpole4
//
this->OTVpole4->Location = System::Drawing::Point(60, 300);
this->OTVpole4->Multiline = true;
this->OTVpole4->Name = L"OTVpole4";
this->OTVpole4->Size = System::Drawing::Size(400, 50);
this->OTVpole4->TabIndex = 17;
this->OTVpole4->TextChanged += gcnew System::EventHandler(this, &MyForm1::OTVpole4_TextChanged);
//
// MyForm1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->AutoSize = true;
this->BackColor = System::Drawing::Color::Silver;
this->ClientSize = System::Drawing::Size(499, 426);
this->Controls->Add(this->OTVpole4);
this->Controls->Add(this->OTVpole3);
this->Controls->Add(this->OTVpole2);
this->Controls->Add(this->OTVpole1);
this->Controls->Add(this->VoprosPole);
this->Controls->Add(this->OTV4);
this->Controls->Add(this->OTV3);
this->Controls->Add(this->OTV2);
this->Controls->Add(this->OTV1);
this->Controls->Add(this->ButtonNext);
this->Controls->Add(this->Zavershit);
this->Controls->Add(this->BackButton);
this->Controls->Add(this->Vopros);
this->Name = L"MyForm1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"Вопрос 1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void BackButton_Click(System::Object^ sender, System::EventArgs^ e) {
Owner->Show();
this->Close();
}
private: System::Void VoprosPole_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void ButtonNext_Click(System::Object^ sender, System::EventArgs^ e) {
StreamWriter^ fileTxt = gcnew StreamWriter(path);
fileTxt->Write(VoprosPole->Text + "\n" + OTVpole1->Text + "\n" + OTVpole2->Text + "\n" + OTVpole3->Text + "\n" + OTVpole4->Text + "\n");
fileTxt->Close();
MessageBox::Show(this, "Вопрос сохранен","Сообщение", MessageBoxButtons::OK, MessageBoxIcon::Information);
VoprosPole->Text = "";
MyForm1^ FormN = gcnew MyForm1();
FormN->Show();
this->Close();
}
private: System::Void Zavershit_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void OTVpole1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void OTVpole2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void OTVpole3_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void OTVpole4_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void VoprosPole_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
} |
|
0
|