C# 桌面程序开发 小技能

更新中...

  1. 窗体不能被拉伸 FormBorderStyle 属性设置为FixedToolWindow
  2. 默认居中显示 StartPosition : CenterScreen
//textbox   提示文字 (来源:百度知道)
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace Baidu_20131113_1
{
    public partial class Form1 : Form
    {
        /// 
        /// 文本框中的提示信息
        /// 
        private const string Hint = "input something here...";
 
        public Form1()
        {
            InitializeComponent();
        }
 
        /// 
        /// 用于处理文本框的单击事件
        /// 
        /// 
        /// 
        private void textBoxMessage_Click(object sender, EventArgs e)
        {
            // 如果文本框的文本值不等于提示信息,直接返回
            if (!this.textBoxMessage.Text.Trim().Equals(Hint))
            {
                return;
            }
            // 否则
            // 将文本框的字体颜色设为黑色
            this.textBoxMessage.ForeColor = Color.Black;
            // 将文本框清空
            this.textBoxMessage.Clear();
        }
 
        /// 
        /// 用于处理文本框的鼠标离开事件
        /// 
        /// 
        /// 
        private void textBoxMessage_MouseLeave(object sender, EventArgs e)
        {
            // 如果文本框的文本值不为空,直接返回
            if (!string.IsNullOrEmpty(this.textBoxMessage.Text.Trim()))
            {
                return;
            }
            // 否则
            // 将文本框的字体颜色设为银色
            this.textBoxMessage.ForeColor = Color.Silver;
            // 将文本框的文本值设为提示信息
            this.textBoxMessage.Text = Hint;
        }
    }
}
参考资料

网络请求
网络请求
listview分页
listview分页
winform控件自定义等

CYQ 连接access数据库 以及读取数据遍历
    /// MAction("表名","连接字符串")
 using (MAction action = new MAction("test", string.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}", AppDomain.CurrentDomain.BaseDirectory + "Database1.accdb"))) {
                MDataTable table =   action.Select();
                //string str = table.Rows[0]["姓名"].Value.ToString();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    foreach (MDataCell mdc in table.Rows[i]) {
                        string strValue = mdc.Value.ToString();
                        string strName = mdc.ColumnName.ToString();
                        System.Diagnostics.Debug.WriteLine(" ddd  "+strName+" --- " + strValue);
                    }
                   
                }
            }
CYQ连接文本数据库 读取数据
//void LoadData()    
        //{
        //    int count;
        //    using (MAction action = new MAction("Users.txt", "Txt Path={0}"))
        //    {
        //        //从文件中获取内容
        //        action.Select(pagerControl2.PageIndex, pagerControl2.PageSize, string.Empty, out count).Bind(dataGridView1);
        //        System.Diagnostics.Debug.WriteLine(pagerControl2.PageIndex + "---"+ pagerControl2.Size);
        //        System.Diagnostics.Debug.WriteLine(""+count);
        //        //LogUtils.AddUpdateLog("ERROR","日志工具");
        //        if (count < 10) {
        //        }
        //        else
        //        {
        //            pagerControl2.DrawControl(count);
        //        }
        //    }
        //}
  /// 
        /// 创建文件件数据库表  并插入数据
        /// 
        /// 
        /// 
        private void StartForm_Load(object sender, EventArgs e)
        {
            //创建文件数据库表。
            MDataTable.CreateSchema("Users.txt", false, new string[] { "TiMing", "PassWord", "Enabled" }, SqlDbType.NText, SqlDbType.NText, SqlDbType.NText);
            for (int i = 0; i < 200; i++)//插入200条数据。
            {
                using (MAction action = new MAction("Users.txt", "Txt Path={0}"))
                {
                    action.Set("TiMing", "题名" + i);
                    action.Set("PassWord", "密码" + i);
                    action.Set("Enabled", i % 2 == 0);
                    action.Insert(InsertOp.None);
                }
            }
            //MessageBox.Show("ddd","ddd");
            pagerControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);
            LoadData();
        }

CYQ的相关文档
一款相当给力的数据库操作组件

用MDataColumns 复制表结构并创建新的表
string conn = "数据库连接串";
using (MAction action = new MAction("test", conn)) {
    mdc = action.Data.Columns;
}
bool a =  DBTool.CreateTable("test2",mdc,conn);
自己指定字段创建新表
  string conn = "数据库连接串";
mdc = new MDataColumn();
mdc.Add("测试字段",SqlDbType.Text);
mdc.Add("测试字段2",SqlDbType.Text);
mdc.Add("测试字段3",SqlDbType.Text);
mdc.Add("测试字段4",SqlDbType.Text);
mdc.Add("测试字段5",SqlDbType.Text);
mdc.Add("测试字段6",SqlDbType.Text);
bool a = DBTool.CreateTable("TEST3",mdc,conn);
if (a)
{
    MessageBox.Show("success");
}
else {
    MessageBox.Show("failed");
}
.net 中没有 ConfigurationManager 解决办法

在引用的文件夹右键添加引用,选择程序集,搜索 configuration 勾选确定即可

从配置文件中读取 配置

    


    

string conn = ConfigurationManager.ConnectionStrings["strCon"].ToString();
string conn = ConfigurationManager.AppSettings["aaa"];

C# Winform 窗体美化(目录)

第一种 关闭当前窗口,并开启新的窗口 (使用的时候提示过时,原理是开启一个新的线程去启动这个窗口)
                        BookSellerMain bsm = new BookSellerMain();
                        //账号、密码验证通过后,执行以下代码
                        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
                            #pragma warning disable CS0618 // 类型或成员已过时
                        t.ApartmentState = System.Threading.ApartmentState.STA;
                            #pragma warning restore CS0618 // 类型或成员已过时
                        t.Start();
                        bsm.myevent += new BookSellerMain.dd(BookSellerMain_myevent);
                        this.Close();
第二种 (也有人说 无法关闭当前窗口,并开启新的窗口)
//两个窗口form1和form2 
//点击form1中的一个按钮,打开form2同时关闭form1

form2 f2 = new form2();

f2.show();

this.close();  //也可以直接 close();

(注意:如果form1是主窗口。不可以close只能hide ,即this.hide())

若要退出当前程序:Application.exit();
点击右上角关闭触发的方法
 protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
           /// MessageBox.Show("OnClosed");
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
          //  MessageBox.Show("OnFormClosing");
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
          //  MessageBox.Show("OnFormClosed");
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
           // MessageBox.Show("OnClosing");
        }

C# 关于单例的简单使用

C# 解析的一点东西

你可能感兴趣的:(C# 桌面程序开发 小技能)