Đọc File text bằng C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace NhapDuLieu
{
public class DuLieuFileTxt
{
private int _n; // Số phần tử
public int N
{
get { return _n; }
set { _n = value; }
}
private List> _MaTran; // Bảng
public List> MaTran
{
get { return _MaTran; }
set { _MaTran = value; }
}
//Thêm header cho listview
public ColumnHeader AddColumnListView(string Text)
{
ColumnHeader HeaderCol = new ColumnHeader();
HeaderCol.Text = Text;
HeaderCol.Width = 50;
HeaderCol.TextAlign = HorizontalAlignment.Center;
HeaderCol.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
return HeaderCol;
}
public List> LoadFile(string fileName, ListView lv)
{
StreamReader fin = new StreamReader(fileName);
this._MaTran = new List>();
this._n = Convert.ToInt32(fin.ReadLine());
string line = "";
List row;
lv.Columns.Add(AddColumnListView("P(X,Y)"));
for (int iX = 0; iX < this._n; ++iX)
{
line = fin.ReadLine();
string[] words = line.Split(' ');
lv.Columns.Add(AddColumnListView("Y"+(iX + 1).ToString())); //Tiêu đề cột
ListViewItem lvi = new ListViewItem("X" + (iX + 1).ToString()); //Tiêu đề dòng
lvi.UseItemStyleForSubItems = false;
row = new List();
for (int iY = 0; iY < this._n; ++iY)
{
// list view sub item
ListViewItem.ListViewSubItem col = lvi.SubItems.Add(words[iY]);
col.ForeColor = Color.Blue; // mau cua list view subitem
row.Add(iY);
}
this._MaTran.Add(row);
lv.Items.Add(lvi);
}
fin.Close();
return this._MaTran;
}
}
}
Post a Comment