c#小技巧

來源:互聯網
上載者:User

1.將一個Form顯示在另一個Form裡面
添加一個Panel控制項到FormA中,設定FormB的TopLevel屬性為false

FormB.TopLevel = false;
FormA.Panel1.Controls.Add(FormB);
FormB.Show();

2.求一串混合字元的長度(中英文相間的)

string str = "hello world 時間你好!";
int len = Encoding.GetEncoding("GB18030").GetBytes(str).Length;

3.使用TextRender和Graphics.DrawString來繪製文本的區別
TextRender是使用GDI介面來完成映像繪製,而Graphics則是使用GDI+來完成映像繪製,顯然GDI+的效率要比GDI高!
兩則都可以實現換行繪製文本在特定的Rectangle中。對於Graphics的DrawString在繪製文本是自動進行換行處理,而TextRender則要添加一個標識:TextFormatFlags flags = TextFormatFlags.WordBreak。使用Graphics的DrawString繪製文本的另一個優點是你可以使用StringFormat來控制所繪製的文本在Rectangle中的對齊狀態。

// 左上方對齊            
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Near;

而TextRender依然使用了TextFormatFlags來處理對齊

TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;

4. 使用VS2005中內建的Step Project打包.net應用程式
    (1)建立 File -> New -> Project -> Other Project Types -> Setup and Deployment 中的Setup Project項目
    (2)在Application Folder右鍵菜單中Add -> File..., 在開啟的對話方塊中選中你要打包的檔案
    (3)右鍵要執行的程式,Create Shortcut to [應用程式名稱], 然後拉到User's Desktop目錄中,說明一下User's Desktop和User's Programs Menu著
              兩個項目是建立內容到案頭和開始菜單中,是可選的(注意:要查看Application Folder的屬性將AlwaysCreate設定為true)
    (4)右鍵工程選擇Properties->Prerequisites...選擇要包含的項目,如.Net framework2.0。在選擇Download prerequisites from the same location  as
              my application。
    (5)編譯工程就可以了。其中路徑名和開發人員等設定,在項目的屬性中(不是點右鍵後的properties哦)。
這是最簡單的打包一個.net工程的步驟。

5. 在字串中實現換行連結: string str = "abcd \r\n efgh"; 其實還可以使用System.Environment.NewLine來實現。
    string str = "abcd" + System.Environment.NewLine + "efgh";

6. 將十六進位形式的字串轉換到數值形式的方法,這裡以32位整數為例
    int val = Int32.Parse("FF", System.Globalization.NumberStyles.AllowHexSpecifier);

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.