public abstract class Shape<S,T>
{
T _Result=default(T);
S _ShapeName = default(S);
//Calculate Shape Area
public virtual T Area(T Par1, T Par2) { return _Result; }
//Calculate Shape Volume
public virtual T Volume(T Length, T Width, T Height) { return _Result; }
//Calculate Triangle Volume
public virtual T Volume(T Bottom, T Height) { return _Result; }
//Show Is ShapeName
public virtual S ShapeName() { return _ShapeName; }
}
public class Square : Shape<string,double>
{
public override double Area(double Length, double Width)
{
return Length * Width;
}
public override double Volume(double Length, double Width, double Height)
{
return Length * Width * Height;
}
public override string ShapeName()
{
return "Shape";
}
}
public class Triangle : Shape<string, int>
{
public override int Area(int Bottom, int Height)
{
return Bottom * Height / 2;
}
public override int Volume(int Bottom, int Height)
{
return Bottom * Height / 2*Height;
}
public override string ShapeName()
{
return "Triangle";
}
}
private void btnAction_Click(object sender, EventArgs e)
{
Shape<string,int> ishap = new Triangle();
MessageBox.Show(ishap.ShapeName() + "面積 : " + ishap.Area(8, 9) + "\n" + ishap.ShapeName() + "體積 : " + ishap.Volume(8, 9));
}
執行結果:
2013年12月21日 星期六
2013年12月17日 星期二
[C#] 利用LIQN 實作 兩個DataTable 差集
private DataTable _dt1 ;
private DataTable _dt2;
public Form1()
{
InitializeComponent();
_dt1 = new DataTable();
_dt2 = new DataTable();
}
private void Form1_Load(object sender, EventArgs e)
{
_dt1.Columns.Add(new DataColumn("Root", typeof(string)));
DataRow r;
for (int i = 1; i < 6; i++)
{
r = _dt1.NewRow();
r["Root"] = i;
_dt1.Rows.Add(r);
}
_dt2.Columns.Add(new DataColumn("Root", typeof(string)));
for (int i = 1; i < 11; i++)
{
r = _dt2.NewRow();
r["Root"] = i;
_dt2.Rows.Add(r);
}
}
private void btnAction_Click(object sender, EventArgs e)
{
IEnumerable<DataRow> Query1 = _dt1.AsEnumerable();
IEnumerable<DataRow> Query2 = _dt2.AsEnumerable();
DataTable dt = Query2.Except(Query1, DataRowComparer.Default).CopyToDataTable();
dataGridView1.DataSource = dt;
}
結果如下:
private DataTable _dt2;
public Form1()
{
InitializeComponent();
_dt1 = new DataTable();
_dt2 = new DataTable();
}
private void Form1_Load(object sender, EventArgs e)
{
_dt1.Columns.Add(new DataColumn("Root", typeof(string)));
DataRow r;
for (int i = 1; i < 6; i++)
{
r = _dt1.NewRow();
r["Root"] = i;
_dt1.Rows.Add(r);
}
_dt2.Columns.Add(new DataColumn("Root", typeof(string)));
for (int i = 1; i < 11; i++)
{
r = _dt2.NewRow();
r["Root"] = i;
_dt2.Rows.Add(r);
}
}
private void btnAction_Click(object sender, EventArgs e)
{
IEnumerable<DataRow> Query1 = _dt1.AsEnumerable();
IEnumerable<DataRow> Query2 = _dt2.AsEnumerable();
DataTable dt = Query2.Except(Query1, DataRowComparer.Default).CopyToDataTable();
dataGridView1.DataSource = dt;
}
結果如下:
2013年12月16日 星期一
[C#]利用BindingSource實現DataGridView 排序
private DataTable _dt;
private BindingSource _BindingSource;
public Form1()
{
InitializeComponent();
_dt = new DataTable();
_BindingSource = new BindingSource();
}
private void Form1_Load(object sender, EventArgs e)
{
_dt.TableName = "TestDataTable";
_dt.Columns.Add(new DataColumn("Sroting", typeof(int)));
_dt.Columns.Add(new DataColumn("Name", typeof(string)));
DataRow dr;
for (int i = 1; i < 10; i++)
{
dr = _dt.NewRow();
dr["Sroting"] = i;
dr["Name"] = "Text" + i.ToString();
_dt.Rows.Add(dr);
}
}
private void button1_Click(object sender, EventArgs e)
{
_BindingSource.DataSource = _dt;
_BindingSource.Sort = "Sroting Asc";
dataGridView1.DataSource = null;
dataGridView1.DataSource = _BindingSource;
}
private void btnUpSorting_Click(object sender, EventArgs e)
{
if (_BindingSource.Position == 0) {
return;
}
((DataRowView)_BindingSource[_BindingSource.Position - 1]).Row.BeginEdit();
((DataRowView)_BindingSource[_BindingSource.Position - 1]).Row["Sroting"] = (int)((DataRowView)_BindingSource[_BindingSource.Position - 1]).Row["Sroting"] + 1;
((DataRowView)_BindingSource[_BindingSource.Position - 1]).Row.EndEdit();
((DataRowView)_BindingSource[_BindingSource.Position]).Row.BeginEdit();
((DataRowView)_BindingSource[_BindingSource.Position]).Row["Sroting"] = (int)((DataRowView)_BindingSource[_BindingSource.Position]).Row["Sroting"] - 1;
((DataRowView)_BindingSource[_BindingSource.Position]).Row.EndEdit();
}
private void btnDownSorting_Click(object sender, EventArgs e)
{
if (_BindingSource.Position == (_dt.Rows.Count-1))
{
return;
}
((DataRowView)_BindingSource[_BindingSource.Position + 1]).Row.BeginEdit();
((DataRowView)_BindingSource[_BindingSource.Position + 1]).Row["Sroting"] = (int)((DataRowView)_BindingSource[_BindingSource.Position + 1]).Row["Sroting"] - 1;
((DataRowView)_BindingSource[_BindingSource.Position + 1]).Row.EndEdit();
((DataRowView)_BindingSource[_BindingSource.Position]).Row.BeginEdit();
((DataRowView)_BindingSource[_BindingSource.Position]).Row["Sroting"] = (int)((DataRowView)_BindingSource[_BindingSource.Position]).Row["Sroting"] + 1;
((DataRowView)_BindingSource[_BindingSource.Position]).Row.EndEdit();
}
執行結果:
private BindingSource _BindingSource;
public Form1()
{
InitializeComponent();
_dt = new DataTable();
_BindingSource = new BindingSource();
}
private void Form1_Load(object sender, EventArgs e)
{
_dt.TableName = "TestDataTable";
_dt.Columns.Add(new DataColumn("Sroting", typeof(int)));
_dt.Columns.Add(new DataColumn("Name", typeof(string)));
DataRow dr;
for (int i = 1; i < 10; i++)
{
dr = _dt.NewRow();
dr["Sroting"] = i;
dr["Name"] = "Text" + i.ToString();
_dt.Rows.Add(dr);
}
}
private void button1_Click(object sender, EventArgs e)
{
_BindingSource.DataSource = _dt;
_BindingSource.Sort = "Sroting Asc";
dataGridView1.DataSource = null;
dataGridView1.DataSource = _BindingSource;
}
private void btnUpSorting_Click(object sender, EventArgs e)
{
if (_BindingSource.Position == 0) {
return;
}
((DataRowView)_BindingSource[_BindingSource.Position - 1]).Row.BeginEdit();
((DataRowView)_BindingSource[_BindingSource.Position - 1]).Row["Sroting"] = (int)((DataRowView)_BindingSource[_BindingSource.Position - 1]).Row["Sroting"] + 1;
((DataRowView)_BindingSource[_BindingSource.Position - 1]).Row.EndEdit();
((DataRowView)_BindingSource[_BindingSource.Position]).Row.BeginEdit();
((DataRowView)_BindingSource[_BindingSource.Position]).Row["Sroting"] = (int)((DataRowView)_BindingSource[_BindingSource.Position]).Row["Sroting"] - 1;
((DataRowView)_BindingSource[_BindingSource.Position]).Row.EndEdit();
}
private void btnDownSorting_Click(object sender, EventArgs e)
{
if (_BindingSource.Position == (_dt.Rows.Count-1))
{
return;
}
((DataRowView)_BindingSource[_BindingSource.Position + 1]).Row.BeginEdit();
((DataRowView)_BindingSource[_BindingSource.Position + 1]).Row["Sroting"] = (int)((DataRowView)_BindingSource[_BindingSource.Position + 1]).Row["Sroting"] - 1;
((DataRowView)_BindingSource[_BindingSource.Position + 1]).Row.EndEdit();
((DataRowView)_BindingSource[_BindingSource.Position]).Row.BeginEdit();
((DataRowView)_BindingSource[_BindingSource.Position]).Row["Sroting"] = (int)((DataRowView)_BindingSource[_BindingSource.Position]).Row["Sroting"] + 1;
((DataRowView)_BindingSource[_BindingSource.Position]).Row.EndEdit();
}
執行結果:
2013年10月26日 星期六
[C#]利用除法原理讀取矩陣(二維陣列)
程式碼如下:
private void Result(int[,] Result)
{
int ResultRows = 0, ResultCols = 0;
ResultRows = Result.GetLength(0);
ResultCols = Result.GetLength(1);
int k = ResultRows * ResultCols;
int x=0,y=0;
string result = "";
for (int i = 0; i < k; i++)
{
//i:被除數 ResultCols:除數
x = i / ResultCols;//商數
y = (i - (x * ResultCols));//餘數
if (y == (ResultCols -1))
{
result += Result[x, y] + "\n";
} else {
result += Result[x, y] + " ";
}
}
MessageBox.Show(result);
}
訂閱:
文章 (Atom)