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

質問編集履歴

6

解決方法追記

2020/07/21 04:34

投稿

Mr.sijimi
Mr.sijimi

スコア21

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,7 @@
7
7
  ### 該当のソースコード
8
8
 
9
9
  ```C#
10
- namespace CalculatorNeo
10
+ namespace Calculator
11
11
  {
12
12
  public partial class MainWindow : Window
13
13
  {
@@ -176,4 +176,77 @@
176
176
  Windows10 Pro 64bit
177
177
  Visual Studio 2015
178
178
 
179
- 他必要な情報がありましたらご教示ください。
179
+ 他必要な情報がありましたらご教示ください。
180
+
181
+
182
+ ### 解決方法(済)
183
+
184
+ ```C#
185
+ namespace Calculator
186
+ {
187
+ public partial class Initialize
188
+ {
189
+ /// <summary>
190
+ /// イベント初期化
191
+ /// </summary>
192
+ public void InitializeEvent(MainWindow w)
193
+ {
194
+ //rootGridは以下のコントロールを探す
195
+ foreach (var ctrl in LogicalTreeHelper.GetChildren(w.rootGrid))
196
+ {
197
+ //ボタン以外は無視
198
+ if (!(ctrl is Button))
199
+ {
200
+ continue;
201
+ }
202
+
203
+ //ボタンがクリックされたときの処理登録
204
+ (ctrl as Button).Click += (sender, e) =>
205
+ {
206
+ string inKey = (sender as Button).Content.ToString();
207
+ switch (inKey)
208
+ {
209
+ //クリア
210
+ case "AC":
211
+ w.formula.Text = "0";
212
+ break;
213
+ case "=":
214
+ if (w.formula.Text == MessageList.CFNM0001)
215
+ {
216
+ w.formula.Text = "0";
217
+ }
218
+ else
219
+ {
220
+ //計算結果表示
221
+ var Calc = new CalculateProcess();
222
+ w.formula.Text = Calc.Calculator(w.formula.Text);
223
+ }
224
+ break;
225
+ default:
226
+ if (w.formula.Text.Length >= ItemProperty.FormulaMax)
227
+ {
228
+ break;
229
+ }
230
+ else if (w.formula.Text == "0" && (inKey != "/" && inKey != "*" && inKey != "-" && inKey != "+"))
231
+ {
232
+ w.formula.Text = inKey;
233
+ }
234
+ else if (w.formula.Text == MessageList.CFNM0001)
235
+ {
236
+ //0が表示されていて数字が入力されたらその値を設定
237
+ w.formula.Text = inKey;
238
+ }
239
+ else
240
+ {
241
+ //入力された値を式に追加
242
+ w.formula.Text += inKey;
243
+ }
244
+ break;
245
+ }
246
+ };
247
+ }
248
+ }
249
+ }
250
+ }
251
+
252
+ ```

5

文脈の修正

2020/07/21 04:34

投稿

Mr.sijimi
Mr.sijimi

スコア21

title CHANGED
File without changes
body CHANGED
@@ -1,8 +1,9 @@
1
1
  ### 前提・実現したいこと
2
2
  C#でクリック操作でできる電卓を作成してますが
3
- Mainwindow処理のインスタンスしている箇所
3
+ Mainwindow処理の「var IniEvent = new Initialize()」
4
4
  無限ループになってしまい処理が進みません。
5
5
 
6
+
6
7
  ### 該当のソースコード
7
8
 
8
9
  ```C#

4

コードの修正と追記

2020/07/21 02:58

投稿

Mr.sijimi
Mr.sijimi

スコア21

title CHANGED
File without changes
body CHANGED
@@ -6,76 +6,161 @@
6
6
  ### 該当のソースコード
7
7
 
