public void InitializeObject() { IsThisClose = true; newReportItemCollection = new ReportItemCollection(); // FormMainインスタンスのListインスタンスに追加するReportItemCollectionインスタンスの作成 newItemBindingSource.DataSource = newReportItemCollection; // FormNewItemインスタンスとReportItemCollectionインスタンスとのデータバインディング newItemContentTextBox.DataBindings.Add ("Text", newItemBindingSource, "AddItemContent", false, DataSourceUpdateMode.OnPropertyChanged); }
private const int ItemContent = 3000; // このフォームの内容(本文)に入力可能な文字数。 #endregion Fields #region Methods public IEnumerable<ReportItemCollection> ImportCsv(string filename) { ReportItemCollection reportitem = new ReportItemCollection(); // 戻り値となるReportItemCollection Stream _stream = new FileStream(filename, FileMode.Open, FileAccess.Read); // 読み込んだファイルサイズを算出するため、ファイルストリームインスタンス作成。 Encoding _encode = Encoding.GetEncoding("Shift_JIS"); // Shift_JIS用のエンコードインスタンス作成。 using (TextFieldParser _txtfieldP = new TextFieldParser(_stream, _encode)) // TextFieldParserクラスを使用し、構造化されたテキストを解析する。 { _txtfieldP.TextFieldType = FieldType.Delimited; // テキストのフィールドが区切り形式である事を指定。 _txtfieldP.SetDelimiters(","); // 区切り形式はカンマ記号によって区切られる事を指定。 _txtfieldP.HasFieldsEnclosedInQuotes = false; // 各区切りフィールドはダブルクォーテーションによって区切られていない事を指定。 _txtfieldP.TrimWhiteSpace = false; // 各区切りフィールドの前後から空白を削除しない事を指定。 while (!_txtfieldP.EndOfData) // 指定されたファイルの終端まで繰り返す。 { string[] _tmpArray = _txtfieldP.ReadFields(); // 一行読み込み、配列に格納。 reportitem = CreateReportItem(_tmpArray); // CreateReportItemメソッドにより正しいフォーマットであれば、ReportItemCollectionインスタンスに追加する。 reportitem.NowReadStream = _stream.Position; // 現在まで読み込んだファイルサイズをReportItemCollectionインスタンスのプロパティに格納。 yield return reportitem; // yieldで、現在読み込んだ一行に関するReportItemCollectionインスタンスを返す。 } } }
private ReportItemCollection CreateReportItem(string[] fields) { try { if (fields.Length != 6) // 各区切りフィールドの数の確認。 { throw new ExceptionImportData(); // 各区切りフィールドの数が規定値と異なっていた場合は例外をスローする。 } } catch (ExceptionImportData ExcImp) { ShowMyDialog.ShowInfoMessageBox(ExcImp.Message, "デバッグ用"); // 例外のキャッチ。 } ReportItemCollection importItem = new ReportItemCollection(); // 戻り値用にReportItemCollectionインスタンスを作成。 importItem.ItemTitle = fields[0].TrimEnd(); // パラメータの配列field[0]の末尾から空白を取り除き、一旦ReportItemCollectionインスタンスのItemTitleプロパティに格納。 if (importItem.ItemTitle.Length == 0 || importItem.ItemTitle.Length > 32) // ReportItemCollectionのItemTitleプロパティが0文字あるいは32文字より多いかを確認。 { throw new ExceptionImportData(); // 0文字あるいは32文字より多い場合は例外をスローする。 } DateTime _tmpDate; // パラメータの配列field[1]が日付に変換出来るか確認するためのDateTime構造体。 if (!DateTime.TryParse(fields[1], out _tmpDate)) // パラメータの配列field[1]が日付に変更出来るかを確認。 { throw new ExceptionImportData(); // 日付に変更出来ない場合は例外をスローする。 } importItem.ItemDate = _tmpDate; // 日付に変更出来る場合は、ReportItemCollectionのItemDateプロパティに格納。 importItem.ItemType = fields[2].TrimEnd(); // パラメータの配列field[2]の末尾から空白を取り除き、一旦ReportItemCollectionインスタンスのItemTypeプロパティに格納。 if (!(importItem.ItemType.Equals("議事録") || // ReportItemCollectionのItemTypeプロパティが"議事録"あるいは"業務案件"あるいは"業務知識"あるいは"提出物"の何れかであるかを確認。 importItem.ItemType.Equals("業務案件") || importItem.ItemType.Equals("業務知識") || importItem.ItemType.Equals("提出物") || importItem.ItemType.Equals("その他"))) { throw new ExceptionImportData(); // "議事録"あるいは"業務案件"あるいは"業務知識"あるいは"提出物"の何れかでない場合は例外をスローする。 } ConversionRegular convertString = new ConversionRegular(); // ConversionRegularインスタンス(置き換え文字の置き換えを行う)作成。 convertString.TextImportConvert(fields[3].TrimEnd()); // 特殊な置き換え文字を改行文字変更する。 importItem.ItemContent = convertString.ImportString; // パラメータの配列field[3]の末尾から空白を取り除き、一旦ReportItemCollectionインスタンスのItemContentプロパティに格納。 if (importItem.ItemContent.Length == 0 || // 0文字あるいは1000文字より多い場合は例外をスローする。 importItem.ItemContent.Length > ItemContent) { throw new ExceptionImportData(); } bool _tmpBool; // パラメータの配列field[4]がbool値に変換出来るかを確認する為の変数。 if (!bool.TryParse(fields[4], out _tmpBool)) // パラメータの配列field[4]がbool値に変換出来るかを確認。 { throw new ExceptionImportData(); // bool値に変換出来ない場合は例外をスローする。 } importItem.AlarmOrNotFlag = _tmpBool; // bool値に変換出来る場合は、ReportItemCollectionのAlarmOrNotFlagプロパティに格納。 importItem.AlarmType = fields[5].TrimEnd(); // パラメータの配列field[5]の末尾から空白を取り除き、一旦ReportItemCollectionインスタンスのAlarmTypeプロパティに格納。 if (!(importItem.AlarmType.Equals("アラームなし") || // ReportItemCollectionのAlarmTypeプロパティがあるいは"アラーム1"あるいは"アラーム2"あるいは"アラーム3"あるいは"アラーム4"の何れかであるかを確認。 importItem.AlarmType.Equals("アラーム1") || importItem.AlarmType.Equals("アラーム2") || importItem.AlarmType.Equals("アラーム3") || importItem.AlarmType.Equals("アラーム4") || importItem.AlarmType.Equals("アラーム5") || importItem.AlarmType.Equals("アラーム6"))) { throw new ExceptionImportData(); // "アラーム1"あるいは"アラーム2"あるいは"アラーム3"あるいは"アラーム4"の何れかでない場合は例外をスローする。 } return importItem; // フォーマットの確認が完了し、各区切りフィールドの内容を格納したReportItemCollectionインスタンスを戻す。 }