回答編集履歴
4
コード改善
test
CHANGED
@@ -50,6 +50,10 @@
|
|
50
50
|
|
51
51
|
{
|
52
52
|
|
53
|
+
|
54
|
+
|
55
|
+
// initialize
|
56
|
+
|
53
57
|
public Form1()
|
54
58
|
|
55
59
|
{
|
@@ -60,31 +64,31 @@
|
|
60
64
|
|
61
65
|
h => (sender, e) => h(e), h => numericUpDown1.ValueChanged += h, h => numericUpDown1.ValueChanged -= h)
|
62
66
|
|
63
|
-
.Throttle(TimeSpan.FromMilliseconds(500)) // 動作がわかりやすいようにわざと大きめの値を入れている
|
67
|
+
.Throttle(TimeSpan.FromMilliseconds(500)) // 動作がわかりやすいように、わざと大きめの値を入れている
|
68
|
+
|
69
|
+
.ObserveOn(System.Threading.SynchronizationContext.Current) // UIスレッドで監視
|
64
70
|
|
65
71
|
.Subscribe(_ => numericUpDown1_Changed());
|
66
72
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
73
|
}
|
74
74
|
|
75
|
+
|
75
76
|
|
76
|
-
|
77
|
-
private void numericUpDown1_Changed()
|
77
|
+
private void numericUpDown1_Changed() // イベントから外しておく
|
78
78
|
|
79
79
|
{
|
80
80
|
|
81
81
|
int length = Convert.ToInt32(numericUpDown1.Value);
|
82
82
|
|
83
|
+
|
84
|
+
|
83
85
|
pictureBox1.Image = MakeBmp(length);
|
86
|
+
|
87
|
+
label1.Text = length.ToString(); // 確認用に追加
|
84
88
|
|
85
89
|
}
|
86
90
|
|
87
|
-
|
91
|
+
|
88
92
|
|
89
93
|
private Bitmap MakeBmp(int length)
|
90
94
|
|
3
ミス修正
test
CHANGED
@@ -54,6 +54,8 @@
|
|
54
54
|
|
55
55
|
{
|
56
56
|
|
57
|
+
InitializeComponent();
|
58
|
+
|
57
59
|
Observable.FromEvent<EventHandler, EventArgs>(
|
58
60
|
|
59
61
|
h => (sender, e) => h(e), h => numericUpDown1.ValueChanged += h, h => numericUpDown1.ValueChanged -= h)
|
@@ -66,7 +68,7 @@
|
|
66
68
|
|
67
69
|
|
68
70
|
|
69
|
-
|
71
|
+
|
70
72
|
|
71
73
|
}
|
72
74
|
|
2
Rx追記
test
CHANGED
@@ -19,3 +19,93 @@
|
|
19
19
|
|
20
20
|
|
21
21
|
面倒なのでRxのThrottle使ったほうが簡潔ですが。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
---
|
26
|
+
|
27
|
+
(NuGetからSystem.Reactiveを追加して)
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
```C#
|
34
|
+
|
35
|
+
using System;
|
36
|
+
|
37
|
+
using System.Drawing;
|
38
|
+
|
39
|
+
using System.Windows.Forms;
|
40
|
+
|
41
|
+
using System.Reactive.Linq;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
namespace WindowsFormsApplication1
|
46
|
+
|
47
|
+
{
|
48
|
+
|
49
|
+
public partial class Form1 : Form
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
public Form1()
|
54
|
+
|
55
|
+
{
|
56
|
+
|
57
|
+
Observable.FromEvent<EventHandler, EventArgs>(
|
58
|
+
|
59
|
+
h => (sender, e) => h(e), h => numericUpDown1.ValueChanged += h, h => numericUpDown1.ValueChanged -= h)
|
60
|
+
|
61
|
+
.Throttle(TimeSpan.FromMilliseconds(500)) // 動作がわかりやすいようにわざと大きめの値を入れている
|
62
|
+
|
63
|
+
.Subscribe(_ => numericUpDown1_Changed());
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
InitializeComponent();
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
private void numericUpDown1_Changed() // これはnumericupDown1のイベントから外しておく
|
76
|
+
|
77
|
+
{
|
78
|
+
|
79
|
+
int length = Convert.ToInt32(numericUpDown1.Value);
|
80
|
+
|
81
|
+
pictureBox1.Image = MakeBmp(length);
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
private Bitmap MakeBmp(int length)
|
88
|
+
|
89
|
+
{
|
90
|
+
|
91
|
+
Bitmap bmp = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
|
92
|
+
|
93
|
+
using (Graphics g_bmp = Graphics.FromImage(bmp))
|
94
|
+
|
95
|
+
{
|
96
|
+
|
97
|
+
g_bmp.FillRectangle(Brushes.Red, new Rectangle(0, 0, length, length));
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
return bmp;
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
```
|
110
|
+
|
111
|
+
|
1
追記
test
CHANGED
@@ -11,3 +11,11 @@
|
|
11
11
|
|
12
12
|
|
13
13
|
時刻はDateTime.Nowでとれます。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
Timerで監視すればいいでしょう。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
面倒なのでRxのThrottle使ったほうが簡潔ですが。
|