8
8
  ```C#
9
- public MainWindow()
9
+ namespace CalculatorNeo
10
10
  {
11
+ public partial class MainWindow : Window
12
+ {
13
+ /// <summary>
14
+ /// コンストラクタ
15
+ /// </summary>
16
+ public MainWindow()
17
+ {
11
- InitializeComponent();
18
+ InitializeComponent();
12
19
 
13
- //イベント初期化
20
+ //イベント初期化
14
- var IniEvent = new Initialize();
21
+ var IniEvent = new Initialize();
15
- IniEvent.InitializeEvent();
22
+ IniEvent.InitializeEvent();
23
+ }
24
+ }
16
25
  }
17
26
  ```
18
27
 
19
28
  ```C#
20
- //初期化イベント
21
- public void InitializeEvent()
29
+ namespace Calculator
22
30
  {
23
- //rootGridは以下のコントロールを探す
24
- foreach (var ctrl in LogicalTreeHelper.GetChildren(rootGrid))
31
+ public partial class Initialize : MainWindow
25
32
  {
33
+ /// <summary>
26
- //ボタ以外は無視
34
+ /// イベト初期化
27
- if (!(ctrl is Button))
28
- {
29
- continue;
30
- }
31
- //ボタンがクリックされたときの処理登録
35
+ /// </summary>
32
- (ctrl as Button).Click += (sender, e) =>
36
+ public void InitializeEvent()
33
- {
34
- string inKey = (sender as Button).Content.ToString();
35
- switch (inKey)
36
37
  {
38
+ //rootGridは以下のコントロールを探す
39
+ foreach (var ctrl in LogicalTreeHelper.GetChildren(rootGrid))
40
+ {
37
- //クリア
41
+ //ボタン以外は無視
38
- case "AC":
42
+ if (!(ctrl is Button))
39
- formula.Text = "0";
40
- break;
41
- case "=":
42
- if (formula.Text == MessageList.CFNM0001)
43
43
  {
44
- formula.Text = "0";
44
+ continue;
45
45
  }
46
+
47
+ //ボタンがクリックされたときの処理登録
46
- else
48
+ (ctrl as Button).Click += (sender, e) =>
47
49
  {
50
+ string inKey = (sender as Button).Content.ToString();
51
+ switch (inKey)
52
+ {
53
+ //クリア
54
+ case "AC":
55
+ formula.Text = "0";
56
+ break;
57
+ case "=":
58
+ if (formula.Text == MessageList.CFNM0001)
59
+ {
60
+ formula.Text = "0";
61
+ }
62
+ else
63
+ {
48
- //計算結果表示
64
+ //計算結果表示
49
- var Calc = new CalculateProcess();
65
+ var Calc = new CalculateProcess();
50
- formula.Text = Calc.Calculator(formula.Text);
66
+ formula.Text = Calc.Calculator(formula.Text);
51
- }
67
+ }
52
- break;
68
+ break;
53
- default:
69
+ default:
54
- if (formula.Text.Length >= ItemProperty.FormulaMax)
70
+ if (formula.Text.Length >= ItemProperty.FormulaMax)
71
+ {
72
+ break;
73
+ }
74
+ else if (formula.Text == "0" && (inKey != "/" && inKey != "*" && inKey != "-" && inKey != "+"))
75
+ {
76
+ formula.Text = inKey;
77
+ }
78
+ else if (formula.Text == MessageList.CFNM0001)
79
+ {
80
+ //0が表示されていて数字が入力されたらその値を設定
81
+ formula.Text = inKey;
82
+ }
83
+ else
84
+ {
85
+ //入力された値を式に追加
86
+ formula.Text += inKey;
87
+ }
88
+ break;
89
+ }
90
+ };
91
+ }
92
+ }
93
+ }
94
+ }
95
+ ```
96
+
97
+ ### 追記
98
+ ```C#
99
+ namespace Calculator
100
+ {
101
+ public partial class CalculateProcess : MainWindow
102
+ {
103
+ /// <summary>
104
+ /// 計算実行
105
+ /// </summary>
106
+ /// <param name="formula">計算式</param>
107
+ /// <returns>計算結果</returns>
108
+ public string Calculator(string formula)
109
+ {
110
+ try
111
+ {
112
+ var p = new Process();
113
+
114
+ // システムフォルダの取得
115
+ string systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);
116
+ // PowerShellのフルパスを組立
117
+ string powerShellFullPath = System.IO.Path.Combine(systemFolder, @"WindowsPowerShell\v1.0\powershell.exe");
118
+
119
+ // 実行ファイル
120
+ p.StartInfo.FileName = powerShellFullPath;
121
+ // パラメータ(計算式)
122
+ p.StartInfo.Arguments = "-Command " + formula;
123
+
124
+ // 標準入力リダイレクトしない
125
+ p.StartInfo.RedirectStandardInput = false;
126
+ // 標準入力リダイレクトする
127
+ p.StartInfo.RedirectStandardOutput = true;
128
+
129
+
130
+ // シェル機能使用しない
131
+ p.StartInfo.UseShellExecute = false;
132
+ // ウィンドウ開かない
133
+ p.StartInfo.CreateNoWindow = true;
134
+
135
+ // 実行
136
+ p.Start();
137
+ // 結果を一行読む
138
+ string calcResult = p.StandardOutput.ReadLine();
139
+ p.WaitForExit();
140
+ p.Close();
141
+
142
+ if (string.IsNullOrEmpty(calcResult))
55
143
  {
144
+ // 計算結果無し(計算失敗)
56
- break;
145
+ return MessageList.CFNM0001;
57
146
  }
58
- else if (formula.Text == "0" && (inKey != "/" && inKey != "*" && inKey != "-" && inKey != "+"))
59
- {
60
- formula.Text = inKey;
61
- }
62
- else if (formula.Text == MessageList.CFNM0001)
63
- {
64
- //0が表示されていて数字が入力されたらその値を設定
65
- formula.Text = inKey;
66
- }
67
147
  else
68
148
  {
69
- //入力された値式に追加
149
+ // 計算結果返す
70
- formula.Text += inKey;
150
+ return calcResult;
71
151
  }
72
- break;
73
152
  }
153
+ catch
154
+ {
155
+ return MessageList.CFNM0001;
74
- };
156
+ }
157
+ }
158
+
75
159
  }
76
160
  }
77
161
  ```
