1.String到Color的转换
Label1.BackColor  = System.Drawing.Color.FromName(“Red”);

2.调用可执行文件
(1)using System.Diagnostics;//引入命名空间
(2)Process.Start(@"D:\Program Files\Tencent\QQ\QQ.exe");//在方
法中写调用exe可执地程序
3.
在多线程中使用控件时出错解决方法
在窗体的构造方法中加入
Control.CheckForIllegalCrossThreadCalls = false;
4.文本框只能输入数字(非正则表达式)
在文本框的KeyPress事件中
if(e.KeyChar<48 && e.KeyChar>57)
{
e.Handled = true ;
}
5.WinForm中子窗体只能显示一次
Form1 form1;//在类中写窗体类的引用
//在调用窗体的方法中写以下代码
if (form1 == null || form1.IsDisposed)
            {
                form1 = new Form1();
                form1.Show();
            }
            else
            {
                form1.Activate();
            }
6.带参数的多线程使用
  Thread th = new Thread(new ParameterizedThreadStart(Ts));
  th.Start(3);//参数与在Start方法中
  private void Ts(object  i)
  {
       Console.WriteLine(i);
  }
 
7.将DataGridView导出为Excel
首先添加引用 Microsoft.Office.Interop.Excel;
然后引入命名空间using Excel = Microsoft.Office.Interop.Excel;
private void GoExcel(DataGridView Dg)
        {
            Excel.Application myExcel = new Excel.Application();
            myExcel.Application.Workbooks.Add(true);
            myExcel.Visible = true;
            for (int i = 0; i < Dg.Rows.Count-1; i++)
            {
                for (int j = 0; j < Dg.Rows[i].Cells.Count; j++)
                {
                    myExcel.Cells[i + 1, j+1] = Dg.Rows[i].Cells[j].Value.ToString();
                }
            }
           
        }
 
8.用pictureBox1获取网络上的图片
首先引入命名空间
using System.Net;
using System.IO;
在方法下写如下代码:
Stream str = WebRequest.Create(@"http://www.baidu.com/img/logo-yy.gif").GetResponse().GetResponseStream();
pictureBox1.Image = Image.FromStream(str);
 
9.让Text文本框的多行滚动条一直在最下边
textBox1.Text += "aaaaaaaaaaaaaa33aaaaaaaaaaaaaaa";
            textBox1.SelectionStart = textBox1.Text.Length;
            textBox1.ScrollToCaret();
0
0
(请您对文章做出评价)