teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

見直しキャンペーン中

2023/07/22 09:29

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,258 +1,258 @@
1
- > 何かいい方法はありませんでしょうか?
2
-
3
- できるだけ簡単な方法を考えていたのですが、一応めどが立ったので一案として試してみてください(簡単を最優先したので、とても正攻法とは言えません^^;
4
-
5
- ```C#
6
- using System;
7
- using System.Drawing;
8
- using System.IO;
9
- using System.Net;
10
- using System.Windows.Forms;
11
-
12
- namespace Questions280412
13
- {
14
- public partial class Form1 : Form
15
- {
16
- public Form1()
17
- {
18
- InitializeComponent2();
19
-
20
- try
21
- {
22
- // 保存済みファイルがあれば読み込み
23
- webBrowser1.DocumentText = File.ReadAllText("Questions280412.html");
24
- }
25
- catch
26
- {
27
- // なければ空文字に(何かは入れないとNullReferenceExceptionが出る)
28
- webBrowser1.DocumentText = "";
29
- }
30
-
31
- // フォント変更
32
- webBrowser1.DocumentCompleted += (s, e) => webBrowser1.Document.Body.Style = "font-family:メイリオ;";
33
- }
34
-
35
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
36
- {
37
- // 終了時に自動保存
38
- // 次回起動時のためdivを表示に戻しておく
39
- foreach(HtmlElement div in webBrowser1.Document.GetElementsByTagName("div"))
40
- {
41
- div.Style = "display:block";
42
- }
43
-
44
- // ファイルが書き込めないと例外
45
- // webBrowser1.DocumentTextだと INPUT CHECKED が反映されないので注意
46
- File.WriteAllText("Questions280412.html", webBrowser1.Document.Body.InnerHtml);
47
- }
48
-
49
- private void SelectImageButton_Click(object sender, EventArgs e)
50
- {
51
- using(var dlg = new OpenFileDialog())
52
- {
53
- dlg.Filter = "画像ファイル(*.png;*.jpg;*.gif)|*.png;*.jpg;*.gif|すべてのファイル(*.*)|*.*";
54
- if(dlg.ShowDialog() == DialogResult.OK)
55
- {
56
- fileTextBox.Text = dlg.FileName;
57
- }
58
- }
59
- }
60
-
61
-
62
- // 1投稿分のテンプレート
63
- private const string template = @"
64
- <div>
65
- <h1>{{title}}</h1>
66
- <img src='{{file}}' />
67
- <br />
68
- <pre>{{comment}}</pre>
69
- <label>
70
- <input type='checkbox' name='like'>お気に入り
71
- </label>
72
- <hr />
73
- </div>";
74
-
75
- private void PostButton_Click(object sender, EventArgs e)
76
- {
77
- // HTMLエンコード(< を &lt; に変える処理)
78
- var title = WebUtility.HtmlEncode(titleTextBox.Text);
79
- var file = WebUtility.HtmlEncode(fileTextBox.Text);
80
- var comment = WebUtility.HtmlEncode(commentTextBox.Text);
81
-
82
- // テンプレートの指定部分と置換
83
- var html = template.Replace("{{title}}", title)
84
- .Replace("{{file}}", file)
85
- .Replace("{{comment}}", comment);
86
-
87
- //webBrowser1.DocumentText += html; // だとダメ
88
- webBrowser1.Document.Body.InnerHtml += html; // 投稿追加
89
- // お気に入り状態で追加されるといろいろ面倒なのでぜんぶに戻す
90
- allRadioButton.Checked = true;
91
- }
92
-
93
- private void AllRadioButton_CheckedChanged(object sender, EventArgs e)
94
- {
95
- // divタグを全列挙
96
- foreach(HtmlElement div in webBrowser1.Document.GetElementsByTagName("div"))
97
- {
98
- // divを表示に戻す
99
- div.Style = "display:block";
100
- }
101
- }
102
-
103
- private void LikeRadioButton_CheckedChanged(object sender, EventArgs e)
104
- {
105
- // 全体から name='like' のものを全列挙
106
- foreach(HtmlElement checkbox in webBrowser1.Document.All.GetElementsByName("like"))
107
- {
108
- // チェックボックスがチェックされていなければ...
109
- if(checkbox.GetAttribute("checked") == "False")
110
- {
111
- // div(チェックボックスの親の親)を非表示にする
112
- checkbox.Parent.Parent.Style = "display:none";
113
- }
114
- }
115
- }
116
-
117
-
118
-
119
- // InitializeComponent()を圧縮
120
- private void InitializeComponent2()
121
- {
122
- ((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
123
- splitContainer1.Panel1.SuspendLayout();
124
- splitContainer1.Panel2.SuspendLayout();
125
- splitContainer1.SuspendLayout();
126
- SuspendLayout();
127
-
128
- // splitContainer1
129
- splitContainer1.Dock = DockStyle.Fill;
130
-
131
- // splitContainer1.Panel1
132
- splitContainer1.Panel1.Controls.Add(label3);
133
- splitContainer1.Panel1.Controls.Add(label2);
134
- splitContainer1.Panel1.Controls.Add(label1);
135
- splitContainer1.Panel1.Controls.Add(postButton);
136
- splitContainer1.Panel1.Controls.Add(selectImageButton);
137
- splitContainer1.Panel1.Controls.Add(commentTextBox);
138
- splitContainer1.Panel1.Controls.Add(fileTextBox);
139
- splitContainer1.Panel1.Controls.Add(titleTextBox);
140
-
141
- // splitContainer1.Panel2
142
- splitContainer1.Panel2.Controls.Add(likeRadioButton);
143
- splitContainer1.Panel2.Controls.Add(allRadioButton);
144
- splitContainer1.Panel2.Controls.Add(webBrowser1);
145
- splitContainer1.Size = new Size(800, 450);
146
- splitContainer1.SplitterDistance = 408;
147
-
148
- // label3
149
- label3.AutoSize = true;
150
- label3.Location = new Point(14, 69);
151
- label3.Text = "コメント";
152
-
153
- // label2
154
- label2.AutoSize = true;
155
- label2.Location = new Point(12, 44);
156
- label2.Text = "画像パス";
157
-
158
- // label1
159
- label1.AutoSize = true;
160
- label1.Location = new Point(12, 15);
161
- label1.Text = "タイトル";
162
-
163
- // postButton
164
- postButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
165
- postButton.Location = new Point(321, 415);
166
- postButton.Size = new Size(75, 23);
167
- postButton.Text = "投稿";
168
- postButton.UseVisualStyleBackColor = true;
169
- postButton.Click += PostButton_Click;
170
-
171
- // selectImageButton
172
- selectImageButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
173
- selectImageButton.Location = new Point(299, 37);
174
- selectImageButton.Size = new Size(97, 23);
175
- selectImageButton.Text = "画像を選択...";
176
- selectImageButton.UseVisualStyleBackColor = true;
177
- selectImageButton.Click += SelectImageButton_Click;
178
-
179
- // commentTextBox
180
- commentTextBox.AcceptsReturn = true;
181
- commentTextBox.AcceptsTab = true;
182
- commentTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
183
- commentTextBox.Location = new Point(66, 66);
184
- commentTextBox.Multiline = true;
185
- commentTextBox.Size = new Size(330, 343);
186
-
187
- // fileTextBox
188
- fileTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
189
- fileTextBox.Location = new Point(66, 41);
190
- fileTextBox.Size = new Size(227, 19);
191
-
192
- // titleTextBox
193
- titleTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
194
- titleTextBox.Location = new Point(66, 12);
195
- titleTextBox.Size = new Size(330, 19);
196
-
197
- // likeRadioButton
198
- likeRadioButton.Appearance = Appearance.Button;
199
- likeRadioButton.AutoSize = true;
200
- likeRadioButton.Location = new Point(54, 5);
201
- likeRadioButton.TabStop = true;
202
- likeRadioButton.Text = "お気に入り";
203
- likeRadioButton.UseVisualStyleBackColor = true;
204
- likeRadioButton.CheckedChanged += LikeRadioButton_CheckedChanged;
205
-
206
- // allRadioButton
207
- allRadioButton.Appearance = Appearance.Button;
208
- allRadioButton.AutoSize = true;
209
- allRadioButton.Checked = true;
210
- allRadioButton.Location = new Point(3, 5);
211
- allRadioButton.TabStop = true;
212
- allRadioButton.Text = "ぜんぶ";
213
- allRadioButton.UseVisualStyleBackColor = true;
214
- allRadioButton.CheckedChanged += AllRadioButton_CheckedChanged;
215
-
216
- // webBrowser1
217
- webBrowser1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
218
- webBrowser1.Location = new Point(3, 33);
219
- webBrowser1.MinimumSize = new Size(20, 20);
220
- webBrowser1.Size = new Size(382, 414);
221
-
222
- // Form1
223
- AutoScaleDimensions = new SizeF(6F, 12F);
224
- AutoScaleMode = AutoScaleMode.Font;
225
- ClientSize = new Size(800, 450);
226
- Controls.Add(splitContainer1);
227
- Text = "Form1";
228
- FormClosing += Form1_FormClosing;
229
- splitContainer1.Panel1.ResumeLayout(false);
230
- splitContainer1.Panel1.PerformLayout();
231
- splitContainer1.Panel2.ResumeLayout(false);
232
- splitContainer1.Panel2.PerformLayout();
233
- ((System.ComponentModel.ISupportInitialize)(splitContainer1)).EndInit();
234
- splitContainer1.ResumeLayout(false);
235
- ResumeLayout(false);
236
- }
237
-
238
- private SplitContainer splitContainer1 = new SplitContainer();
239
- private WebBrowser webBrowser1 = new WebBrowser();
240
- private Label label3 = new Label();
241
- private Label label2 = new Label();
242
- private Label label1 = new Label();
243
- private Button postButton = new Button();
244
- private Button selectImageButton = new Button();
245
- private TextBox commentTextBox = new TextBox();
246
- private TextBox fileTextBox = new TextBox();
247
- private TextBox titleTextBox = new TextBox();
248
- private RadioButton likeRadioButton = new RadioButton();
249
- private RadioButton allRadioButton = new RadioButton();
250
- }
251
- }
252
- ```
253
-
254
- ![アプリ画像](48751df8700e4a94434cec3a2206f504.png)
255
- **注意**
256
- Form1.Designer.csのままだと1万字に収まらないので、しかたなくInitializeComponent2を作成。
257
- いろいろ気が利かない部分があります(改善の余地を残しています^^;
1
+ > 何かいい方法はありませんでしょうか?
2
+
3
+ できるだけ簡単な方法を考えていたのですが、一応めどが立ったので一案として試してみてください(簡単を最優先したので、とても正攻法とは言えません^^;
4
+
5
+ ```cs
6
+ using System;
7
+ using System.Drawing;
8
+ using System.IO;
9
+ using System.Net;
10
+ using System.Windows.Forms;
11
+
12
+ namespace Questions280412
13
+ {
14
+ public partial class Form1 : Form
15
+ {
16
+ public Form1()
17
+ {
18
+ InitializeComponent2();
19
+
20
+ try
21
+ {
22
+ // 保存済みファイルがあれば読み込み
23
+ webBrowser1.DocumentText = File.ReadAllText("Questions280412.html");
24
+ }
25
+ catch
26
+ {
27
+ // なければ空文字に(何かは入れないとNullReferenceExceptionが出る)
28
+ webBrowser1.DocumentText = "";
29
+ }
30
+
31
+ // フォント変更
32
+ webBrowser1.DocumentCompleted += (s, e) => webBrowser1.Document.Body.Style = "font-family:メイリオ;";
33
+ }
34
+
35
+ private void Form1_FormClosing(object sender, FormClosingEventArgs e)
36
+ {
37
+ // 終了時に自動保存
38
+ // 次回起動時のためdivを表示に戻しておく
39
+ foreach(HtmlElement div in webBrowser1.Document.GetElementsByTagName("div"))
40
+ {
41
+ div.Style = "display:block";
42
+ }
43
+
44
+ // ファイルが書き込めないと例外
45
+ // webBrowser1.DocumentTextだと INPUT CHECKED が反映されないので注意
46
+ File.WriteAllText("Questions280412.html", webBrowser1.Document.Body.InnerHtml);
47
+ }
48
+
49
+ private void SelectImageButton_Click(object sender, EventArgs e)
50
+ {
51
+ using(var dlg = new OpenFileDialog())
52
+ {
53
+ dlg.Filter = "画像ファイル(*.png;*.jpg;*.gif)|*.png;*.jpg;*.gif|すべてのファイル(*.*)|*.*";
54
+ if(dlg.ShowDialog() == DialogResult.OK)
55
+ {
56
+ fileTextBox.Text = dlg.FileName;
57
+ }
58
+ }
59
+ }
60
+
61
+
62
+ // 1投稿分のテンプレート
63
+ private const string template = @"
64
+ <div>
65
+ <h1>{{title}}</h1>
66
+ <img src='{{file}}' />
67
+ <br />
68
+ <pre>{{comment}}</pre>
69
+ <label>
70
+ <input type='checkbox' name='like'>お気に入り
71
+ </label>
72
+ <hr />
73
+ </div>";
74
+
75
+ private void PostButton_Click(object sender, EventArgs e)
76
+ {
77
+ // HTMLエンコード(< を &lt; に変える処理)
78
+ var title = WebUtility.HtmlEncode(titleTextBox.Text);
79
+ var file = WebUtility.HtmlEncode(fileTextBox.Text);
80
+ var comment = WebUtility.HtmlEncode(commentTextBox.Text);
81
+
82
+ // テンプレートの指定部分と置換
83
+ var html = template.Replace("{{title}}", title)
84
+ .Replace("{{file}}", file)
85
+ .Replace("{{comment}}", comment);
86
+
87
+ //webBrowser1.DocumentText += html; // だとダメ
88
+ webBrowser1.Document.Body.InnerHtml += html; // 投稿追加
89
+ // お気に入り状態で追加されるといろいろ面倒なのでぜんぶに戻す
90
+ allRadioButton.Checked = true;
91
+ }
92
+
93
+ private void AllRadioButton_CheckedChanged(object sender, EventArgs e)
94
+ {
95
+ // divタグを全列挙
96
+ foreach(HtmlElement div in webBrowser1.Document.GetElementsByTagName("div"))
97
+ {
98
+ // divを表示に戻す
99
+ div.Style = "display:block";
100
+ }
101
+ }
102
+
103
+ private void LikeRadioButton_CheckedChanged(object sender, EventArgs e)
104
+ {
105
+ // 全体から name='like' のものを全列挙
106
+ foreach(HtmlElement checkbox in webBrowser1.Document.All.GetElementsByName("like"))
107
+ {
108
+ // チェックボックスがチェックされていなければ...
109
+ if(checkbox.GetAttribute("checked") == "False")
110
+ {
111
+ // div(チェックボックスの親の親)を非表示にする
112
+ checkbox.Parent.Parent.Style = "display:none";
113
+ }
114
+ }
115
+ }
116
+
117
+
118
+
119
+ // InitializeComponent()を圧縮
120
+ private void InitializeComponent2()
121
+ {
122
+ ((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
123
+ splitContainer1.Panel1.SuspendLayout();
124
+ splitContainer1.Panel2.SuspendLayout();
125
+ splitContainer1.SuspendLayout();
126
+ SuspendLayout();
127
+
128
+ // splitContainer1
129
+ splitContainer1.Dock = DockStyle.Fill;
130
+
131
+ // splitContainer1.Panel1
132
+ splitContainer1.Panel1.Controls.Add(label3);
133
+ splitContainer1.Panel1.Controls.Add(label2);
134
+ splitContainer1.Panel1.Controls.Add(label1);
135
+ splitContainer1.Panel1.Controls.Add(postButton);
136
+ splitContainer1.Panel1.Controls.Add(selectImageButton);
137
+ splitContainer1.Panel1.Controls.Add(commentTextBox);
138
+ splitContainer1.Panel1.Controls.Add(fileTextBox);
139
+ splitContainer1.Panel1.Controls.Add(titleTextBox);
140
+
141
+ // splitContainer1.Panel2
142
+ splitContainer1.Panel2.Controls.Add(likeRadioButton);
143
+ splitContainer1.Panel2.Controls.Add(allRadioButton);
144
+ splitContainer1.Panel2.Controls.Add(webBrowser1);
145
+ splitContainer1.Size = new Size(800, 450);
146
+ splitContainer1.SplitterDistance = 408;
147
+
148
+ // label3
149
+ label3.AutoSize = true;
150
+ label3.Location = new Point(14, 69);
151
+ label3.Text = "コメント";
152
+
153
+ // label2
154
+ label2.AutoSize = true;
155
+ label2.Location = new Point(12, 44);
156
+ label2.Text = "画像パス";
157
+
158
+ // label1
159
+ label1.AutoSize = true;
160
+ label1.Location = new Point(12, 15);
161
+ label1.Text = "タイトル";
162
+
163
+ // postButton
164
+ postButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
165
+ postButton.Location = new Point(321, 415);
166
+ postButton.Size = new Size(75, 23);
167
+ postButton.Text = "投稿";
168
+ postButton.UseVisualStyleBackColor = true;
169
+ postButton.Click += PostButton_Click;
170
+
171
+ // selectImageButton
172
+ selectImageButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
173
+ selectImageButton.Location = new Point(299, 37);
174
+ selectImageButton.Size = new Size(97, 23);
175
+ selectImageButton.Text = "画像を選択...";
176
+ selectImageButton.UseVisualStyleBackColor = true;
177
+ selectImageButton.Click += SelectImageButton_Click;
178
+
179
+ // commentTextBox
180
+ commentTextBox.AcceptsReturn = true;
181
+ commentTextBox.AcceptsTab = true;
182
+ commentTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
183
+ commentTextBox.Location = new Point(66, 66);
184
+ commentTextBox.Multiline = true;
185
+ commentTextBox.Size = new Size(330, 343);
186
+
187
+ // fileTextBox
188
+ fileTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
189
+ fileTextBox.Location = new Point(66, 41);
190
+ fileTextBox.Size = new Size(227, 19);
191
+
192
+ // titleTextBox
193
+ titleTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
194
+ titleTextBox.Location = new Point(66, 12);
195
+ titleTextBox.Size = new Size(330, 19);
196
+
197
+ // likeRadioButton
198
+ likeRadioButton.Appearance = Appearance.Button;
199
+ likeRadioButton.AutoSize = true;
200
+ likeRadioButton.Location = new Point(54, 5);
201
+ likeRadioButton.TabStop = true;
202
+ likeRadioButton.Text = "お気に入り";
203
+ likeRadioButton.UseVisualStyleBackColor = true;
204
+ likeRadioButton.CheckedChanged += LikeRadioButton_CheckedChanged;
205
+
206
+ // allRadioButton
207
+ allRadioButton.Appearance = Appearance.Button;
208
+ allRadioButton.AutoSize = true;
209
+ allRadioButton.Checked = true;
210
+ allRadioButton.Location = new Point(3, 5);
211
+ allRadioButton.TabStop = true;
212
+ allRadioButton.Text = "ぜんぶ";
213
+ allRadioButton.UseVisualStyleBackColor = true;
214
+ allRadioButton.CheckedChanged += AllRadioButton_CheckedChanged;
215
+
216
+ // webBrowser1
217
+ webBrowser1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
218
+ webBrowser1.Location = new Point(3, 33);
219
+ webBrowser1.MinimumSize = new Size(20, 20);
220
+ webBrowser1.Size = new Size(382, 414);
221
+
222
+ // Form1
223
+ AutoScaleDimensions = new SizeF(6F, 12F);
224
+ AutoScaleMode = AutoScaleMode.Font;
225
+ ClientSize = new Size(800, 450);
226
+ Controls.Add(splitContainer1);
227
+ Text = "Form1";
228
+ FormClosing += Form1_FormClosing;
229
+ splitContainer1.Panel1.ResumeLayout(false);
230
+ splitContainer1.Panel1.PerformLayout();
231
+ splitContainer1.Panel2.ResumeLayout(false);
232
+ splitContainer1.Panel2.PerformLayout();
233
+ ((System.ComponentModel.ISupportInitialize)(splitContainer1)).EndInit();
234
+ splitContainer1.ResumeLayout(false);
235
+ ResumeLayout(false);
236
+ }
237
+
238
+ private SplitContainer splitContainer1 = new SplitContainer();
239
+ private WebBrowser webBrowser1 = new WebBrowser();
240
+ private Label label3 = new Label();
241
+ private Label label2 = new Label();
242
+ private Label label1 = new Label();
243
+ private Button postButton = new Button();
244
+ private Button selectImageButton = new Button();
245
+ private TextBox commentTextBox = new TextBox();
246
+ private TextBox fileTextBox = new TextBox();
247
+ private TextBox titleTextBox = new TextBox();
248
+ private RadioButton likeRadioButton = new RadioButton();
249
+ private RadioButton allRadioButton = new RadioButton();
250
+ }
251
+ }
252
+ ```
253
+
254
+ ![アプリ画像](48751df8700e4a94434cec3a2206f504.png)
255
+ **注意**
256
+ Form1.Designer.csのままだと1万字に収まらないので、しかたなくInitializeComponent2を作成。
257
+ いろいろ気が利かない部分があります(改善の余地を残しています^^;
258
258
  大量にアイテムがあると、投稿後のお気に入りへの切り替えが激遅い。

2

からでいい

2020/07/27 11:09

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -25,7 +25,7 @@
25
25
  catch
26
26
  {
27
27
  // なければ空文字に(何かは入れないとNullReferenceExceptionが出る)
28
- webBrowser1.DocumentText = "<!DOCTYPE html>";
28
+ webBrowser1.DocumentText = "";
29
29
  }
30
30
 
31
31
  // フォント変更

1

画像忘れ

2020/07/27 11:09

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -251,6 +251,7 @@
251
251
  }
252
252
  ```
253
253
 
254
+ ![アプリ画像](48751df8700e4a94434cec3a2206f504.png)
254
255
  **注意**
255
256
  Form1.Designer.csのままだと1万字に収まらないので、しかたなくInitializeComponent2を作成。
256
257
  いろいろ気が利かない部分があります(改善の余地を残しています^^;