【質問】
・チェックされたcheckListBoxの値のときに、変数に格納したい。(条件分岐の条件がうまくいかない)
・値を変数に格納したまま次のフォームで使用したい(まだ未実装)
【開発環境】
C# visual studio2022 .net framework windowsフォーム
【実装の目標】
1.csvファイルの読み込み、格納
2.検索、格納
3.チェックしているチェックリストボックスのときに値を格納
4.値を格納した変数を次のフォームに値を渡す
【事前実装】
・条件である「チェックしたリストのとき」の実装で参考記事を確認しましたが、使用の方法がわからず。。
【検証】
・条件分岐がうまくいかなかったり、チェック以外のすべての内容が変数に入る。
※プログラミング初心者兼勉強中です。
プログラマーの先輩方よろしくお願いいたします。
■Form1.cs[デザイン]
checkListBox1
textBox1
button1
button2
■Form2.cs[デザイン]
button1
button2
csv
1番号,種類,種類(日本語),果物名(日本語),果物名(英語),価格,在庫,在庫数,購入,手数料,購入数 21,fruits,フルーツ,りんご,apple,250円,yes,10,yes,0,6 32,fruits,フルーツ,おれんじ,orange,120円,yes,60,yes,0,2 43,fruits,フルーツ,もも,peach,350円,yes,6,yes,0,3 54,fruits,フルーツ,ぎんなん,ginkgo,150円,yes,45,yes,0,2 65,fruits,フルーツ,すいか,watermelon,500円,none,0,none,0,0 76,meat,肉,鶏肉,bird,800円,yes,100,none,0,0 87,meat,肉,豚肉,pig,400円,yes,60,none,0,0 98,meat,肉,牛肉,cow,1200円,yes,1000,yes,0,30 109,meat,肉,羊肉,sheep,2000円,yes,500,yes,0,15 1110,meat,肉,馬肉,horse,5000円,yes,30,yes,0,10
■Form1,cs
c#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Collections; 9using System.IO; 10using System.Threading.Tasks; 11using System.Windows.Forms; 12 13namespace WindowsFormsApp2 14{ 15 public partial class Form1 : Form 16 { 17 //csvファイルの中身を変数に格納 18 List<string[]> foodlists = new List<string[]>(); 19 20 //検索結果を格納 21 List<string[]> foodreslist = new List<string[]>(); 22 23 24 //一つに検索した内容を格納 25 List<string[]> fooddata = new List<string[]>(); 26 public Form1() 27 { 28 InitializeComponent(); 29 30 //読み込みモードでcsvファイルを開いた 31 StreamReader sr = new StreamReader(@".\input\fooddata1.csv", Encoding.GetEncoding("sjis")); 32 33 // 末尾まで繰り返す 34 //csvファイルの行数文だけまわす foreachが使えない 35 //EndOfStreamはファイルの最後を意味する ファイルの最後まで 36 while (!sr.EndOfStream) 37 { 38 // CSVファイルの一行を読み込む 39 string line = sr.ReadLine(); 40 // 読み込んだ一行をカンマ毎に分けて配列に格納する 41 string[] values = line.Split(','); 42 //foodlistsに格納 43 foodlists.Add(values); 44 45 } 46 //csvファイルを閉じる 47 sr.Close(); 48 } 49 50 51 int lastCheckedIndex = -1; 52 private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) 53 { 54 //チェックを1つだけ 55 if (e.Index != lastCheckedIndex) 56 { 57 if (lastCheckedIndex != -1) 58 { 59 checkedListBox1.SetItemCheckState(lastCheckedIndex, CheckState.Unchecked); 60 } 61 lastCheckedIndex = e.Index; 62 63 // フォーム2を表示 64 Form2 form2 = new Form2(); 65 form2.ShowDialog(); 66 } 67 68 } 69 70 //検索ボタンを押したとき 71 private void button1_Click(object sender, EventArgs e) 72 { 73 74 //検索結果を初期化 75 foodreslist.Clear(); 76 //検索結果を初期化 77 checkedListBox1.Items.Clear(); 78 79 //変数の中身を回す 80 foreach (string[] foodlist in foodlists) 81 { 82 //入力内容とおなじのとき(meat,or,fruits) 83 if (textBox1.Text == foodlist[1]) 84 { 85 86 checkedListBox1.Items.Add(foodlist[3]); 87 //格納 88 foodreslist.Add(foodlist); 89 } 90 } 91 92 } 93 94 //選択ボタンを押したとき 95 private void button2_Click(object sender, EventArgs e) 96 { 97 //チェックリストボックスにチェックがついているとき 98 if (checkedListBox1.CheckedItems.Count != 0) 99 { 100 int i; 101 102 //チェックされたリストを確認のため回す 103 foreach (string[] foodseach in foodreslist) 104 { 105 106 ////チェックがついているリストのとき 107 if (checkedListBox1.GetItemChecked(i))//エラー 108 { 109 //チェックしたリストを変数に格納 110 fooddata.Add(foodseach); 111 } 112 i++; 113 } 114 115 116 //フォーム3を開く 117 Form3 form3 = new Form3(); 118 form3.ShowDialog(); 119 } 120 else 121 { 122 //「チェックされておりません」メッセージボックスを表示 123 MessageBox.Show("ERROR", 124 "警告", 125 MessageBoxButtons.OK, 126 MessageBoxIcon.Warning); 127 return; 128 } 129 } 130 } 131}
■Form2.cs
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10 11namespace WindowsFormsApp2 12{ 13 public partial class Form2 : Form 14 { 15 public Form2() 16 { 17 InitializeComponent(); 18 } 19 20 private void button1_Click(object sender, EventArgs e) 21 { 22 this.Close(); 23 } 24 25 private void button2_Click(object sender, EventArgs e) 26 { 27 this.Close(); 28 } 29 } 30} 31
回答1件
あなたの回答
tips
プレビュー