78
162
 
163
+
79
164
  ### 試したこと
80
165
  「インスタンス化 無限ループ」や、「Main 無限ループ」などで調べてみましたが出てこず、原因がわかりません。
81
166
 

3

文面の修正

2020/07/21 02:54

投稿

Mr.sijimi
Mr.sijimi

スコア21

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,7 @@
1
1
  ### 前提・実現したいこと
2
2
  C#でクリック操作でできる電卓を作成してますが
3
+ Mainwindow処理のインスタンスしている箇所で
3
- Main処理でインスタンス化すると無限ループになってしまい処理が進みません。
4
+ 無限ループになってしまい処理が進みません。
4
5
 
5
6
  ### 該当のソースコード
6
7
 

2

文面の修正

2020/07/21 02:42

投稿

Mr.sijimi
Mr.sijimi

スコア21

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
- C#でクリック操作でできる電卓を作成してます
2
+ C#でクリック操作でできる電卓を作成してます
3
- しかし、Main処理でインスタンス化すると無限ループになってしまい処理が進みません。
3
+ Main処理でインスタンス化すると無限ループになってしまい処理が進みません。
4
4
 
5
5
  ### 該当のソースコード
6
6
 

1

Main処理内のコード内容の修正

2020/07/21 02:38

投稿

Mr.sijimi
Mr.sijimi

スコア21

title CHANGED
File without changes
body CHANGED
@@ -7,6 +7,8 @@
7
7
  ```C#
8
8
  public MainWindow()
9
9
  {
10
+ InitializeComponent();
11
+
10
12
  //イベント初期化
11
13
  var IniEvent = new Initialize();
12
14
  IniEvent.InitializeEvent();