回答編集履歴

2

反応が悪いのでvar禁止・説明を増やす

2023/04/01 20:38

投稿

TN8001
TN8001

スコア9321

test CHANGED
@@ -8,82 +8,173 @@
8
8
 
9
9
  ---
10
10
 
11
- とりあえずそうだ(1日だけ版)として、提示通りの条件で書きました(`GraphTextBox`・`TotalTextBox`のクリアもないと変ですが置いておきます)
11
+ とりあえずそうだ(1日だけ版)として、提示通りの条件で書きました
12
12
  ```cs
13
13
  using System.Data;
14
+ using System.Text;
14
15
 
15
- namespace Q1fyr16mcto59tg;
16
+ namespace Q1fyr16mcto59tg
17
+ {
18
+ public partial class Form1 : Form
19
+ {
20
+ // ItemListDataGridViewとバインドしたDataTable
21
+ // 最初にDataSourceにセットしたら、以降はAddやClearするだけで作り直したり入れ替えたりはしない!!
22
+ private readonly DataTable dt;
16
23
 
17
- public partial class Form1 : Form
24
+ public Form1()
18
- {
25
+ {
19
- private readonly DataTable dt;
26
+ InitializeComponent();
20
27
 
21
- public Form1()
28
+ // DataTable作成
22
- {
23
- InitializeComponent();
29
+ dt = new DataTable("ItemList");
30
+ dt.Columns.Add("日付", typeof(string));
31
+ dt.Columns.Add("商品", typeof(string));
32
+ dt.Columns.Add("個数", typeof(int));
33
+ dt.Columns.Add("値段", typeof(int));
24
34
 
25
- dt = new DataTable("ItemList");
35
+ ItemListDataGridView.DataSource = dt;
26
- dt.Columns.Add("日付", typeof(string));
27
- dt.Columns.Add("商品", typeof(string));
28
- dt.Columns.Add("個数", typeof(int));
29
- dt.Columns.Add("値段", typeof(int));
30
36
 
31
- ItemListDataGridView.DataSource = dt;
37
+ Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // Shift_JIS有効化
32
- }
38
+ }
33
39
 
34
- private void CalculateButton_Click(object sender, EventArgs e)
40
+ private void CalculateButton_Click(object sender, EventArgs e)
35
- {
36
- if (string.IsNullOrWhiteSpace(ItemNameComboBox.Text))
37
41
  {
42
+ // 品物が選択されているか確認
43
+ if (string.IsNullOrWhiteSpace(ItemNameComboBox.Text))
44
+ {
38
- MessageBox.Show("品物を選択してください");
45
+ MessageBox.Show("品物を選択してください");
39
- return;
46
+ return;
40
- }
47
+ }
48
+
49
+ // 個数に1以上の数値が入力されているか確認
41
- var count = (int)CountNumericUpDown.Value;
50
+ int count = (int)CountNumericUpDown.Value;
42
- if (count <= 0)
51
+ if (count <= 0)
43
- {
52
+ {
44
- MessageBox.Show("1つ以上の数字を入力してください");
53
+ MessageBox.Show("1つ以上の数字を入力してください");
45
- return;
54
+ return;
46
- }
55
+ }
56
+
57
+ // 単価が入力されているか確認
47
- var ListPrice = (int)ListPriceNumericUpDown.Value;
58
+ int ListPrice = (int)ListPriceNumericUpDown.Value;
48
- if (ListPrice <= 0)
59
+ if (ListPrice <= 0)
49
- {
60
+ {
50
- MessageBox.Show("単価を入力してください");
61
+ MessageBox.Show("単価を入力してください");
51
- return;
62
+ return;
63
+ }
64
+
65
+
66
+ // 今回の金額
67
+ int price = ListPrice * count;
68
+
69
+ // 買い物リスト用の文字列を作成
70
+ string date = dateTimePicker.Value.ToLongDateString();
71
+ string item = ItemNameComboBox.Text;
72
+
73
+
74
+ // 「DataTable.Row[0][0]が空白であれば、4列入力」
75
+ // 1行も追加されていないと dt.Rows[0][0] はエラーになるので行数が0かどうかで判断
76
+ if (0 == dt.Rows.Count)
77
+ {
78
+ // ここでやってもいいけど同じことを何度も書くのは無駄なのでif後にやる
79
+ //DataRow row = dt.NewRow();
80
+ //row["日付"] = date;
81
+ //row["商品"] = item;
82
+ //row["個数"] = count;
83
+ //row["値段"] = price;
84
+ //dt.Rows.Add(row);
85
+ }
86
+
87
+ // 「入れようとしている文字列と同じであれば[1][1]から3列いれる」
88
+ // else if (dt.Rows[0][0].ToString() == date) と書いてもいいが
89
+ else if (dt.Rows[0].Field<string>("日付") == date)
90
+ {
91
+ date = " ";
92
+ // ここでやってもいいけど...略
93
+ //DataRow row = dt.NewRow();
94
+ //row["日付"] = " ";
95
+ //row["商品"] = item;
96
+ //row["個数"] = count;
97
+ //row["値段"] = price;
98
+ //dt.Rows.Add(row);
99
+ }
100
+
101
+ // 「入れようと文字列と違うのであれば、DataTableをクリア」
102
+ else
103
+ {
104
+ dt.Clear();
105
+ // ここでやってもいいけど...略
106
+ //DataRow row = dt.NewRow();
107
+ //row["日付"] = date;
108
+ //row["商品"] = item;
109
+ //row["個数"] = count;
110
+ //row["値段"] = price;
111
+ //dt.Rows.Add(row);
112
+
113
+ GraphTextBox.Text = "";
114
+ TotalTextBox.Text = "";
115
+ }
116
+
117
+ // 買い物リストに追加
118
+ DataRow row = dt.NewRow();
119
+ row["日付"] = date;
120
+ row["商品"] = item;
121
+ row["個数"] = count;
122
+ row["値段"] = price;
123
+ dt.Rows.Add(row);
124
+
125
+
126
+ // グラフ用の文字列を作成
127
+ //string graphLine = "";
128
+ //for (int i = 0; i < count; i++) graphLine += "■";
129
+ string graphLine = new string('■', count);
130
+
131
+
132
+ // グラフを表示
133
+ //if (string.IsNullOrWhiteSpace(GraphTextBox.Text))
134
+ // GraphTextBox.Text = graphLine;
135
+ //else
136
+ // GraphTextBox.Text += Environment.NewLine + graphLine;
137
+ // 厳密には同じではないが改行を後ろにすれば分岐が不要
138
+ GraphTextBox.Text += graphLine + Environment.NewLine;
139
+
140
+
141
+ // 合計金額を表示
142
+ //if (int.TryParse(TotalTextBox.Text, out int total))
143
+ //{
144
+ // int sum = total + price;
145
+ // TotalTextBox.Text = sum.ToString();
146
+ //}
147
+ //else
148
+ // TotalTextBox.Text = price.ToString();
149
+ // totalは初期値0なので分岐は不要
150
+ // _ = はなくてもいいが🛈️(CA1806)がでるので破棄(使わないこと)を明示
151
+ _ = int.TryParse(TotalTextBox.Text, out int total);
152
+ TotalTextBox.Text = $"{total + price}";
52
153
  }
53
154
 
54
155
 
55
- var price = ListPrice * count;
56
- var date = dateTimePicker.Value.ToLongDateString();
156
+ private void SaveButton_Click(object sender, EventArgs e)
157
+ {
158
+ SaveFileDialog dlg = new SaveFileDialog
159
+ {
57
- var item = ItemNameComboBox.Text;
160
+ Title = "ファイルを保存する",
161
+ //InitialDirectory = @"C:\",
162
+ FileName = "ItemList.csv",
163
+ Filter = "CSV(カンマ区切り)|*.csv",
164
+ };
58
165
 
59
- // DataTable.Row[0][0]が空白であれば、4列入力
60
- if (0 == dt.Rows.Count)
166
+ if (dlg.ShowDialog() == DialogResult.Cancel)
61
- {
167
+ {
168
+ MessageBox.Show("キャンセルされました");
169
+ return;
170
+ }
171
+
172
+ using StreamWriter writer = new StreamWriter(dlg.FileName, false, Encoding.GetEncoding("shift_jis"));
173
+ foreach (DataRow row in dt.Rows)
174
+ {
175
+ writer.WriteLine(string.Join(",", row.ItemArray));
176
+ }
62
177
  }
63
- // 入れようとしている文字列と同じであれば[1][1]から3列いれる
64
- else if (dt.Rows[0].Field<string>("日付") == date)
65
- {
66
- date = " ";
67
- }
68
- // 入れようと文字列と違うのであれば、DataTableをクリア
69
- else
70
- {
71
- dt.Clear();
72
- }
73
-
74
- var row = dt.NewRow();
75
- row["日付"] = date;
76
- row["商品"] = item;
77
- row["個数"] = count;
78
- row["値段"] = price;
79
- dt.Rows.Add(row);
80
-
81
-
82
- GraphTextBox.Text += new string('■', count) + Environment.NewLine;
83
-
84
-
85
- _ = int.TryParse(TotalTextBox.Text, out var total);
86
- TotalTextBox.Text = $"{total + price}";
87
178
  }
88
179
  }
89
180
  ```

1

補間

2023/03/31 15:27

投稿

TN8001
TN8001

スコア9321

test CHANGED
@@ -83,8 +83,7 @@
83
83
 
84
84
 
85
85
  _ = int.TryParse(TotalTextBox.Text, out var total);
86
- var sum = total + price;
87
- TotalTextBox.Text = sum.ToString();
86
+ TotalTextBox.Text = $"{total + price}";
88
87
  }
89
88
  }
90
89
  ```