posts - 124,  comments - 29,  trackbacks - 0
     摘要:   1 private void MoveCmdToLast(TextBox txtCmdInput, String selCmd)//把所选中的命令移动到最下一行然后显示在文本框中  2        { &...  阅读全文
posted @ 2008-09-04 10:35 天书 阅读(4503) | 评论 (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 天书 阅读(795) | 评论 (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 天书 阅读(1324) | 评论 (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 - 11);
29                }

30            }

31            if (strLeft != "")
32            {
33                while (strLeft[0== '\r')
34                {
35                    strLeft = strLeft.Remove(02);
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 - 11);
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 天书 阅读(4840) | 评论 (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 天书 阅读(1334) | 评论 (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 天书 阅读(1499) | 评论 (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();
1FactoryType = (string)asr.GetValue("FactoryName"typeof(string));
注意语句要写在函数里别直接写在类里面了!!!!!!!!!!!!!!!!!
posted @ 2008-06-24 11:08 天书 阅读(619) | 评论 (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 成员
17    }

18    class AmericanFactory : IFactory
19    {
20        IFactory 成员
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 天书 阅读(825) | 评论 (0)编辑 收藏

 

       Strategy策略模式是一种对象行为模式。主要是应对:在软件构建过程中,某些对象使用的算法可能多种多样,经常发生变化。如果在对象内部实现这些算法,将会使对象变得异常复杂,甚至会造成性能上的负担。

       GoF设计模式》中说道:定义一系列算法,把它们一个个封装起来,并且使它们可以相互替换。该模式使得算法可独立于它们的客户变化。

       Strategy模式的结构图如下:


 

 

 


      
从图中我们不难看出:Strategy模式实际上就是将算法一一封装起来,如图上的ConcreteStrategyAConcreteStrategyBConcreteStrategyC,但是它们都继承于一个接口,这样在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 天书 阅读(158) | 评论 (0)编辑 收藏
基于对象可以这样说主要看重封装这个特性的, 即把数据和操作数据的行为封装;
向对象主要是在对象封装之上更加重视“多态性”特性。
posted @ 2008-06-17 13:04 天书 阅读(131) | 评论 (0)编辑 收藏
仅列出标题
共13页: First 5 6 7 8 9 10 11 12 13 

<2010年10月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