1.保存數據到指定文件
String response = "";//數據 byte[] bytes = response.getBytes();//轉化Byte string filename =「asd」;//本身指定保存的文件名 FileStream fs = new FileStream(@"d:\" + filename + ".txt", FileMode.OpenOrCreate, FileAccess.Write);//本身指定路徑 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));//經過指定字符編碼方式能夠實現對漢字的支持,不然在用記事本打開查看會出現亂碼 sw.Flush(); sw.BaseStream.Seek(0, SeekOrigin.Begin); sw.WriteLine(bytes); sw.Flush(); sw.Close();
2.Invoke.[form.show()]dom
Type type = Type.GetType("[namespace]." + form);ide
if (type != null)字體
{this
MethodInfo method = type.GetMethod("ShowDialog", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { }, null);編碼
object obj = type.Assembly.CreateInstance(type.FullName);spa
method.Invoke(obj, null);日誌
}orm
3.時間換算事件
//這樣對 long 作除法會出偏差(不能整除的時候)ip
public long getTimeInMillis()
{
return Decimal.ToInt64(Decimal.Divide(DateTime.Now.Ticks - 621355968000000000, 10000));
}
//Linux 時間是從 Epoch 開始算的,1970-01-01 00:00:00.//000 叫「Epoch」。
// 使用 DateTime.Now 的時候注意時區問題!//Java 是以 UTC 爲基準的,而經查證,.NET 中與其對應的 是 DateTime.UtcNow 而非 DateTime.Now!
//因此,最後的結論就是:
return Decimal.ToInt64(Decimal.Divide(DateTime.Now.Ticks - new DateTime(1970, 1, 1, 8, 0, 0).Ticks, 10000))
//或者
return Decimal.ToInt64(Decimal.Divide(DateTime.UtcNow.Ticks - 621355968000000000, 10000));
4.隨機顏色
public System.Drawing.Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
// 對於C#的隨機數,沒什麼好說的
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
// 爲了在白色背景上顯示,儘可能生成深色
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return System.Drawing.Color.FromArgb(int_Red, int_Green, int_Blue);
}
5.搞不懂
**對於一些大型的項目,一般由不少個DLL文件組成,引用了這些DLL,就能訪問DLL裏面的類和類裏面的方法。 好比,你寫了一個記錄日誌的DLL,任何項目只要引用此DLL就能實現記錄日誌的功能,這個DLL文件的程序就是一個程序集。 若是你記錄日誌的程序集是這麼定義的 namespace LogerHelper { internal class aa{ public void bb(){ return ""; } } public class Write{ public void WriteIn(string content){ class x = new aa(); x.bb();}}} 當另外一個項目引用了此DLL 它能夠這麼訪問 LogerHelper.Write x = new LogerHelper.Write(); x.WriteIn(""); 但不能夠這麼訪問 LogerHelper.aa x = new LogerHelper.aa(); x.bb(); 這就叫,只能在程序集中訪問
6.枚舉+隨機
class Program{
static void Main(string[] args){
Color[] colors = Enum.GetValues(typeof(Color)) as Color[];
Random random = new Random();
Color color = colors[random.Next(0, colors.Length)];}}
internal enum Color{
White,Black,Red,Green,Pink}
7.threadsleep
using System.Threading; //導入命名空間,類Thread就在此空間中
Thread.Sleep(10000);
8.系統字體顏色字體大小
if (MessageBox.Show(" ?", "提示", MessageBoxButtons.YesNo) ==DialogResult.Yes) {}
FontFamily獲取:
//前臺有個familyList(DropDownList控件)
for(int i=0;i<FontFamily.Families.Length;i++)
{
familyList.Items.Add(FontFamily.Families[i].Name);
}
//InstalledFontCollection
InstalledFontCollection ifc=new InstalledFontCollection();
foreach(FontFamily ff in ifc.Families)
{
familyList2.Items.Add(ff.Name);
}
獲取系統已安裝的顏色:
//System.Drawing.KnownColor
string[] colors=Enum.GetNames(typeof(System.Drawing.KnownColor);
foreach(string color in colors)
{
ListItem list=new ListItem(color);
list.Attributes.Add("style","color:"+color);
colorList.Items.Add(list);
}
獲取字體大小:
//System.Web.UI.WebControls.FontSize
string[] sizes=Enum.GetName(typeof(System.Web.UI.WebControls.FontSize));
foreach(string size in sizes)
{
sizeList.Items.Add(size);
}
9.快捷鍵
1、 C# button快捷鍵之第一種:Alt + *(按鈕快捷鍵)
在你們給button、label、menuStrip等控件設置Text屬性時在名字後邊加&鍵名就能夠了,好比button1.text= "肯定(&O)"。就會有快捷鍵了,這時候按Alt+O就能夠執行按鈕單擊事件。
2、C# button快捷鍵之第二種:Ctrl+*及其餘組合鍵
在WinForm中設置要使用組合鍵的窗體的KeyPreview(向窗體註冊鍵盤事件)屬性爲True;
而後使用窗體的KeyDown事件(在首次按下某個鍵時發生).
C# button快捷鍵實例代碼以下:
private void ***_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F && e.Control)
{
button1.PerformClick(); //執行單擊button1的動做
}
}
此處注意:
一、***表明窗體名稱,看一下 」Keys」的枚舉參數,以實現本身的須要
二、還有一個問題,當使用Ctrl + *快捷鍵時,對於焦點在可寫的控件(如TextBox)上時,可能會將* 鍵值同時輸入,則須要加另外一句話將Handled設置爲true,以取消 KeyPress 事件。即:
private void ***_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F && e.Control)
{
e.Handled = true; //將Handled設置爲true,指示已經處理過KeyPress事件
button1.PerformClick();
}
}
3、C# button快捷鍵之第三種方法
仍是以button爲例。給form添加一個contextMenuStrip1,將其邦定到button上,假設爲button1。給 contextMenuStrip1添加一個item,而後爲它設置快捷鍵(就是你想加在button上的快捷鍵),而且將它的Visible屬性設爲 false。這樣,C# button快捷鍵設置成功。
4、C# button快捷鍵之第四種方法
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Escape))
{
this.Close();
}
return base.ProcessCmdKey(ref msg, keyData);
}
10.DataTable Set PrimaryKey:
if (dt.PrimaryKey == null || dt.PrimaryKey.Length == 0)
{
dt.PrimaryKey = new DataColumn[] { dt.Columns["PK_ColunmeName"] };
}
DataRow myDataRow= myDataSet.Tables["TableName"].Rows.Find("primary key data");
11.Find the ContextMenuStrip(Contrl) ‘s ParentControl
stringcontrol_name= (senderasContextMenuStrip).SourceControl.Name;