回答編集履歴

2

修正

2017/12/26 08:22

投稿

Zuishin
Zuishin

スコア28660

test CHANGED
@@ -624,4 +624,6 @@
624
624
 
625
625
  他にもまずいところは多くありますが、直していません。
626
626
 
627
+
628
+
627
629
  一応説明通りに動くはずです。

1

追記

2017/12/26 08:21

投稿

Zuishin
Zuishin

スコア28660

test CHANGED
@@ -1 +1,627 @@
1
1
  バインドする方とされる方を逆にしてください。
2
+
3
+
4
+
5
+ コメントを受けて追記
6
+
7
+
8
+
9
+ Form1.cs
10
+
11
+ ```C#
12
+
13
+ using System.Collections.Generic;
14
+
15
+ using System.Drawing;
16
+
17
+ using System.Windows.Forms;
18
+
19
+
20
+
21
+
22
+
23
+ public delegate void tb_Click(object sender, KeyPressEventArgs e);
24
+
25
+
26
+
27
+ namespace WindowsFormsApp1
28
+
29
+ {
30
+
31
+ public partial class Form1 : Form
32
+
33
+ {
34
+
35
+ public Form1()
36
+
37
+ {
38
+
39
+ InitializeComponent();
40
+
41
+ }
42
+
43
+
44
+
45
+ public static int k = 0;
46
+
47
+ public Directory temp = new Directory("tb", 0);
48
+
49
+ public Form t = new Form();
50
+
51
+
52
+
53
+ // tempを格納しておく動的リスト
54
+
55
+ public static List<Directory> tempa = new List<Directory>();
56
+
57
+
58
+
59
+ public Directory supar = new Directory("supar", k);
60
+
61
+
62
+
63
+ public List<Point> spoint = new List<Point>();
64
+
65
+ public List<Point> epoint = new List<Point>();
66
+
67
+
68
+
69
+ private void button1_KeyPress(object sender, KeyPressEventArgs e)
70
+
71
+ {
72
+
73
+
74
+
75
+ if (e.KeyChar == (char)Keys.T)//Tキーが押されたとき
76
+
77
+ {
78
+
79
+ Control c = this.ActiveControl;
80
+
81
+ Directory tb = new Directory("tb", k);
82
+
83
+ tb.Add(tb, supar, c);
84
+
85
+ tb.Multiline = true;
86
+
87
+ tb.Top = 100;
88
+
89
+ tb.Left = 100;
90
+
91
+ tb.Height = 100;
92
+
93
+ tb.Width = 100;
94
+
95
+ tb.Text = "hello";
96
+
97
+ this.Controls.Add(tb);
98
+
99
+ tb.KeyDown += new KeyEventHandler(tb_KeyDown);
100
+
101
+ tb.Focus();
102
+
103
+ tb.BringToFront();
104
+
105
+ temp = tb;
106
+
107
+ supar = tb;
108
+
109
+ this.Controls.Add(temp);
110
+
111
+ tempa.Add(temp);
112
+
113
+ e.Handled = true;
114
+
115
+ k++;
116
+
117
+ }
118
+
119
+ }
120
+
121
+ private void tb_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
122
+
123
+ {
124
+
125
+ Directory temp = new Directory("temp", k);
126
+
127
+
128
+
129
+ Control d = this.ActiveControl;
130
+
131
+ t.Width = d.Left;
132
+
133
+ t.Height = d.Top;
134
+
135
+ if (t.Width >= this.Width)
136
+
137
+ {
138
+
139
+ this.Width += 100;
140
+
141
+ }
142
+
143
+ if (t.Height >= this.Height)
144
+
145
+ {
146
+
147
+ this.Height += 100;
148
+
149
+ }
150
+
151
+
152
+
153
+ if (e.KeyCode == Keys.Enter)
154
+
155
+ {
156
+
157
+ if (e.Shift == true)
158
+
159
+ {
160
+
161
+ this.SelectNextControl(this.ActiveControl, false, true, true, true);
162
+
163
+ e.Handled = true;
164
+
165
+ Control c = this.ActiveControl;
166
+
167
+ c.BringToFront();
168
+
169
+ }
170
+
171
+ else //メインの動作
172
+
173
+ {
174
+
175
+ int th = 0;
176
+
177
+ int tw = 0;
178
+
179
+ Control c = this.ActiveControl;
180
+
181
+ th = c.Top + c.Height;
182
+
183
+ tw = c.Left + c.Width;
184
+
185
+ Directory tb = new Directory("tb", k);
186
+
187
+ tb.Add(tb, (TextBox)sender, c);
188
+
189
+ tb.Multiline = true;
190
+
191
+ tb.Top = th;
192
+
193
+ tb.Left = tw;
194
+
195
+ tb.Height = tb.HeightS(k);
196
+
197
+ tb.Width = 50;
198
+
199
+ tb.Text = "hello";
200
+
201
+ this.Controls.Add(tb);
202
+
203
+ tb.KeyDown += new KeyEventHandler(tb_KeyDown);
204
+
205
+ tb.Focus();
206
+
207
+ tb.BringToFront();
208
+
209
+ temp = tb;
210
+
211
+ supar = tb;
212
+
213
+ this.Controls.Add(temp);
214
+
215
+ tempa.Add(temp);
216
+
217
+ e.Handled = true;
218
+
219
+ k++;
220
+
221
+ }
222
+
223
+ if (e.Alt)
224
+
225
+ {
226
+
227
+ e.Handled = true;
228
+
229
+ }
230
+
231
+ }
232
+
233
+ if (e.Alt)
234
+
235
+ {
236
+
237
+ if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
238
+
239
+ {
240
+
241
+ Directory.LinkGraph();
242
+
243
+ }
244
+
245
+ //移動
246
+
247
+ if (e.KeyCode == Keys.Up)
248
+
249
+ {
250
+
251
+ Control c = this.ActiveControl;
252
+
253
+ c.Top -= 5;
254
+
255
+ e.Handled = true;
256
+
257
+ }
258
+
259
+ if (e.KeyCode == Keys.Down)
260
+
261
+ {
262
+
263
+ Control c = this.ActiveControl;
264
+
265
+ c.Top += 5;
266
+
267
+ e.Handled = true;
268
+
269
+ }
270
+
271
+ if (e.KeyCode == Keys.Left)
272
+
273
+ {
274
+
275
+ Control c = this.ActiveControl;
276
+
277
+ c.Left -= 5;
278
+
279
+ e.Handled = true;
280
+
281
+ }
282
+
283
+ if (e.KeyCode == Keys.Right)
284
+
285
+ {
286
+
287
+ Control c = this.ActiveControl;
288
+
289
+ c.Left += 5;
290
+
291
+ e.Handled = true;
292
+
293
+ }
294
+
295
+
296
+
297
+ if (e.Control)//移動
298
+
299
+ {
300
+
301
+
302
+
303
+ if (e.KeyCode == Keys.Up)
304
+
305
+ {
306
+
307
+ Control c = this.ActiveControl;
308
+
309
+ c.Top -= 50;
310
+
311
+ e.Handled = true;
312
+
313
+ }
314
+
315
+ if (e.KeyCode == Keys.Down)
316
+
317
+ {
318
+
319
+ Control c = this.ActiveControl;
320
+
321
+ c.Top += 50;
322
+
323
+ e.Handled = true;
324
+
325
+ }
326
+
327
+ if (e.KeyCode == Keys.Left)
328
+
329
+ {
330
+
331
+ Control c = this.ActiveControl;
332
+
333
+ c.Left -= 50;
334
+
335
+ e.Handled = true;
336
+
337
+ }
338
+
339
+ if (e.KeyCode == Keys.Right)
340
+
341
+ {
342
+
343
+ Control c = this.ActiveControl;
344
+
345
+ c.Left += 50;
346
+
347
+ e.Handled = true;
348
+
349
+ }
350
+
351
+ }
352
+
353
+ }
354
+
355
+ if (e.Control)
356
+
357
+ {
358
+
359
+ foreach (Control l in Controls)
360
+
361
+ {
362
+
363
+ l.Top += 100;
364
+
365
+ l.Left += 100;
366
+
367
+ }
368
+
369
+ }
370
+
371
+ }
372
+
373
+
374
+
375
+ private void button1_Click(object sender, System.EventArgs e)
376
+
377
+ {
378
+
379
+ }
380
+
381
+ }
382
+
383
+ }
384
+
385
+ ```
386
+
387
+
388
+
389
+ Directory.cs
390
+
391
+ ```C#
392
+
393
+ using System.Collections;
394
+
395
+ using System.Drawing;
396
+
397
+ using System.Windows.Forms;
398
+
399
+
400
+
401
+ namespace WindowsFormsApp1
402
+
403
+ {
404
+
405
+
406
+
407
+ public class Directory : Entry
408
+
409
+ {
410
+
411
+ public string name; // ディレクトリの名前
412
+
413
+ public int num; //ディレクトリの識別番号
414
+
415
+ private static ArrayList directory = new ArrayList(); // ディレクトリエントリの集合
416
+
417
+ public new ArrayList supnode = new ArrayList();//子ノードの集合
418
+
419
+ public static ArrayList _su = new ArrayList();
420
+
421
+ public static ArrayList cop = new ArrayList();
422
+
423
+ public static ArrayList con = new ArrayList();
424
+
425
+
426
+
427
+ Directory cont;
428
+
429
+
430
+
431
+ string supename = "";
432
+
433
+
434
+
435
+ // 名前
436
+
437
+ override public string Name
438
+
439
+ {
440
+
441
+ set { this.name = value; }
442
+
443
+ get { return name; }
444
+
445
+ }
446
+
447
+
448
+
449
+ // サイズ
450
+
451
+ override public int Size
452
+
453
+ {
454
+
455
+ get
456
+
457
+ {
458
+
459
+ int size = 0;
460
+
461
+ foreach (Entry entry in directory)
462
+
463
+ {
464
+
465
+ size += entry.Size;
466
+
467
+ }
468
+
469
+ return size;
470
+
471
+ }
472
+
473
+ }
474
+
475
+
476
+
477
+ // コンストラクタ
478
+
479
+ public Directory(string name, int k) : base(name, k)
480
+
481
+ {
482
+
483
+ string s = k.ToString("D");
484
+
485
+ name = name + s;
486
+
487
+ this.name = name;
488
+
489
+ this.num = k;
490
+
491
+ }
492
+
493
+
494
+
495
+ // 追加
496
+
497
+ public Directory Add(Directory sub, TextBox supar, Control c)
498
+
499
+ {
500
+
501
+ int i = 0;
502
+
503
+ Directory su = new Directory("su", Form1.k);
504
+
505
+ foreach (Directory supe in directory)
506
+
507
+ {
508
+
509
+ if (supe.Name == ((WindowsFormsApp1.Directory)c).Name)
510
+
511
+ {
512
+
513
+ break;
514
+
515
+ }
516
+
517
+ i++;
518
+
519
+ }
520
+
521
+ directory.Add(sub);
522
+
523
+ cop.Add(sub);
524
+
525
+ cont = (Directory)cop[i];
526
+
527
+ sub.supnode.Add(supar);
528
+
529
+ con.Add(cont);
530
+
531
+ return this;
532
+
533
+ }
534
+
535
+
536
+
537
+ public int HeightS(int k)
538
+
539
+ {
540
+
541
+ string nam;
542
+
543
+ int h = 0;
544
+
545
+ string s = k.ToString("D");
546
+
547
+ nam = "tb" + s;
548
+
549
+ foreach (Directory tbs in directory)
550
+
551
+ {
552
+
553
+
554
+
555
+ if (tbs.name == nam)
556
+
557
+ {
558
+
559
+ h += 50;
560
+
561
+ }
562
+
563
+ }
564
+
565
+ return h;
566
+
567
+ }
568
+
569
+
570
+
571
+ public static void LinkGraph()
572
+
573
+ {
574
+
575
+ Graphics f = Form1.ActiveForm.CreateGraphics();
576
+
577
+ Graphics g = Form1.ActiveForm.CreateGraphics();
578
+
579
+
580
+
581
+ //背景色を更新
582
+
583
+ f.Clear(color: SystemColors.Control);
584
+
585
+
586
+
587
+ //pen作成
588
+
589
+ Pen redPen = new Pen(Color.Red, 10);
590
+
591
+ foreach (Directory sub in directory)
592
+
593
+ {
594
+
595
+ foreach (TextBox sup in sub.supnode)
596
+
597
+ {
598
+
599
+ //始点と終点
600
+
601
+ Point Start_point = new Point(sub.Left + sub.Width / 2, sub.Top + sub.Height / 2);
602
+
603
+ Point End_point = new Point(sup.Left + sup.Width / 2, sup.Top + sup.Height / 2);
604
+
605
+ //描画
606
+
607
+ g.DrawLine(redPen, Start_point, End_point);
608
+
609
+ }
610
+
611
+ }
612
+
613
+ redPen.Dispose();
614
+
615
+ }
616
+
617
+ }
618
+
619
+ }
620
+
621
+ ```
622
+
623
+
624
+
625
+ 他にもまずいところは多くありますが、直していません。
626
+
627
+ 一応説明通りに動くはずです。