c# - If-statement inside Add() method? -
i'm trying add each line of textfile different columns in listview
. however, i've run problem.
this how did method:
public void openfile() { openfiledialog openfiledialog = new openfiledialog(); openfiledialog.filter = "text files (*.txt)|*.txt|all files (*.*)|*.*"; string line = ""; int index = 0; if (openfiledialog.showdialog() == true) using (streamreader sr = file.opentext(openfiledialog.filename)) { while ((line = sr.readline()) != null) { index++; if (index == 1) invoicenumbertxt.text = line; else if (index == 2) invoicedatetxt.text = line; else if (index == 3) duedatetxt.text = line; else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9) personinfolst.items.add(line); else if (index == 10) { items.add(new itemproperties { item = line if(index == 11)// <---- if-statement inside add? description = line; }); itemlst.itemssource = items; } else break; } } }
as can see index convenient flag (variable) insert lines in order, , not overlap multiple lines same control.
the problem have want check if index value inside add() method can add new textfile line same row different column in list.
update:
public partial class mainwindow : window { observablecollection<itemproperties> items = new observablecollection<itemproperties>(); public mainwindow() { initializecomponent(); } public observablecollection<itemproperties> gamecollection { { if (items == null) { items = new observablecollection<itemproperties>(); } return items; } } private void btnopenfile_click(object sender, routedeventargs e) { openfile(); } public void openfile() { openfiledialog openfiledialog = new openfiledialog(); openfiledialog.filter = "text files (*.txt)|*.txt|all files (*.*)|*.*"; string line = ""; int index = 0; if (openfiledialog.showdialog() == true) using (streamreader sr = file.opentext(openfiledialog.filename)) { while ((line = sr.readline()) != null) { index++; if (index == 1) invoicenumbertxt.text = line; else if (index == 2) invoicedatetxt.text = line; else if (index == 3) duedatetxt.text = line; else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9) personinfolst.items.add(line); else if (index == 10) { items.add(new itemproperties { item = line }); itemlst.itemssource = items; } else if (index == 11) { //?? } else break; } } } private void btnopenimage_click(object sender, system.windows.routedeventargs e) { microsoft.win32.openfiledialog openfile = new microsoft.win32.openfiledialog(); openfile.defaultext = "*.jpg"; openfile.filter = "image files|*.jpg"; nullable<bool> result = openfile.showdialog(); if (result == true) { imagefile.source = new bitmapimage(new uri(openfile.filename)); } } public class itemproperties { public string item { get; set; } public string description { get; set; } public string quantity { get; set; } public string unitprice { get; set; } public string tax { get; set; } } }
you can use ternary operator perform inline conditional check , set value
description = (index == 11) ? line : "";
Comments
Post a Comment