10.07.2015, 14:44. Показов 550. Ответов 0
Доброго времени суток. Начнём с предыстории. В ходе попытки разработки проекта возникли некоторые трудности.
Создал, я значит, три формы New, Password, Edit. По моему замыслу New - основная форма. Редактировать в ней XML нельзя. При нажатие в ней editToolStripMenuItem1 вызывается форма Password с полем ввода пароля. При вводе правильного пароля, появляется форма Edit в которой возможно редактирование записи. Однако в ходе попытки разработки проекта возникли некоторые трудности.
Во-первых, это как восстановить форму New при закрытие формы Edit. Я прочитал, что можно через событие закрытия одной формы восстановить видимость другой, но как это сделать не очень понятно. Так что не могли бы вы пояснить.
Форма New:
Кликните здесь для просмотра всего текста
| 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
| #pragma once
#include "Password.h"
#include "Global.h"
#include "Edit.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
namespace test {
...
private: System::Windows::Forms::MenuStrip^ menuStrip1;
protected:
private: System::Data::DataSet^ StorageDataSet;
public: System::Windows::Forms::DataGridView^ dataGridView1;
private: System::Windows::Forms::ToolStripMenuItem^ editToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ editToolStripMenuItem1;
private: System::Windows::Forms::ToolStripMenuItem^ cearchToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ gameToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ exitToolStripMenuItem;
public:
private:
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(New::typeid));
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->editToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->editToolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->cearchToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->gameToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->StorageDataSet = (gcnew System::Data::DataSet());
this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->menuStrip1->SuspendLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->StorageDataSet))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->BeginInit();
this->SuspendLayout();
//
// menuStrip1
//
resources->ApplyResources(this->menuStrip1, L"menuStrip1");
this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->editToolStripMenuItem });
this->menuStrip1->Name = L"menuStrip1";
//
// editToolStripMenuItem
//
resources->ApplyResources(this->editToolStripMenuItem, L"editToolStripMenuItem");
this->editToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(4) {
this->editToolStripMenuItem1,
this->cearchToolStripMenuItem, this->gameToolStripMenuItem, this->exitToolStripMenuItem
});
this->editToolStripMenuItem->Name = L"editToolStripMenuItem";
//
// editToolStripMenuItem1
//
resources->ApplyResources(this->editToolStripMenuItem1, L"editToolStripMenuItem1");
this->editToolStripMenuItem1->Name = L"editToolStripMenuItem1";
this->editToolStripMenuItem1->Click += gcnew System::EventHandler(this, &New::editToolStripMenuItem1_Click);
//
// cearchToolStripMenuItem
//
resources->ApplyResources(this->cearchToolStripMenuItem, L"cearchToolStripMenuItem");
this->cearchToolStripMenuItem->Name = L"cearchToolStripMenuItem";
//
// gameToolStripMenuItem
//
resources->ApplyResources(this->gameToolStripMenuItem, L"gameToolStripMenuItem");
this->gameToolStripMenuItem->Name = L"gameToolStripMenuItem";
//
// StorageDataSet
//
this->StorageDataSet->DataSetName = L"NewDataSet";
//
// dataGridView1
//
resources->ApplyResources(this->dataGridView1, L"dataGridView1");
this->dataGridView1->AllowUserToAddRows = false;
this->dataGridView1->AllowUserToDeleteRows = false;
this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
this->dataGridView1->Name = L"dataGridView1";
this->dataGridView1->ReadOnly = true;
//
// exitToolStripMenuItem
//
resources->ApplyResources(this->exitToolStripMenuItem, L"exitToolStripMenuItem");
this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
//
// New
//
resources->ApplyResources(this, L"$this");
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->Controls->Add(this->dataGridView1);
this->Controls->Add(this->menuStrip1);
this->Cursor = System::Windows::Forms::Cursors::Arrow;
this->MainMenuStrip = this->menuStrip1;
this->MaximizeBox = false;
this->Name = L"New";
this->Load += gcnew System::EventHandler(this, &New::New_Load);
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->StorageDataSet))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
DataTable ^ Таблица;
DataSet ^ ВводДанных;
private: System::Void New_Load(System::Object^ sender, System::EventArgs^ e) {
this->Text = "Библиотека";
Таблица = gcnew DataTable();
ВводДанных = gcnew DataSet();
if (IO::File::Exists("baza.xml") == false)
{
dataGridView1->DataSource = Таблица;
Таблица->Columns->Add("Шифр");
Таблица->Columns->Add("Фамилия");
Таблица->Columns->Add("Название");
Таблица->Columns->Add("Город");
Таблица->Columns->Add("Год");
Таблица->Columns->Add("Кол-во страниц");
ВводДанных->Tables->Add(Таблица);
}
else
{
ВводДанных->ReadXml("baza.xml");
String ^ СтрокаXML = ВводДанных->GetXml();
dataGridView1->DataMember = ("Название таблицы");
dataGridView1->DataSource = ВводДанных;
}
}
private: System::Void saveToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
Таблица->TableName = "Библиотека";
ВводДанных->WriteXml("baza.xml");
}
private: System::Void editToolStripMenuItem1_Click(System::Object^ sender, System::EventArgs^ e) {
if(Global::flag1)
{
Edit^ gEdit = gcnew Edit;
gEdit->Show();
gEdit->Visible = true;
this->Visible = false;
}
else
{
Password^ gPassword = gcnew Password;
gPassword->Show();
gPassword->Visible = true;
this->Visible = false;
}
}
};
} |
|
Edit:
Кликните здесь для просмотра всего текста
| 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
| private: System::Windows::Forms::DataGridView^ dataGridView1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
protected:
private:
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->BeginInit();
this->SuspendLayout();
//
// dataGridView1
//
this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
this->dataGridView1->Location = System::Drawing::Point(12, 12);
this->dataGridView1->Name = L"dataGridView1";
this->dataGridView1->Size = System::Drawing::Size(610, 377);
this->dataGridView1->TabIndex = 0;
//
// button1
//
this->button1->Location = System::Drawing::Point(58, 395);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(175, 54);
this->button1->TabIndex = 1;
this->button1->Text = L"Сохранить";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Edit::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(380, 395);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(175, 54);
this->button2->TabIndex = 2;
this->button2->Text = L"Вернутся";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Edit::button2_Click);
//
// Edit
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(634, 461);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->dataGridView1);
this->MaximizeBox = false;
this->Name = L"Edit";
this->Text = L"Редактирование";
this->Load += gcnew System::EventHandler(this, &Edit::Edit_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
DataTable ^ Table;
DataSet ^ Input;
private: System::Void Edit_Load(System::Object^ sender, System::EventArgs^ e) {
Table = gcnew DataTable();
Input = gcnew DataSet();
if (IO::File::Exists("baza.xml") == false)
{
dataGridView1->DataSource = Table;
Table->Columns->Add("Шифр");
Table->Columns->Add("Фамилия");
Table->Columns->Add("Название");
Table->Columns->Add("Город");
Table->Columns->Add("Год");
Table->Columns->Add("Кол-во страниц");
Input->Tables->Add(Table);
}
else
{
Input->ReadXml("baza.xml");
String ^ StringXML = Input->GetXml();
dataGridView1->DataMember = ("Название таблицы");
dataGridView1->DataSource = Input;
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Table->TableName = "Библиотека";
Input->WriteXml("baza.xml");
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
this->Close();
}
};
} |
|
Во-вторых, в связи с решение использовать формат XML возникла такая проблема. Я создал 2 по сути одинаковых элемента dataGridView в формах New и Edit. И задал для них один и тот же алгоритм исполнения, а именно,
Кликните здесь для просмотра всего текста
| 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
| #pragma endregion
DataTable ^ Таблица;
DataSet ^ ВводДанных;
private: System::Void New_Load(System::Object^ sender, System::EventArgs^ e) {
this->Text = "Библиотека";
Таблица = gcnew DataTable();
ВводДанных = gcnew DataSet();
if (IO::File::Exists("baza.xml") == false)
{
dataGridView1->DataSource = Таблица;
Таблица->Columns->Add("Шифр");
Таблица->Columns->Add("Фамилия");
Таблица->Columns->Add("Название");
Таблица->Columns->Add("Город");
Таблица->Columns->Add("Год");
Таблица->Columns->Add("Кол-во страниц");
ВводДанных->Tables->Add(Таблица);
}
else
{
ВводДанных->ReadXml("baza.xml");
String ^ СтрокаXML = ВводДанных->GetXml();
dataGridView1->DataMember = ("Название таблицы");
dataGridView1->DataSource = ВводДанных;
}
} |
|
и такой же кусок
Кликните здесь для просмотра всего текста
| 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
| #pragma endregion
DataTable ^ Table;
DataSet ^ Input;
private: System::Void Edit_Load(System::Object^ sender, System::EventArgs^ e) {
Table = gcnew DataTable();
Input = gcnew DataSet();
if (IO::File::Exists("baza.xml") == false)
{
dataGridView1->DataSource = Table;
Table->Columns->Add("Шифр");
Table->Columns->Add("Фамилия");
Table->Columns->Add("Название");
Table->Columns->Add("Город");
Table->Columns->Add("Год");
Table->Columns->Add("Кол-во страниц");
Input->Tables->Add(Table);
}
else
{
Input->ReadXml("baza.xml");
String ^ StringXML = Input->GetXml();
dataGridView1->DataMember = ("Название таблицы");
dataGridView1->DataSource = Input;
}
} |
|
Так вот при условии, что файл не создан видна, что таблица создана. Есле же попробовать отредактировать. То при повторном открытии ни в той, ни в другой форме нет возможности не редактировать не просматривать. Что же делать? Что бы в форме New была возможность просто просматривать baza.xml, а в Edit редактировать.