Posts
[VB.NET].NET多語系程式(四) - 已開啟表單的語系切換
For Each ctl As Control In form.Controls ApplyControlResource(ctl, rm) Next Finally rm = Nothing End Try End Sub ''' <summary> ''' 套用資源設定到控制項(含選單與工具列) ''' </summary> ''' <param name="control">The control.</param> ''' <param name="rm">The ComponentResourceManager.</param> ''' <remarks></remarks> Private Shared Sub ApplyControlResource(ByRef control As Control, ByRef rm As ComponentResourceManager) '------------------------------- 'Author: Larry Nung Date:2008/12/11 'Memo: 參數檢查 '------------------------------- If control Is Nothing Then Throw New ArgumentNullException("control") End If If rm Is Nothing Then Throw New ArgumentNullException("rm") End If '------------------------------- 'Author: Larry Nung Date:2008/12/11 'Memo: '------------------------------- rm.
read morePosts
[VB.NET]VB 10.0 Statement Lambdas
'呼叫addLambdas Console.WriteLine(addLambdas(123, 456))</pre></div><p> </p><p>同樣的程式我們也可以明確的定義回傳值型態</p><p> </p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:15d67606-728e-475d-991c-500cc1d0da0d" class="wlWriterEditableSmartContent"><pre class="vb:nocontrols" name="code"> Dim addLambdas = Function(num1, num2) As Integer 也可以明確的指定帶入的參數型態 Dim addLambdas = Function(num1 As Integer, num2 As Integer) As Integer 若有要傳遞參考的需求,也可以在函式參數前加上ByRef Dim addLambdas = Function(ByRef num1 As Integer, num2 As Integer) As Integer Sub LambdasSub Lambdas使用上就跟一般的Lambdas運算式一樣,不同的是Sub Lambdas呼叫後不會有回傳值。就跟副程式一樣是沒有回傳值的,使用上只需把Lambdas運算式的Function關鍵字改為Sub即可。跟副程式的寫法類似。Sub Lambdas跟一般的Lambdas一樣,除了有Multiline lambdas外 ‘宣告addLambdas Dim addLambdas = Sub(num1, num2) Console.WriteLine(num1 + num2) End Sub
read morePosts
[VB.NET]VB 10.0 Auto-Implemented Properties
Property Name As String Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property</pre></div><p> </p><p>透過VB.NET 10.0的Auto-Implemented Properties功能,我們可以將屬性的撰寫給簡化。像是上面的例子可簡化為像下面這樣:</p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:636f2abe-f420-46bd-a9e4-5ced6d93ac70" class="wlWriterEditableSmartContent"><pre class="vb:nocontrols" name="code"> Property Name As String 短短的一行就可以取代本來冗長的程式,是不是很方便呢?除此之外,Auto-Implemented Properties也可以利用變數初始器來設定屬性的預設值。就像: Property Name As String = “Larry” 若是使用較為複雜的型別也可以 Property SupplierList() As New List(Of Supplier) Property OrderList() As New List(Of Order) With {.Capacity = 100} 也可以用在介面屬性的實作上 Property Name() As String Implements ICustomer.
read morePosts
[C#]C# 4.0 動態繫結 (Dynamic Lookup)
namespace ConsoleApplication16 { class Program { static void Main(string[] args) { dynamic[] ds = new dynamic[2]; ds[0] = new System.Windows.Forms.TextBox(){Name=“TextBox”}; ds[1] = new Person() { Name = “Larry” }; foreach (dynamic d in ds) { Console.WriteLine(d.Name); } } } class Person { public string Name { get; set; } } } 由於不用把型態從Object轉回匿名型別,因此動態繫結也可以用在匿名型別上,真的方便許多,看來以後若要偷懶直接傳遞匿名型別也可以了(不建議也不鼓勵濫用)。 using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication19 { class Program { static void Main(string[] args) { dynamic d = new { Name = “Larry” }; Console.
read morePosts
[C#]利用FlowLayoutPanel控制項合併大量圖片
namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK ) { listBox1.Items.Clear(); listBox1.Items.AddRange(openFileDialog1.FileNames); } } private void button2_Click(object sender, EventArgs e) { flowLayoutPanel1.Controls.Clear(); foreach(string file in listBox1.Items){ flowLayoutPanel1.Controls.Add(new PictureBox() { Image = new Bitmap(file), SizeMode = PictureBoxSizeMode.AutoSize, Margin=new Padding(0)}); } } private void button3_Click(object sender, EventArgs e) { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { Bitmap b = new Bitmap(flowLayoutPanel1.
read morePosts
[C#]C# 4.0 選擇性參數 (Optional Parameters)
Sub SayHello(Optional ByVal name As String = "無名氏") Console.WriteLine("Hello~My name is " & name) End Sub</pre> static void SayHello(String name = "無名氏") { Console.WriteLine("Hello~My name is " + name); }</pre> namespace SayHello { class Program { static void Main(string[] args) { SayHello(); SayHello(name:“Larry”); SayHello(“Larry”); } static void SayHello() { Console.WriteLine(“Hi~”); } static void SayHello(object name) { Console.WriteLine(“Hi~My name is” + name.ToString()); } static void SayHello(String name = “無名氏”) { Console.
read more