//早餐
public abstract class Breakfast {
public abstract int Cost();
}
//主食-吐司
public class Toast: Breakfast
{
public Toast() {
}
public override int Cost()
{
return 10;
}
}
//配料-起司
public class Cheese : Breakfast
{
private Breakfast _breakfast;
public Cheese(Breakfast breakfast)
{
_breakfast = breakfast;
}
public override int Cost()
{
return 20 + _breakfast.Cost(); ;
}
}
//配料-火腿
public class Ham : Breakfast
{
private Breakfast _breakfast;
public Ham(Breakfast breakfast)
{
_breakfast = breakfast;
}
public override int Cost()
{
return 15+ _breakfast.Cost();
}
}
private void Main()
{
Breakfast b = new Toast();
b = new Ham(b);
b = new Cheese(b);
Console.WriteLine( "價錢 :"+ b.Cost().ToString());
}
如出一轍程式碼
2017年10月6日 星期五
2016年1月26日 星期二
[OpenXML] Word 套版
private void btnDox_Click(object sender, EventArgs e)
{
DocX docx = new DocX();
docx.Load(@"D:\Test.docx");
docx.ReplaceText("$Name$", "你好", "標楷體", System.Drawing.Color.Red);
docx.ReplaceText("$Name1$", "大家好", "標楷體", System.Drawing.Color.Green);
docx.SaveAs(@"D:\Output.docx");
}
public class DocX {
private string _xmlString;
private string _xmlOutputString;
private WordprocessingDocument _Templateword;
private Document _Outputdoc;
private Dictionary<string, Run> _dic;
public DocX() {
_xmlString = string.Empty;
_Outputdoc = null;
_dic = new Dictionary<string, Run>();
}
public void Load(string Templatefile)
{
_xmlString = string.Empty;
_xmlOutputString = string.Empty;
_Outputdoc = null;
_Templateword = WordprocessingDocument.Open(Templatefile, true);
_Outputdoc = _Templateword.MainDocumentPart.Document;
_Templateword.Close();
_xmlString = _Outputdoc.InnerXml;
}
private void GetRunList() {
_dic = new Dictionary<string, Run>();
foreach (Paragraph pap in _Outputdoc.Descendants<Paragraph>())
{
string tmpe = pap.InnerText;
if (tmpe == "") {
continue;
}
if (_dic.ContainsKey(tmpe) == false)
{
Text text = new Text(tmpe);
Run run = new Run();
run.Append(text);
Paragraph newParagraph = new Paragraph(run);
pap.InnerXml = newParagraph.InnerXml;
Run r = (Run)pap.FirstChild;
_dic.Add(tmpe, r);
}
else { pap.Remove(); }
}
_xmlString = _Outputdoc.InnerXml;
}
private void FontSetting(Run r, string Fonts, System.Drawing.Color color)
{
string ColorCode = string.Empty;
ColorCode = string.Format("{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B);
RunProperties runProp = new RunProperties();
runProp.Color = new Color() { Val = ColorCode };
runProp.RunFonts = new RunFonts { Ascii = Fonts, EastAsia = Fonts };
r.RunProperties = runProp;
_xmlString = _Outputdoc.InnerXml;
}
public void ReplaceText(string oldValue, string newValue, string Fonts, System.Drawing.Color color) {
if (_dic.Count == 0)
{
GetRunList();
}
foreach (Paragraph pap in _Outputdoc.Descendants<Paragraph>())
{
if (pap.InnerText.Trim() == oldValue.Trim()) {
FontSetting((Run)pap.FirstChild, Fonts, color);
break;
}
}
_xmlString = _xmlString.Replace(oldValue, newValue);
_Outputdoc.InnerXml = _xmlString;
}
public void Save()
{
_Outputdoc.Save();
}
public void SaveAs(string OutputFile) {
WordprocessingDocument package = WordprocessingDocument.Create(OutputFile, DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
package.AddMainDocumentPart();
package.MainDocumentPart.Document = new Document(new Body());
package.MainDocumentPart.Document.InnerXml = _Outputdoc.InnerXml;
package.MainDocumentPart.Document.Save();
package.Close();
}
}
{
DocX docx = new DocX();
docx.Load(@"D:\Test.docx");
docx.ReplaceText("$Name$", "你好", "標楷體", System.Drawing.Color.Red);
docx.ReplaceText("$Name1$", "大家好", "標楷體", System.Drawing.Color.Green);
docx.SaveAs(@"D:\Output.docx");
}
public class DocX {
private string _xmlString;
private string _xmlOutputString;
private WordprocessingDocument _Templateword;
private Document _Outputdoc;
private Dictionary<string, Run> _dic;
public DocX() {
_xmlString = string.Empty;
_Outputdoc = null;
_dic = new Dictionary<string, Run>();
}
public void Load(string Templatefile)
{
_xmlString = string.Empty;
_xmlOutputString = string.Empty;
_Outputdoc = null;
_Templateword = WordprocessingDocument.Open(Templatefile, true);
_Outputdoc = _Templateword.MainDocumentPart.Document;
_Templateword.Close();
_xmlString = _Outputdoc.InnerXml;
}
private void GetRunList() {
_dic = new Dictionary<string, Run>();
foreach (Paragraph pap in _Outputdoc.Descendants<Paragraph>())
{
string tmpe = pap.InnerText;
if (tmpe == "") {
continue;
}
if (_dic.ContainsKey(tmpe) == false)
{
Text text = new Text(tmpe);
Run run = new Run();
run.Append(text);
Paragraph newParagraph = new Paragraph(run);
pap.InnerXml = newParagraph.InnerXml;
Run r = (Run)pap.FirstChild;
_dic.Add(tmpe, r);
}
else { pap.Remove(); }
}
_xmlString = _Outputdoc.InnerXml;
}
private void FontSetting(Run r, string Fonts, System.Drawing.Color color)
{
string ColorCode = string.Empty;
ColorCode = string.Format("{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B);
RunProperties runProp = new RunProperties();
runProp.Color = new Color() { Val = ColorCode };
runProp.RunFonts = new RunFonts { Ascii = Fonts, EastAsia = Fonts };
r.RunProperties = runProp;
_xmlString = _Outputdoc.InnerXml;
}
public void ReplaceText(string oldValue, string newValue, string Fonts, System.Drawing.Color color) {
if (_dic.Count == 0)
{
GetRunList();
}
foreach (Paragraph pap in _Outputdoc.Descendants<Paragraph>())
{
if (pap.InnerText.Trim() == oldValue.Trim()) {
FontSetting((Run)pap.FirstChild, Fonts, color);
break;
}
}
_xmlString = _xmlString.Replace(oldValue, newValue);
_Outputdoc.InnerXml = _xmlString;
}
public void Save()
{
_Outputdoc.Save();
}
public void SaveAs(string OutputFile) {
WordprocessingDocument package = WordprocessingDocument.Create(OutputFile, DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
package.AddMainDocumentPart();
package.MainDocumentPart.Document = new Document(new Body());
package.MainDocumentPart.Document.InnerXml = _Outputdoc.InnerXml;
package.MainDocumentPart.Document.Save();
package.Close();
}
}
2014年5月16日 星期五
[C#] 簡單實作MDI Form
private void button1_Click(object sender, EventArgs e)
{
this.Text = "父表單";
//設定為MDI Form(父表單)
this.IsMdiContainer = true;
Form2 f = new Form2();
CreateChildForm(f);
}
private void CreateChildForm(Form f)
{
f.Text = "子表單";
//設定為MDI Form(子表單)
f.MdiParent = this;
//最大化
f.WindowState = FormWindowState.Maximized;
//顯示子表單
f.Show();
}
執行畫面如下:
{
this.Text = "父表單";
//設定為MDI Form(父表單)
this.IsMdiContainer = true;
Form2 f = new Form2();
CreateChildForm(f);
}
private void CreateChildForm(Form f)
{
f.Text = "子表單";
//設定為MDI Form(子表單)
f.MdiParent = this;
//最大化
f.WindowState = FormWindowState.Maximized;
//顯示子表單
f.Show();
}
執行畫面如下:
2014年4月27日 星期日
[C#] LINQ Where In 的寫法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private List<string> str = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
string[] t1 = { "Abel", "Abner") };
str.Add('Abe");;
str.Add("Abel");
str.Add("Abner");
str.Add("Abraham");
var query = from s in str where t1.Contains(s) select s;
foreach (string s in query)
{
MessageBox.Show(s);
}
}
}
}
有興趣的朋友可以試試看喔!!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private List<string> str = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
string[] t1 = { "Abel", "Abner") };
str.Add('Abe");;
str.Add("Abel");
str.Add("Abner");
str.Add("Abraham");
var query = from s in str where t1.Contains(s) select s;
foreach (string s in query)
{
MessageBox.Show(s);
}
}
}
}
有興趣的朋友可以試試看喔!!
2014年1月19日 星期日
[Web] IIS架設簡易流程
1.
安裝 .NET Framework 4.X版本以上
2. 開啟命令字元提示
輸入:
cd C:\WINNT\Microsoft.NET\Framework\v4.0.XXXXX
執行aspnet_regiis –i
3.
控制台\所有控制台項目\系統管理工具
開啟IIS
設定:
1.確認 .NET FrameWork 是否為 4.0版本
2.作業系統為64位元(X64) 假設DLL檔為32位元(X86)
設定為:
2013年12月21日 星期六
[C#] 透過繼承抽象類別(泛型)並實作
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));
}
執行結果:
{
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月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;
}
結果如下:
訂閱:
文章 (Atom)