C#常用开发技巧

来源:互联网 发布:女生可怕经历知乎 编辑:程序博客网 时间:2024/05/17 03:22

1、字符串简单加解密(常用于配置文件)

static public string Encode(string str)
        {
            string htext = "";
            for (int i = 0; i < str.Length; i++)
            {
                htext = htext + (char)(str[i] + 10 - 1 * 2);
            }
            return htext;
        }
        static public string Decode(string str)
        {
            string dtext = "";
            for (int i = 0; i < str.Length; i++)
            {
                dtext = dtext + (char)(str[i] - 10 + 1 * 2);
            }
            return dtext;
        }

2、From加载时,默认光标。

方法<1>、在Load()事件或者构造函数中加入:this.ActiveControl = control;

方法<2>、将control的TabIndex设置为0;

3、密码文本框

            txt_password.PasswordChar = '*'; //***

            txt_password.UseSystemPasswordChar = true;//小圆点

4、设置窗体为屏幕居中显示
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(SystemInformation.WorkingArea.Width / 2 - this.Width / 2, SystemInformation.WorkingArea.Height / 2 - this.Height / 2);

5、修改配置文件

    XmlDocument doc = new XmlDocument();

            doc.Load(Application.ExecutablePath + ".config");


            node = doc.SelectSingleNode(@"//add[@key='user']");
            ele = (XmlElement)node;
            ele.SetAttribute("value", this.user.Text);

            node = doc.SelectSingleNode(@"//add[@key='password']");
            ele = (XmlElement)node;
            ele.SetAttribute("value", this.password.Text);


            try
            {
                doc.Save(Application.ExecutablePath + ".config");
                MessageBox.Show("保存成功.");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

6、窗体大小改变(展开和收起)

    if (button.Text == "展开>>")
            {
                this.Height += 180;
                button.Text = "收起<<";
            }
            else
            {
                this.Height -= 180;
                button.Text = "展开>>";
            }

7、登录界面关闭,进入主界面

在main()中加入:

    using (Login login = new Login())
            {
                if (login.ShowDialog() == DialogResult.OK)
                {
                    Application.Run(new MainForm());
                }
            }

8、Process

System.Diagnostics.Process.Start("osk.exe");//打开屏幕小键盘

System.Diagnostics.Process.Start("www.baidu.com");//打开网址


0 0