摘要: 1 private void MoveCmdToLast(TextBox txtCmdInput, String selCmd)//把所選中的命令移動到最下一行然后顯示在文本框中 2 { &...
閱讀全文
posted @
2008-09-04 10:35 天書 閱讀(4542) |
評論 (0) |
編輯 收藏
String curRowText = "Hello!"
FileStream fs = new FileStream("D:\\file.txt",FileMode.Append,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Flush();
sw.Write(curRowText);
sw.Flush();
sw.Close();或者:
StreamWriter writer = null;
try
{
writer = new StreamWriter(strFileName, true);
writer.WriteLine(strCmdText);
}
catch (Exception ex)
{
//進行異常處理
}
finally
{
if (writer != null) writetofile.Close();
}
return false;
posted @
2008-09-03 18:17 天書 閱讀(812) |
評論 (0) |
編輯 收藏
private void 導入模板ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = " 請選擇您要導入的模板文件:";
ofd.Filter = "TextDocument(*.cmd)|*.cmd|TextDocument(*.txt)|*.txt";
ofd.ShowDialog();
System.IO.StreamReader sr = new System.IO.StreamReader( ofd.FileName ,System.Text.Encoding.Default);
txtCmdInput.Text =
sr.ReadToEnd(); }
posted @
2008-09-03 16:34 天書 閱讀(1342) |
評論 (1) |
編輯 收藏
1
private void GetLine(TextBox txtCmdInput)//取控件里鼠標所在行的命令發送后提到最前
2
{
3
//取光標所在行的字符串包括末尾的換行回車符"\r\n"
4
string strCmdText = txtCmdInput.Text;
5
int curInx = txtCmdInput.SelectionStart; //光標所在位置索引
6
string tmp = strCmdText.Substring(0, curInx); //開始到光標處的子串
7
int start = tmp.LastIndexOf('\n'); //找光標所在行的開頭索引start + 1
8
9
tmp = strCmdText.Substring(curInx);//當前光標所在位置到最后的子串
10
int end = tmp.IndexOf('\n'); //找該行的末尾索引包括"\r\n"
11
string curRowText = null;
12
if (end > 0)
13
{
14
curRowText = strCmdText.Substring(start + 1, curInx - start + end);
15
}
16
else
17
{
18
curRowText = strCmdText.Substring(start + 1);
19
}
20
//把光標所在行的命令提到第一行的下一行
21
String strLeft = strCmdText.Remove(start + 1, curRowText.Length);
22
23
//處理剩下的字符串,注意把開頭結尾的"\r\n"找到刪掉
24
if (strLeft != "")
25
{
26
while (strLeft[strLeft.Length - 1] == '\r' || strLeft[strLeft.Length - 1] == '\n')
27
{
28
strLeft = strLeft.Remove(strLeft.Length - 1, 1);
29
}
30
}
31
if (strLeft != "")
32
{
33
while (strLeft[0] == '\r')
34
{
35
strLeft = strLeft.Remove(0, 2);
36
}
37
}
38
//處理你取出的當前行的字符串若有"\r\n"注意把它去掉
39
if (curRowText != "" && curRowText.Length > 0)
40
{
41
while (curRowText[curRowText.Length - 1] == '\r' || curRowText[curRowText.Length - 1] == '\n')
42
{
43
curRowText = curRowText.Remove(curRowText.Length - 1, 1);
44
}
45
}
46
String strNew = curRowText + "\r\n" + strLeft;
47
//最后前面留一行空格且把鼠標定位到此
48
txtCmdInput.Text = "\r\n" + strNew;
49
}
接著引發textbox控件的KeyDown事件 private void txtCmdInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//發送光標所在行指令,且把它提到頭一行
GetLine(txtCmdInput);
e.SuppressKeyPress = true;//回車事件已經處理完不再響應了 }
}
posted @
2008-09-03 15:09 天書 閱讀(4931) |
評論 (3) |
編輯 收藏
private void myNeTree_MouseDown(object sender, MouseEventArgs e)
{
MyTreeView mtreev = (MyTreeView)sender;
if(e.Button == MouseButtons.Right)
{
if (this.myNeTree.SelectedNode != null && this.myNeTree.SelectedNode.Nodes.Count == 0)
{
Point p = new Point(e.X,e.Y);
TreeNode tn = mtreev.GetNodeAt(p);//根據鼠標右鍵點下的位置,得到該位置的節點
if(myNeTree.SelectedNode.Equals(tn))//看當前鼠標位置所在的節點是否為當前控件樹中選中的節點
{
this.rightMenu.Show(mtreev, p.X, p.Y);
}
}
}
}
因為MyTreeView 是動態加載:
private MyTreeView myNeTree;
ControlContainerItem contNe = new ControlContainerItem("myNeTree", "網元");
myNeTree = new BurEmluator.MyTreeView();
myNeTree.Name = "myNeTree";
myNeTree.Size = new System.Drawing.Size(95, 350);
//contNe鼠標右鍵監聽事件
myNeTree.MouseDown += new MouseEventHandler(myNeTree_MouseDown);
contNe.Control = myNeTree;
this.NeGroup1.SubItems.AddRange(new DevComponents.DotNetBar.BaseItem[] { radiocont, contNe });
posted @
2008-09-02 15:47 天書 閱讀(1343) |
評論 (0) |
編輯 收藏
C#事件支持發布者/訂閱者模式,發布者將事件通知給訂閱者,而訂閱者在事件發生時調用已經注冊好的事件處理函數。
public delegate void delUpdate(); //委托定義,相當于一個函數簽名,函數指針
public event delUpdate ENotify; //定義事件,該事件引發此委托類型的事件處理函數
private int a = 2;
public int A
{
get { return a; }
set
{
a = value;
if (ENotify != null) //如果事件不等于空就是說有訂閱者注冊過該事件,比如:Publisher.getInstance().ENotify +=new Publisher.delUpdate(GetData);也就是說觸發事件后有相應的事件處理函數被調用。
{
ENotify();
} }
}
public Observer()
{
Publisher.getInstance().ENotify +=new Publisher.delUpdate(GetData);
}
public void GetData()
{
oa = Publisher.getInstance().A;
ob = Publisher.getInstance().B;
oc = Publisher.getInstance().C;
}
posted @
2008-06-30 11:07 天書 閱讀(1507) |
評論 (0) |
編輯 收藏
首先可以在解決方案資源管理器中添加->新建項目->配置文件(.config),
寫配置文件,如:
1
<?xml version="1.0" encoding="utf-8" ?>
2
<configuration>
3
<appSettings>
4
<add key ="FactoryName" value="ChineseFactory"/>
5
</appSettings>
6
</configuration>
讀配置文件
private string FactoryType = null;
System.Configuration.AppSettingsReader asr = new System.Configuration.AppSettingsReader();
1
FactoryType = (string)asr.GetValue("FactoryName", typeof(string));
注意語句要寫在函數里別直接寫在類里面了!!!!!!!!!!!!!!!!!
posted @
2008-06-24 11:08 天書 閱讀(627) |
評論 (0) |
編輯 收藏
Abstract Factory 模式:關鍵特征
意圖:為特定(不同)的客戶提供特定(不同)系列的對象。
比如Vista風格的桌面,Window標準的桌面(其中包括圖標的樣式,菜單欄,任務欄等)
問題:一系列相關或相互依賴的對象需要被實例化
解決方案:
先定義一個抽象工廠類來選擇工廠類(可以根據配置文件選取)
1
class AbstractFactory
2
{
3
private string FactoryType = null;
4
System.Configuration.AppSettingsReader asr = new System.Configuration.AppSettingsReader();
5
public IFactory GetFactory()
6
{
7
FactoryType = (string)asr.GetValue("FactoryName", typeof(string));
8
switch(FactoryType)
9
{
10
case "ChineseFactory":
11
return new ChineseFactory();
12
break;
13
case "AmericanFactory":
14
return new AmericanFactory();
15
break;
16
default:
17
return new ChineseFactory();
18
}
19
20
}
21
}
各個工廠類里創建不同系列的對象(同一工廠類里的對象風格相同),但他們有一個共同的接口或父類
1
interface IFactory
2
{
3
Service CreateService();
4
}
5
6
class ChineseFactory : IFactory
7
{
8
9
IFactory 成員#region IFactory 成員
10
11
Service IFactory.CreateService()
12
{
13
return new ChineseService();
14
}
15
16
#endregion
17
}
18
class AmericanFactory : IFactory
19
{
20
IFactory 成員#region IFactory 成員
21
22
Service IFactory.CreateService()
23
{
24
return new AmericanService();
25
}
26
27
#endregion
28
}
最后在客戶端Service se = (new AbstractFactory()).GetFactory().CreateService();
,提供一種“封裝機制”來避免客戶程序和這種“多系列具體對象創建工作”的緊耦合。每次在中國和美國的工資體制上切換時可以通過更改配置文件來切換。這樣就避免了源代碼保密的情況下無法更改業務規則的弊端。
配置文件如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="FactoryName" value="ChineseFactory"/>
</appSettings>
</configuration>
posted @
2008-06-24 11:00 天書 閱讀(837) |
評論 (0) |
編輯 收藏
Strategy策略模式是一種對象行為模式。主要是應對:在軟件構建過程中,某些對象使用的算法可能多種多樣,經常發生變化。如果在對象內部實現這些算法,將會使對象變得異常復雜,甚至會造成性能上的負擔。
GoF《設計模式》中說道:定義一系列算法,把它們一個個封裝起來,并且使它們可以相互替換。該模式使得算法可獨立于它們的客戶變化。
Strategy模式的結構圖如下:
從圖中我們不難看出:Strategy模式實際上就是將算法一一封裝起來,如圖上的ConcreteStrategyA、ConcreteStrategyB、ConcreteStrategyC,但是它們都繼承于一個接口,這樣在Context調用時就可以以多態的方式來實現對于不用算法的調用。
Strategy模式的實現如下:
我們現在來看一個場景:我在下班在回家的路上,可以有這幾種選擇,走路、騎車、坐車。首先,我們需要把算法抽象出來:
public interface IStrategy
{
void OnTheWay();
}
接下來,我們需要實現走路、騎車和坐車幾種方式。
public class WalkStrategy : IStrategy
{
public void OnTheWay()
{
Console.WriteLine("Walk on the road");
}
}
public class RideBickStragtegy : IStrategy
{
public void OnTheWay()
{
Console.WriteLine("Ride the bicycle on the road");
}
}
public class CarStragtegy : IStrategy
{
public void OnTheWay()
{
Console.WriteLine("Drive the car on the road");
}
}
最后再用客戶端代碼調用封裝的算法接口,實現一個走路回家的場景:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Arrive to home");
IStrategy strategy = new WalkStrategy();
strategy.OnTheWay();
Console.Read();
}
}
運行結果如下;
Arrive to home
Walk on the road
如果我們需要實現其他的方法,只需要在Context改變一下IStrategy所示例化的對象就可以。
posted @
2008-06-18 09:38 天書 閱讀(166) |
評論 (0) |
編輯 收藏
基于對象可以這樣說主要看重封裝這個特性的, 即把數據和操作數據的行為封裝; 面向對象主要是在對象封裝之上更加重視“多態性”特性。
|
posted @
2008-06-17 13:04 天書 閱讀(140) |
評論 (0) |
編輯 收藏