C# WinForm 功能代码备忘-刘欣

工具类:

        // XPtable字段识别
        public static string isXPcell(XPTable.Models.Cell cell)
        {
            string tmp = "";

            if (cell.Text != null) { tmp = Convert.ToString(cell.Text); }
            else { tmp = Convert.ToString(cell.Data); }

            return tmp;
        }

        //excel字段识别
        public static string PickBox(ClosedXML.Excel.IXLCell cell)
        {
            string tmp = "";

            if (!cell.IsEmpty() & cell.Value.IsText) { tmp = cell.Value.GetText(); }            
            else if (!cell.IsEmpty() & cell.Value.IsNumber) { tmp = cell.Value.GetNumber().ToString(); }

            return tmp;
        }

扁平窗口切换


        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {    
            if (e.Node.Text != null)
            { 
                    if      (e.Node.Text.Contains("ADS01")) WinShow(new ADS01(), panel1);
                    else if (e.Node.Text.Contains("ADS02")) WinShow(new ADS02(), panel1); 
                    else if (e.Node.Text.Contains("REPASS")) WinShow(new RePass(), panel1);
             }

        }


        //窗口刷新函数
        private Form WinShow(Form form,Panel panel )
        {
            panel.Controls.Clear();
            form.TopLevel = false;         // 设置TopLevel属性
            form.Parent = panel;          // 设置窗口的父窗
            form.Dock = DockStyle.Fill;    // 子窗体填充父窗体
            form.Show();                   // 显示子窗体

            return form;
        }

日志记录处理

 //日志处理(1/4)
        public static StreamWriter sw = new StreamWriter("SAPsenderlog.txt", true, Encoding.GetEncoding("gb2312"));


 public void ps(ListBox box,string s)
        {

            String line = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + s;
            box.Items.Add(line);    

            //日志处理(2/4)
            sw.WriteLine(line);
        }


//日志处理(3/4)
            sw.Flush(); //缓存区写文件

//日志处理(4/4)
            sw.Close();

按钮中的线程启动:

        //取数线程启动
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //线程启动
            Control.CheckForIllegalCrossThreadCalls = false;
            Thread lsThread = new Thread(new ThreadStart(T1));
            lsThread.IsBackground = true;
            lsThread.Start();

        }

        private void T1()
        {

        }

按设定的时间点启动功能:

//app.config配置文件中加入定时启动时间点
  <appSettings>
    <add key="t1" value="1:01"/>
  </appSettings>


//取定时作业时间配置到UI
            foreach(string key in ConfigurationManager.AppSettings.AllKeys)            
            {
                string value = ConfigurationManager.AppSettings[key];
                listBox2.Items.Add(value);                
            }

 
//线程,为定时作业用
            Control.CheckForIllegalCrossThreadCalls = false;
            Thread m1Thread = new Thread(new ThreadStart(m1));
            m1Thread.IsBackground = true;
            m1Thread.Start();


 private void m1()
        {
            while (true)
            {
                Thread.Sleep(10 * 1000);
                string now = System.DateTime.Now.ToShortTimeString();     //得到现在的时间                 

                foreach (var tp in listBox2.Items)  //时间点格式 01:12
                {
                    if (now.Equals(tp.ToString()))
                    {
                        Thread1();
                        Thread.Sleep(1000 * 60);//延迟一分钟,避免一分钟内发多次
                    }
                }
            }
        }

EF变量传递处理NULL值:

 //新增对象
 QAS0027WS.dt_jk0027_reqDATA data = new QAS0027WS.dt_jk0027_reqDATA();
 data.AENNR = ss(sheet.Cells[i, "D"].Value2);

        public static int ii(object x1)
        {
            return Convert.ToInt32(x1);
        }


        public static string ss(object x1)
        {
            return Convert.ToString(x1);
        }

        public static Decimal dd(object x1)
        {
            return Convert.ToDecimal(x1);
        }

        public static DateTime tt(object x1)
        {

            if (ss(x1).Equals("0000-00-00")) { x1 = null; }
            return Convert.ToDateTime(x1);
        }

        public static TimeSpan t3(object x)
        {

            if (ss(x).Equals("00:00:00")) { x = null; }
            return TimeSpan.Parse(x.ToString());
        }

        public static DateTime t2(object d, object t)
        {

            if (ss(d).Equals("0000-00-00")) { d = null; }
            if (ss(t).Equals("00:00:00")) { t = null; }

            return Convert.ToDateTime(d.ToString() + " " + t.ToString());
        }

抛出异常:

 try
 {
    XXXXX
 }
 catch (Exception ex)
 {
     ps(listBox1, ex.Message);

 }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘欣的博客

你将成为第一个打赏博主的人!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值