一些知识点

关闭程序用哪个方法好

This.close();只关闭当前窗口,不关非主线程

Application.exit();关闭所有窗体,不关非主线程

Application.exitthread();不关非主线程

System.environment.thread();关闭一切


遍历所有控件并找到textbox

 foreach (Control cur in Controls)//遍历所有控件找出textbox加换行
            {
                if (cur is TextBox && cur.Text.Length <=2)
                {
                    cur.Text = "";
                    
                }
                else if (cur is TextBox && cur.Text.Length > 5)
                    cur.Text ="    "+ cur.Text + "\n";
            }



button随机跳动不让鼠标碰到

private void button3_MouseEnter(object sender, EventArgs e)
        {
            int x = this.ClientSize.Width - button3.Width;
            int y = this.ClientSize.Height - button3.Height;
            Random r = new Random();
            button3.Location = new Point(r.Next(0, x + 1), r.Next(0, y + 1));
        }


引用mysqlclient连接mysql数据库并把查询放到dgv里(查)

ps:DataSet与DataAdapter对象读取数据前不需要手动写代码连接数据库,会自动识别,若数据库连接没开,则开启,如果没有关闭,则自动关闭。

            string strconn = "server=localhost;Database=world;port=3306;User Id=root;Password=pass";
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(strconn);
            conn.Open();
            string sql = "select* from city";
            //MySqlCommand comm = new MySqlCommand(sql, conn);
            MySqlDataAdapter ad = new MySqlDataAdapter(sql, conn);
            DataTable ds = new DataTable();
            ad.Fill(ds);
            dataGridView1.DataSource = ds;
            conn.Close();conn.Dispose();

从datatable改变数据update到mysql里

            MySqlDataAdapter ad = new MySqlDataAdapter(sql, conn);
            MySqlCommandBuilder cb = new MySqlCommandBuilder(ad);
            ds.Tables["cityy"].Rows[0]["population"] = 1991;
            ad.Update(ds, "cityy");


直接用sql修改DB(增删改)

            MySqlCommand cmd = new MySqlCommand(sql, conn);
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


基本冒泡排序

int[] a = { 1, 3, 2,4,9,6,5,7,22 };
            for(int i=0;i<a.Length-1;i++)
            {
                for (int j = 0; j < a.Length - 1-i; j++)
                {
                    if (a[j] > a[j+1])
                    {
                        a[j] = a[j+1] + a[j];
                        a[j + 1] = a[j] - a[j + 1];
                        a[j] = a[j] - a[j + 1];
                    }
                }
            }
            foreach (var i in a)
            {
                textBox1.Text += i.ToString()+",";
            }


使用图片查看器或画图全屏打开相应jpg图片
private void button_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = @"C:\Users\DELL-KEVIN\Pictures\Camera Roll\ironman.png";
            process.StartInfo.Arguments = "rundll32.exe C://WINDOWS//system32//shimgvw.dll,ImageView_Fullscreen";
            process.StartInfo.UseShellExecute = true;//此项为是否使用Shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true  
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//窗体显示样式
            //System.Diagnostics.Process.Start("mspaint.exe", @"C:\ironman.png");
            process.Start();
            process.Close();//可以不写
        }


把每一笔操作写入日志文件

public void WriteLog(string msg)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";//appdomain在解决方案debug下,当然也可以在别的地方
            if (!Directory.Exists(filePath))//LOG文件夹是否存在,如果不存在
            {
                Directory.CreateDirectory(filePath);//生成文件夹
            }
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";//文本文件名
            try
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("消息:" + msg);//将后跟行结束符的字符串写入文本字符串或流
                    sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//将后跟行结束符的字符串写入文本字符串或流
                    sw.WriteLine("**************************************************");//将后跟行结束符的字符串写入文本字符串或流
                    sw.WriteLine(); //将行结束符的字符串写入文本字符串或流。
                    sw.Flush();//清理当前写入器的所有缓冲区,并使所有缓冲数据写入基础流
                    sw.Close();//关闭当前 StreamWriter 对象和基础流
                    sw.Dispose(); //释放由 TextWriter 对象使用的所有资源。
                }
            }
            catch (IOException e)//出现错误的写入方法
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("异常:" + e.Message);
                    sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
                    sw.WriteLine("**************************************************");
                    sw.WriteLine();
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/weixin_31808811/article/details/79537090