Below you will find pages that utilize the taxonomy term “VB.NET”
Posts
[C#][VB.NET]最大公因數 amp; 最小公倍數
private int LCM(int num1, int num2) { return num1 * num2 / GCD(num1, num2); }</pre></div> Private Function LCM(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Return num1 * num2 / GCD(num1, num2) End Function</pre></div> namespace ConsoleApplication1 { class Program { private static int GCD(int num1, int num2) { int min = 0; int max = 0; int maxModMin = 0; min = Math.Min(num1, num2); max = Math.Max(num1, num2); maxModMin = max % min; return maxModMin > 0 ?
read morePosts
PermissionController權限管理類別
#Region “Var” Private _allPermission As T Private _permission As T #End Region
#Region “Public Property” Public Property AllPermission() As T Get Return _allPermission End Get Private Set(ByVal value As T) _allPermission = value End Set End Property
''' <summary> ''' Gets the permission. ''' </summary> ''' <value>The permission.</value> Public Property Permission() As T Get Return _permission End Get Private Set(ByVal value As T) _permission = value End Set End Property #End Region
read morePosts
.NET 4.0 New Feature - Memory Mapped File
.NET Framework 在 4.0 新增了記憶體對應檔案(Memory Mapped File)功能,將以前需透過 API 才能使用的功能包在 .NET Framework 的 System.IO.MemoryMappedFiles 命名空間中,可用以編輯大小極大的檔案、減少IO的存取次數、提高檔案處理的效能、與在多個處理序中共享其內容。
要使用記憶體對應檔案物件,首先要先建立記憶體對應檔案物件,須了解到記憶體對應檔案有兩種類型;
保存的記憶體對應檔案 非保存的記憶體對應檔案 保存的記憶體對應檔案顧名思義其記憶體對應檔案的內容會與磁碟中的檔案做對應,可將檔案內容放至記憶體對應檔案中用以處理極大的檔案,當處理完畢資料會自動回存回對應檔案。
非保存的記憶體對應檔案則沒有與磁碟中的檔案對應,其所存放的值只是記憶體中的資料,可用來作處理序間通訊(IPC)的共用記憶體,當處理完畢其記憶體對應檔案的資料會被捨棄,且會被GC回收。
使用上是透過 MemoryMappedFile 類別內含的 CreateNew、 CreateOrOpen、與 CreateFromFile 等方法來建立記憶體對應檔案物件。
Method Description CreateNew 建立非保存的記憶體對應檔案物件 CreateOrOpen 建立或開啟非保存的記憶體對應檔案物件 CreateFromFile 建立保存的記憶體對應檔案物件 若是有已建好的記憶體對應檔案,也可以透過 MemoryMappedFile 類別內含的 OpenExisting 方法將已經建立好的記憶體對應檔案物件開啟。
有了記憶體對應檔案物件後,剩下的就是透過記憶體對應檔案檢視去對其作存取的動作。一個記憶體對應檔案物件可產生多個記憶體對應檔案檢視,可設定要針對記憶體對應檔案物件整體或是部份來檢視,需特別注意到記憶體對應檔案檢視也分為兩種類型:
資料流存取檢視 隨機存取檢視 資料流存取檢視透過 MemoryMappedFile.CreateViewStream 建立,採循序存取的方式處理資料,適用於非保存的記憶體對應檔案。
隨機存取檢視透過 MemoryMappedFile.CreateViewAccessor 建立,採隨機存取的方式處理資料,適用於保存的記憶體對應檔案。
這邊來看個非保存的記憶體對應檔案物件的使用範例:
Imports System.IO.MemoryMappedFiles Imports System.Collections.Specialized Imports System.Runtime.Serialization.Formatters.Binary Module Module1 Sub Main() Const capacity As Integer = 512 Const mmfKey As String = "Larry" Dim levelUpBlog As New Blog With {.
read morePosts
使用Extension Method計算漢字筆畫
public static int GetStrokesNumber(this char c) { String hex = BitConverter.ToString(Encoding.GetEncoding("Big5").GetBytes(new char[] { c })).Replace("-", string.Empty); for (int i = 0; i < _strokesNumberData.Length; ++i) { for (int j = 0; j < _strokesNumberData[i].Length; j += 2) { if (hex.CompareTo(_strokesNumberData[i][j]) >= 0 && hex.CompareTo(_strokesNumberData[i][j + 1]) <= 0) return i+1; } } return 0; } }</pre></div> Module CharExtension
Private _strokesNumberData As String()() = { _ New String() {"A440", "A441"}, _ New String() {"A442", "A453", "C940", "C944"}, _ New String() {"A454", "A47E", "C945", "C94C"}, _ New String() {"A4A1", "A4FD", "C94D", "C95C"}, _ New String() {"A4FE", "A5DF", "C95D", "C9AA"}, _ New String() {"A5E0", "A6E9", "C9AB", "C959"}, _ New String() {"A6EA", "A8C2", "CA5A", "CBB0"}, _ New String() {"A8C3", "AB44", "CBB1", "CDDC"}, _ New String() {"AB45", "ADBB", "CDDD", "D0C7", "F9DA", "F9DA"}, _ New String() {"ADBC", "B0AD", "D0C8", "D44A"}, _ New String() {"B0AE", "B3C2", "D44B", "D850"}, _ New String() {"B3C3", "B6C3", "D851", "DCB0", "F9DB", "F9DB"}, _ New String() {"B6C4", "B9AB", "DCB1", "E0EF", "F9D6", "F9D8"}, _ New String() {"B9AC", "BBF4", "E0F0", "E4E5"}, _ New String() {"BBF5", "BEA6", "E4E6", "E8F3", "F9DC", "F9DC"}, _ New String() {"BEA7", "C074", "E8F4", "ECB8", "F9D9", "F9D9"}, _ New String() {"C075", "C24E", "ECB9", "EFB6"}, _ New String() {"C24F", "C35E", "EFB7", "F1EA"}, _ New String() {"C35F", "C454", "F1EB", "F3FC"}, _ New String() {"C455", "C4D6", "F3FD", "F5BF"}, _ New String() {"C3D7", "C56A", "F5C0", "F6D5"}, _ New String() {"C56B", "C5C7", "F6D6", "F7CF"}, _ New String() {"C5C8", "C5C7", "F6D6", "F7CF"}, _ New String() {"C5F1", "C654", "F8A5", "F8ED"}, _ New String() {"C655", "C664", "F8E9", "F96A"}, _ New String() {"C665", "C66B", "F96B", "F9A1"}, _ New String() {"C66C", "C675", "F9A2", "F9B9"}, _ New String() {"C676", "C67A", "F9BA", "F9C5"}, _ New String() {"C67B", "C67E", "F9C6", "F9DC"}} <Extension()> _ Public Function GetStrokesNumber(ByVal c As Char) As Integer Dim hex As [String] = BitConverter.
read morePosts
[VB.NET]用Extension Method移除控制項
Public Module ControlExtension
#Region “Public Method” <Extension()> _ Public Sub Remove(ByVal ctrl As Control) Dim parent As Control = ctrl.Parent If parent Is Nothing Then Return End If parent.Controls.Remove(ctrl) End Sub #End Region
End Module
read morePosts
.NET 4.0 New Feature - Complex
Sub Main() ShowDetail("Complex.Zero", Complex.Zero) ShowDetail("Complex.One", Complex.One) Dim c1 As New Complex(1, 2) Dim c2 As New Complex(3, 4) ShowDetail("C1", c1) ShowDetail("C2", c2) 'Complex +-*/ Console.WriteLine("{0} + {1} = {2}", c1, c2, c1 + c2) Console.WriteLine("{0} - {1} = {2}", c1, c2, c1 - c2) Console.WriteLine("{0} * {1} = {2}", c1, c2, c1 * c2) Console.WriteLine("{0} / {1} = {2}", c1, c2, c1 / c2) Console.WriteLine() Console.WriteLine("Complex.Add({0}, {1}) = {2}", c1, c2, Complex.
read morePosts
.NET 4.0 New Feature - StringBuilder.Clear
Module Module1
Sub Main() Dim str As New StringBuilder Dim size As Integer = 100000000 Dim sw As New Stopwatch '預先編譯降低測試誤差 AppendData(str, size) ClearData1(str) ClearData2(str) ClearData3(str) '開始測試 AppendData(str, size) Console.WriteLine("Test Remove...") sw.Restart() ClearData1(str) Console.WriteLine(sw.ElapsedMilliseconds) AppendData(str, size) Console.WriteLine("Test Length...") sw.Restart() ClearData2(str) Console.WriteLine(sw.ElapsedMilliseconds) AppendData(str, size) Console.WriteLine("Test Clear...") sw.Restart() ClearData3(str) Console.WriteLine(sw.ElapsedMilliseconds) End Sub Private Sub AppendData(ByVal str As StringBuilder, ByVal size As Integer) str.Append(New String("0", size)) End Sub Private Sub ClearData1(ByVal str As StringBuilder) str.
read morePosts
.NET 4.0 New Feature - Enum.HasFlag
… Dim myOrder As DinnerItems = DinnerItems.Appetizer Or DinnerItems.Entree Or DinnerItems.Beverage Or DinnerItems.Dessert Dim hasFlag As Boolean = myOrder.HasFlag(DinnerItems.Entree Or DinnerItems.Beverage) …
read morePosts
[VB.NET]用Extension Method取得CustomAttributes
Module EnumExtension
<Extension()> _ Function GetCustomAttributes(Of T)(ByVal e As [Enum]) As IEnumerable(Of T) Return e.GetType.GetField(e.ToString).GetCustomAttributes(GetType(T), False).Cast(Of T)() End Function <Extension()> _ Function GetCustomAttribute(Of T)(ByVal e As [Enum]) As T Return GetCustomAttributes(Of T)(e).FirstOrDefault End Function End Module
Module TypeExtension
<Extension()> _ Function GetCustomAttributes(Of T)(ByVal type As Type) As IEnumerable(Of T) Return type.GetCustomAttributes(GetType(T), False).Cast(Of T)() End Function <Extension()> _ Function GetCustomAttribute(Of T)(ByVal type As Type) As T Return DirectCast(GetCustomAttributes(Of T)(type).FirstOrDefault, T) End Function <Extension()> _ Function GetCustomAttributes(Of T)(ByVal type As Type, ByVal memberName As String) As IEnumerable(Of T) Dim m = type.
read morePosts
[VB.NET]Attribute與反射的搭配使用
Property Data as Object End Class
Property RelativedType As Type Sub New(ByVal relativedType As Type) Me.RelativedType = relativedType End Sub End Class
Enum ApplicationType <RelativedType(GetType(LevelUp.Office.Word))> _ Word
<RelativedType(GetType(LevelUp.Office.Excel))> _ Excel <RelativedType(GetType(LevelUp.Office.Access))> _ Access <RelativedType(GetType(LevelUp.Office.PowerPoint))> _ PowerPoint <RelativedType(GetType(LevelUp.Office.Visio))> _ Visio End Enum
Sub StartApplication(ByVal apType As ApplicationType) Dim type As Type = apType.GetType() Dim field = type.GetField(apType.ToString) Dim att = field.GetCustomAttributes(GetType(RelativedTypeAttribute), False).Cast(Of RelativedTypeAttribute)().FirstOrDefault() Activator.CreateInstance(att.RelativedType).Start() End Sub</pre></div> Property ResourceID As String Sub New(ByVal resourceID As String) Me.
read morePosts
[VB.NET]Merge MDI ToolStrip
ReadOnly Property MainToolStrip() As ToolStrip End Interface 子視窗實做IChildForm介面,把子視窗的工具列開出。 Public Class Form1 Implements IChildForm
Public ReadOnly Property MainToolStrip() As System.Windows.Forms.ToolStrip Implements IChildForm.MainToolStrip Get Return ToolStrip1 End Get End Property End Class 在父視窗中處理MDIChildActivate事件,將工具列合併。 Public Class MDIParent1 Protected Overrides Sub OnMdiChildActivate(ByVal e As System.EventArgs) MyBase.OnMdiChildActivate(e) ToolStripManager.RevertMerge(ToolStrip) Dim childForm As IChildForm = CType(ActiveMdiChild, IChildForm) If childForm IsNot Nothing Then ToolStripManager.Merge(childForm.MainToolStrip, ToolStrip) End If End Sub End Class DownloadMergeMDIToolStrip.zip
read morePosts
[VB.NET]MDI Thumbnail Preview
Public Class ThumbnailDialog
Private _mdiParent As Form Private _thumbnailPool As Dictionary(Of Form, Image) Private Property m_MDIParent As Form Get Return _mdiParent End Get Set(ByVal value As Form) _mdiParent = value End Set End Property Private ReadOnly Property m_ThumbnailPool As Dictionary(Of Form, Image) Get If _thumbnailPool Is Nothing Then _thumbnailPool = New Dictionary(Of Form, Image) End If Return _thumbnailPool End Get End Property Public ReadOnly Property SelectedChildForm As Form Get Return If(ListBox1.
read morePosts
用Extension Method在執行階段進行控制項的拖曳
Public Module ControlExtension
#Region “Var” Private _runtimeDragClickPoint As Point #End Region
#Region “Private Property” Private Property m_RuntimeDragClickPoint() As Point Get Return _runtimeDragClickPoint End Get Set(ByVal value As Point) If _runtimeDragClickPoint <> value Then _runtimeDragClickPoint = value End If End Set End Property #End Region
#Region “Public Method” <Extension()> _ Public Sub EnableRuntimeDrag(ByVal ctrl As Control) DisableRuntimeDrag(ctrl) AddHandler ctrl.MouseDown, AddressOf ctrl_MouseDown AddHandler ctrl.MouseMove, AddressOf ctrl_MouseMove AddHandler ctrl.HandleDestroyed, AddressOf ctrl_HandleDestroyed End Sub
read morePosts
[VB.NET]利用反射載入並運行資源檔中的組件
Module MainModule
<STAThread()> _ Public Sub main(ByVal args() As String) Assembly.Load(My.Resources.ColorPicker).EntryPoint.Invoke(Nothing, New Object() {args}) End Sub End Module
同樣的概念,其實放在資源檔中的組件可以先經過壓縮處理,再放入資源檔中。載入時只要先經過解壓縮的動作,把要執行的組件從壓縮中解出,就可以執行了。 Imports System.IO Imports System.Reflection Imports ICSharpCode.SharpZipLib.Zip
Module MainModule
<STAThread()> _ Public Sub main(ByVal args() As String) Using compressedMS As New MemoryStream(My.Resources.ColorPicker) Dim zf As New ZipFile(compressedMS) Dim ze As ZipEntry = zf.GetEntry("ColorPicker.exe") Dim zs As Stream = zf.GetInputStream(ze) Dim buffer(ze.Size - 1) As Byte zs.Read(buffer, 0, buffer.Length - 1) Assembly.Load(buffer).EntryPoint.Invoke(Nothing, New Object() {args}) End Using End Sub End Module 那若是放在資源檔中的組件需要參考多個組件呢?
read morePosts
[VB.NET]MDI子視窗清單的實作
Private Sub WindowsMenu_DropDownOpening(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WindowsMenu.DropDownOpening If ActiveMdiChild Is Nothing Then Return End If Dim sp As New ToolStripSeparator sp.Name = "MdiWindowListSeperator" WindowsMenu.DropDownItems.Add(sp) For Each f As Form In MdiChildren If Not f.Visible Then Continue For End If WindowsMenu.DropDownItems.Add(New ToolStripMenuItem(f.Text, Nothing, AddressOf MDIChildren_Click) With {.Checked = ActiveMdiChild Is f, .Tag = f}) Next End Sub Private Sub MDIChildren_Click(ByVal sender As Object, ByVal e As EventArgs) DirectCast(DirectCast(sender, ToolStripMenuItem).
read morePosts
[VB.NET]使用Win32 API擷取滑鼠游標位置的顏色
Private Function GetColor() As Color Return GetColor(Cursor.Position) End Function Private Function GetColor(ByVal point As Point) As Color Return GetColor(point.X, point.Y) End Function Private Function GetColor(ByVal x As Integer, ByVal y As Integer) As Color Dim hdc As IntPtr hdc = GetDC(IntPtr.Zero) '取該Handle值的DC GetColor = ColorTranslator.FromWin32(GetPixel(hdc, x, y)) ReleaseDC(IntPtr.Zero, hdc) '將DC 釋放 End Function</pre></div> Public Class Form1 <DllImport(“User32.dll”, EntryPoint:=“GetDC”, _ CallingConvention:=CallingConvention.StdCall, _ CharSet:=CharSet.Auto, exactspelling:=True)> _ Public Shared Function GetDC(ByVal hwnd As IntPtr) As IntPtr End Function Public Declare Function GetPixel Lib “gdi32” Alias “GetPixel” (ByVal hdc As IntPtr, ByVal X As Int32, ByVal Y As Int32) As Int32 <DllImport(“user32.
read morePosts
[VB.NET]使用mouse_event API 來操控滑鼠動作
<td valign="top" width="404">說明</td> </tr> <tr> <td valign="top" width="91"><em>dwFlags</em></td> <td valign="top" width="404">指示滑鼠動作</td> </tr> <tr> <td valign="top" width="91">dx</td> <td valign="top" width="404">x座標 (dwFlags有設MOUSEEVENTF_ABSOLUTE時,該座標為絕對座標)</td> </tr> <tr> <td valign="top" width="91">dy</td> <td valign="top" width="404">y座標 (dwFlags有設MOUSEEVENTF_ABSOLUTE時,該座標為絕對座標)</td> </tr> <tr> <td valign="top" width="91">dwData</td> <td valign="top" width="404">dwFlags為MOUSEEVENTF_HWHEEL時,該值代表捲軸捲動的量。 <br /> <br />dwFlags為MOUSEEVENTF_XDOWN或MOUSEEVENTF_XUP時,該值可為XBUTTON1 (&H0001)或XBUTTON2 (&H0002)。 <br /> <br />當dwFlags不為MOUSEEVENTF_HWHEEL、 <br />MOUSEEVENTF_XDOWN或MOUSEEVENTF_XUP,該值為0。</td> </tr> <tr> <td valign="top" width="91">dwExtraInfo</td> <td valign="top" width="404">An additional value associated with the mouse event. An application calls <strong>GetMessageExtraInfo</strong> to obtain this extra information.
read morePosts
[VB.NET]將集合類別繫結至DataGridView並使其具備新增功能
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim lst As New List(Of Person) lst.Add(New Person With {.Name = "Larry"}) DataGridView1.DataSource = lst End Sub End Class
Class Person Dim _name As String Property Name() As String Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.
read morePosts
[VB.NET]SecureString較為安全的加密字串類別
Public Sub PrintSecureString(ByVal secureString As SecureString) ... End Sub</pre></div> Module Module1
Sub Main() Console.WriteLine("Please enter your password to be encrypted:") Dim password As SecureString = ReadPassword() Console.WriteLine() Console.WriteLine("Decripted password:") PrintPassword(password) End Sub Public Function ReadPassword() As SecureString Dim password As New SecureString() Dim nextKey As ConsoleKeyInfo = Console.ReadKey(True) While nextKey.Key <> ConsoleKey.Enter If nextKey.Key = ConsoleKey.Backspace Then If password.Length > 0 Then password.RemoveAt(password.Length - 1) ' erase the last * as well Console.
read morePosts
[VB.NET]Keys lt;=gt; Char
Module Module1
Sub Main() Dim key As Keys = Keys.A Dim keyChar As Char = Convert.ToChar(key) Console.WriteLine(Convert.ToChar(key)) Console.WriteLine(Asc(keyChar)) Console.WriteLine(Microsoft.VisualBasic.ChrW(key)) End Sub End Module
read morePosts
[Extension Method][VB.NET]ErrorProvider.GerErrorMsgs amp; ErrorProvider.HasError
Public Module ErrorProviderExtension
<Extension()> _ Public Function GetErrorMsgs(ByVal ep As ErrorProvider) As String() If ep.ContainerControl Is Nothing Then Return New String() {} End If Dim linq = From c In ep.ContainerControl.Controls Let msg = ep.GetError(c) Where msg.Length > 0 Select msg Return linq.ToArray End Function <Extension()> _ Public Function HasError(ByVal ep As ErrorProvider) As Boolean Return ep.GetErrorMsgs.Length > 0 End Function End Module
read morePosts
[VB.NET]密碼框顯示程式探討與其簡易的保護之道
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean End Function
Const EM_SETPASSWORDCHAR = &HCC Private Sub TargetSelectedControl1_TargetSelected(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TargetSelectedControl1.TargetSelected Dim hWnd As IntPtr = WindowFromPoint(MousePosition) PostMessage(hWnd, EM_SETPASSWORDCHAR, 0, 0) End Sub</pre></div> Const WM_SETTEXT As Integer = &HC Const WM_GETTEXT As Integer = &HD Const EM_SETPASSWORDCHAR = &HCC Sub New() With Me .
read morePosts
[VB.NET]自定義.NET WindowForm表單介面(三)
<DllImport("user32.dll", SetLastError:=True)> _ Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer End Function
Public Const WS_SYSMENU As Integer = &H80000 Const WS_MINIMIZEBOX As Integer = &H20000 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim winLong As Integer = GetWindowLong(New HandleRef(Me, Me.Handle), -16) SetWindowLong(New HandleRef(Me, Me.Handle), -16, winLong Or WS_SYSMENU Or WS_MINIMIZEBOX) End Sub</pre></div>
read morePosts
[Linq]Linq程式逐步執行與偵錯
foreach (var item in linq) { Console.WriteLine(item); } }</pre></div> For Each item In linq Console.WriteLine(item) Next End Sub</pre>
read morePosts
[Extension Method][VB.NET]使用擴充方法過濾出組件內的特定類別
Module AssembleExtension <Extension()> _ Public Function GetTypes(ByVal asm As Assembly, ByVal filterTypeName As String, Optional ByVal includeSubClass As Boolean = False) As Type() Return GetTypes(asm, Type.GetType(filterTypeName), includeSubClass) End Function
<Extension()> _ Public Function GetTypes(ByVal asm As Assembly, ByVal filterType As Type, Optional ByVal includeSubClass As Boolean = False) As Type() Dim linq = From t In asm.GetTypes Where t Is filterType OrElse (includeSubClass AndAlso t.IsSubclassOf(filterType)) Select t Return linq.ToArray End Function End Module
read morePosts
[VB.NET]比對兩個目錄中不同的檔案
Module DirectoryInfoExtension <Extension()> _ Function GetDifferentFileList(ByVal sourceDirectoryInfo As DirectoryInfo, ByVal targetDirectoryInfo As DirectoryInfo, Optional ByVal searchPattern As String = “.”, Optional ByVal searchOption As SearchOption = SearchOption.TopDirectoryOnly) As FileInfo() If sourceDirectoryInfo Is Nothing Then Throw New ArgumentNullException(“sourceDirectoryInfo”) End If If targetDirectoryInfo Is Nothing Then Throw New ArgumentNullException(“targetDirectoryInfo”) End If If targetDirectoryInfo.FullName = sourceDirectoryInfo.FullName Then Return New FileInfo() {} End If Dim sourceFiles = sourceDirectoryInfo.GetFiles(searchPattern, searchOption) Dim destFiles = targetDirectoryInfo.GetFiles(searchPattern, searchOption) Dim intersectFiles = sourceFiles.
read morePosts
[Extension Method]使用擴充方法來做物件的深層複製
Public Module ObjectExtension
#Region “Const” Const BUFFER_SIZE As Integer = 512 #End Region
#Region “Public Method”
<Extension()> _ Public Function Clone(Of T)(ByVal obj As T, Optional ByVal serializeType As SerializationFormat = SerializationFormat.Binary) As T Select Case serializeType Case SerializationFormat.Binary Dim br As New BinaryFormatter() Using ms As New MemoryStream(BUFFER_SIZE) br.Serialize(ms, obj) ms.Seek(0, SeekOrigin.Begin) Return DirectCast(br.Deserialize(ms), T) End Using Case SerializationFormat.Xml Dim x As New XmlSerializer(obj.GetType) Using ms As New MemoryStream(BUFFER_SIZE) x.
read morePosts
[Extension Method]使用擴充方法來做二維陣列排序
T[] tempArray = new T[count]; for (int idx = 0; idx < count; ++idx) { tempArray[idx] = array[idx, d2Idx]; } Array.Sort(tempArray); for (int idx = 0; idx < count; ++idx) { T tempValue = tempArray[idx]; for (int idx2 = idx+1; idx2 < count; ++idx2) { if (array[idx2, d2Idx].Equals(tempValue)) { array.Swap(idx, d2Idx, idx2, d2Idx); int d2Idx2 = (d2Idx == 0) ? 1 : 0; array.Swap(idx, d2Idx2, idx2, d2Idx2); } } } } public static void Swap<T>(this T[,] array, int idx1, int idx2, int targetIdx1, int targetIdx2) { T temp; temp = array[targetIdx1, targetIdx2]; array[targetIdx1, targetIdx2] = array[idx1, idx2]; array[idx1, idx2] = temp; } 使用上呼叫Sort方法,並傳入要排序依據的索引即可。
read morePosts
[VB.NET]用FindExecutable API取得開啟文件用的執行檔位置
<td valign="top" width="200">指定的檔案不存在</td> </tr> <tr> <td valign="top" width="200">SE_ERR_NOASSOC (31)</td> <td valign="top" width="200">沒有對應用來開啟的執行檔</td> </tr> <tr> <td valign="top" width="200">SE_ERR_OOM (8)</td> <td valign="top" width="200">Windows XP only. 系統記憶體資源不足</td> </tr> Public Class NoAssociatedFileTypeException Inherits ApplicationException
End Class
Public Class Win32API Const SE_ERR_FNF As Integer = 2 Const SE_ERR_OOM As Integer = 8 Const SE_ERR_NOASSOC As Integer = 31
<Runtime.InteropServices.DllImport("shell32.DLL", EntryPoint:="FindExecutable")> _ Private Shared Function FindExecutable(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As StringBuilder) As Integer End Function
read morePosts
[VB.NET]如何自製使用者控制項測試容器(User Control Test Container)
Private Sub SetPreviewControl(ByVal c As Control) With pnlControlContainer.Controls .Clear() .Add(c) End With Me.PropertyGrid1.SelectedObject = c End Sub</pre></div><p>如此簡易的User Control Test Container就完成了。</p><p> </p><p>這邊附上我寫好的範例程式,主要介面仿照User Test Container。除提供User Test Container的功能外,另加上語系切換,與預覽表單的功能。程式介面外觀如下:</p><p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" width="602" height="522" src="\images\posts\13810\image_thumb.png" /></a></p><p> </p> <h2>Download</h2><p><a href="http://Files.Dotblogs.com.tw/larrynung/1002/2010227232727262.zip">User Control Test Container.zip</p>
read morePosts
[VB.NET]調整TreeView或TreeNode下的節點
Private Function GetAllNodes(ByVal treeOrNode As Object) As TreeNode() If Not TypeOf treeOrNode Is TreeNode AndAlso Not TypeOf treeOrNode Is TreeView Then Throw New ArgumentException("Error param type!!") End If Dim nodes As New List(Of TreeNode) If TypeOf treeOrNode Is TreeNode Then nodes.Add(treeOrNode) End If For Each tn As TreeNode In treeOrNode.Nodes nodes.AddRange(GetAllNodes(tn)) Next Return nodes.ToArray End Function</pre></div> Public Module TreeViewExtension
#Region “Private Method” Private Function GetAllTreeNodes(ByVal treeOrNode As Object) As TreeNode() If Not TypeOf treeOrNode Is TreeNode AndAlso Not TypeOf treeOrNode Is TreeView Then Throw New ArgumentException(“Error param type!
read morePosts
[VB.NET]取得TreeView或TreeNode下的樹葉節點
Private Function GetAllNodes(ByVal treeOrNode As Object) As TreeNode() If Not TypeOf treeOrNode Is TreeNode AndAlso Not TypeOf treeOrNode Is TreeView Then Throw New ArgumentException("Error param type!!") End If Dim nodes As New List(Of TreeNode) If TypeOf treeOrNode Is TreeNode Then nodes.Add(treeOrNode) End If For Each tn As TreeNode In treeOrNode.Nodes nodes.AddRange(GetAllNodes(tn)) Next Return nodes.ToArray End Function</pre></div> Public Module TreeViewExtension
#Region “Private Method” Private Function GetAllTreeNodes(ByVal treeOrNode As Object) As TreeNode() If Not TypeOf treeOrNode Is TreeNode AndAlso Not TypeOf treeOrNode Is TreeView Then Throw New ArgumentException(“Error param type!
read morePosts
[VB.NET]取得Gif動畫圖檔內含的圖片
Private Sub btnFileBrowser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileBrowser.Click If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then Me.tbxGifFile.Text = Me.OpenFileDialog1.FileName SplitGifFrame(New Bitmap(Me.tbxGifFile.Text)) End If End Sub Private Sub SplitGifFrame(ByVal gifBmp As Bitmap) Me.flpGifFrames.Controls.Clear() Dim gifFrames() As Bitmap = GetGifFrames(gifBmp) flpGifFrames.SuspendLayout() For Each frame As Bitmap In gifFrames flpGifFrames.Controls.Add(New PictureBox With {.Image = frame}) Next flpGifFrames.ResumeLayout() Me.tsslStatus.Text = My.Computer.FileSystem.GetName(Me.tbxGifFile.Text) & " 內含 " & gifFrames.Count.ToString & " 個圖片" End Sub Private Function GetGifFrames(ByVal gifBmp As Bitmap) As Bitmap() Dim imgFrmDim As Imaging.
read morePosts
[C#][VB.NET]FullScreen the winform
static class FullScreenExtension { #region Struct struct FullScreenData { public FormBorderStyle FormBorderStyle { get; set; } public FormWindowState WindowState { get; set; }
public FullScreenData(FormBorderStyle formBorderStyle, FormWindowState windowState):this() { this.FormBorderStyle = formBorderStyle; this.WindowState = windowState; } } #endregion #region Var private static Dictionary<Form, FullScreenData> _fullScreenDataPool; #endregion #region Private Property private static Dictionary<Form, FullScreenData> m_FullScreenDataPool { get { if (_fullScreenDataPool == null) _fullScreenDataPool = new Dictionary<Form, FullScreenData>(); return _fullScreenDataPool; } } #endregion #region Public Method public static void FullScreen(this Form frm) { if (m_FullScreenDataPool.
read morePosts
[VB.NET]為控制項加上顯示載入動畫的機制
#End Region
Public Module ControlLoadingExtension
#Region “Class” Class LoadingInfo Private _loaddingBmp As Image Private _loaddingBmpActiveFrameIdx As Integer Private _loaddingBmpDimension As FrameDimension Private _loaddingBmpFrameCount As Integer Private _displayLocation As Point
Property LoaddingBmp As Image Get Return _loaddingBmp End Get Set(ByVal value As Image) If _loaddingBmp IsNot value Then _loaddingBmp = value LoaddingBmpDimension = New FrameDimension(value.FrameDimensionsList(0)) LoaddingBmpFrameCount = value.GetFrameCount(LoaddingBmpDimension) LoaddingBmpActiveFrameIdx = 0 End If End Set End Property Property LoaddingBmpActiveFrameIdx As Integer Get Return _loaddingBmpActiveFrameIdx End Get Set(ByVal value As Integer) _loaddingBmpActiveFrameIdx = value LoaddingBmp.
read morePosts
[VB.NET]Change MDI Parent BackColor
Private Sub SetMdiBackColor() For Each c As Control In Me.Controls If TypeOf c Is MdiClient Then c.BackColor = Me.BackColor Exit For End If Next End Sub End Class
Private Sub SetMdiBackColor() Me.Controls(Me.Controls.Count - 1).BackColor = Me.BackColor End Sub End Class
read morePosts
[VB.NET]ASCII String與Hex String的互轉
Public Function AsciiStringToHexString(ByVal asciiString As String) As String Dim ascii() As Byte = System.Text.Encoding.Default.GetBytes(asciiString) Dim count As Integer = ascii.Length Dim hexArray(count - 1) As String For idx As Integer = 0 To count - 1 hexArray(idx) = ascii(idx).ToString("x2") Next Return String.Join(" ", hexArray) End Function Public Function HexStringToAsciiString(ByVal hexString As String) As String Dim array() As String = hexString.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries) For idx As Integer = 0 To array.
read morePosts
[VB.NET]MFC CArchive的讀取
#Region “Config” Private ReadOnly BYTES_COUNT_IN_BOOL As Byte = 4 ‘VC6的BOOL佔 4 Bytes Private ReadOnly BYTES_COUNT_IN_INT As Byte = 4 ‘VC6的Int佔 4 Bytes Private ReadOnly BYTES_COUNT_IN_FLOAT As Byte = 4 ‘VC6的Float佔 4 Bytes Private ReadOnly BYTES_COUNT_IN_DOUBLE As Byte = 8 ‘VC6的Double佔 8 Bytes Private ReadOnly BUFFERSIZE As Integer = 512
#End Region
#Region “Private Var”
Private m_baryBinaryData(BUFFERSIZE) As Byte Private m_fsFileStream As FileStream Private m_nDataLength As Integer Private m_strData As String Private m_nData As Integer Private m_sData As Single Private m_dData As Double Private m_bData As Boolean #End Region
read morePosts
[VB.NET]統計英文字串中字母個數
For Each c As Char In inputString idx = Asc(c) - startAscii symbolCount(idx) = symbolCount(idx) + 1 If symbolCount(idx) > symbolCount(maxSymbolIdx) Then maxSymbolIdx = idx End If Next For idx = 0 To 25 Console.Write(Chr(startAscii + idx) & ": ") Console.WriteLine(symbolCount(idx).ToString) Next Console.WriteLine() Console.Write("Max Count Symbol: ") Console.WriteLine(Chr(startAscii + maxSymbolIdx)) End Sub</pre></div><p> </p><h2> </h2><h2>解法二</h2><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:313a96f2-0f63-4ed7-aa9d-e6b0bc28f245" class="wlWriterEditableSmartContent"><pre class="vb:nocontrols" name="code"> Sub Test(ByVal inputString As String) Dim chars() As Char = inputString.
read morePosts
[VB.NET]列舉型別字串處理的注意事項
Sub Main() Dim msg As String Dim sex As SexType = SexType.Boy msg = "Sex: " & sex.ToString & " (" & sex & ")" Console.WriteLine(msg) msg = String.Format("Sex: {0} ({1})", sex, CInt(sex)) Console.WriteLine(msg) End Sub End Module 執行結果
read morePosts
[C#][VB.NET].NET 4.0 Barrier Class
Module Module1
Private sync As Barrier Sub Main(ByVal args() As String) sync = New Barrier(3) Dim charlie = New Thread(Sub() DriveToBoston("Charlie", TimeSpan.FromSeconds(1))) charlie.Start() Dim mac = New Thread(Sub() DriveToBoston("Mac", TimeSpan.FromSeconds(2))) mac.Start() Dim dennis = New Thread(Sub() DriveToBoston("Dennis", TimeSpan.FromSeconds(3))) dennis.Start() charlie.Join() mac.Join() dennis.Join() Console.ReadKey() End Sub Sub DriveToBoston(ByVal name As String, ByVal timeToGasStation As TimeSpan) Console.WriteLine("[{0}] Leaving House", name) ' Perform some work Thread.Sleep(timeToGasStation) Console.WriteLine("[{0}] Arrived at Gas Station", name) ' Need to sync here sync.
read morePosts
[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#][VB.NET]擴充方法 (Extension Method)
public static class EnumExtension { public static string GetName(this Enum e) { return Enum.GetName(e.GetType(), e); } } class Program { static void Main(string[] args) { Console.WriteLine(Grade.APlus.GetName()); } }</pre></div> Enum Grade APlus A B C D End Enum
Module Module1 Sub Main() Console.WriteLine(Grade.APlus.GetName) End Sub End Module
Module EnumExtension <Extension()> _ Public Function GetName(ByVal e As [Enum]) As String Return [Enum].GetName(e.GetType, e) End Function End Module
class Program { static void Main(string[] args) { string str = null; Console.
read morePosts
[VB.NET]用.NET實作檔案總管
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET]從登錄檔中讀取CPU資訊
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET]HatchBrush
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET]ErrorProvider
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET]自定義ComboBox下拉清單選項
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET]用MyDataBase更新DataGridView上變動的資料
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET]增加圖片亮度
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[C#][VB.NET]彩色濾鏡
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[C#][VB.NET]反轉圖片顏色
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[C#][VB.NET]彩色圖片轉為黑白圖片
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET]突破Disable按鈕的封鎖與限制
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[C#][VB.NET]壓縮.NET程式的記憶體用量
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[Library][VB.NET].NET簡易測試用表單類別
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET]Lambda運算式
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET].NET多語系程式(三)
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET].NET多語系程式(一)
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[VB.NET].NET多語系程式(二)
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[C#][VB.NET]Isolated Storage 隔離儲存區
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[.NET Concept][C#][VB.NET].NET兩個表單間的資料互通
Public Class Form2 … Public MainForm As Form1 … ‘Form2透過Form1傳進的物件參考控制Form1 MainForm.Value = Me.NumericUpDown1.Value … End Class
read morePosts
[C#][VB.NET]自定義.NET WindowForm表單介面(二)
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[Performance][VB.NET].NET空字串判斷徹底研究
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[Performance][VB.NET]If V.S IIf
<td valign="top" width="133">If</td> <td valign="top" width="133">IIf</td> </tr> <tr> <td valign="top" width="133">10000</td> <td valign="top" width="133">0 ms</td> <td valign="top" width="133">0 ms</td> </tr> <tr> <td valign="top" width="133">100000</td> <td valign="top" width="133">1 ms</td> <td valign="top" width="133">3 ms</td> </tr> <tr> <td valign="top" width="133">1000000</td> <td valign="top" width="133">10 ms</td> <td valign="top" width="133">32 ms</td> </tr> <tr> <td valign="top" width="133">10000000</td> <td valign="top" width="133">101 ms</td> <td valign="top" width="133">326 ms</td> </tr>
read morePosts
[C#][VB.NET]使用AxWindowsMediaPlayer撥放多媒體
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[C#][VB.NET]使用AxMediaPlayer撥放多媒體
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[.NET Concept][C#][VB.NET]四捨六入五成雙
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[C#][VB.NET]XML序列化私有欄位
#Region “Enum” Enum SexType Boy Girl End Enum #End Region
#Region “Var” Private _name As String Private _year As Integer Private _sex As SexType Private _friendNames As New List(Of String) #End Region
#Region “Property” Public Property Name() As String Get If String.IsNullOrEmpty(_name) Then Return String.Empty End If Return _name End Get Set(ByVal value As String) _name = value End Set End Property
Public Property Year() As Integer Get Return _year End Get Set(ByVal value As Integer) _year = value End Set End Property Public Property Sex() As SexType Get Return _sex End Get Set(ByVal value As SexType) _sex = value End Set End Property #End Region
read morePosts
[C#][VB.NET]自製桌面小玩意
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If PictureBox1.Capture = True Then '如果滑鼠按著拖曳 '設定新的視窗位置 Me.Top = e.Y + nOldWndTop - nClickY Me.Left = e.X + nOldWndLeft - nClickX '更新紀錄的視窗位置 nOldWndLeft = Me.Left nOldWndTop = Me.Top End If End Sub</pre></div><p> </p><p>C#</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:f0c57834-e54e-4456-896a-5ebc58634d32" class="wlWriterSmartContent"><pre class="c#" name="code"> private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
read morePosts
[C#][VB.NET]自定義.NET WindowForm表單介面
Private Sub pnlTitleBar_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlTitleBar.MouseMove If pnlTitleBar.Capture = True Then '如果滑鼠按著拖曳 '設定新的視窗位置 Me.Top = e.Y + nOldWndTop - nClickY Me.Left = e.X + nOldWndLeft - nClickX '更新紀錄的視窗位置 nOldWndLeft = Me.Left nOldWndTop = Me.Top End If End Sub</pre></div><p> </p><p>C#</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:0d2adec9-5189-493d-821d-1272f208c5e0" class="wlWriterSmartContent"><pre class="c#" name="code"> private void pnlTitleBar_MouseDown(object sender, MouseEventArgs e) { //紀錄滑鼠點選時的視窗位置與滑鼠點選位置 nOldWndLeft = this.
read morePosts
[C#][VB.NET]設定.NET透明表單
Abstract Introduction Form.Opacity屬性設定不透明度 Form.TransparencyKey屬性設定透明的顏色 Form.Opacity VS Form.TransparencyKey Conclusion Introduction 在.NET WinForm程式中,要設定透明表單有兩種方式。一種是用Form.Opacity屬性來設定表單的不透明度,一種則是用Form.TransparencyKey屬性來設定表單上視為透明的顏色。本篇將會對兩種方法做個簡單的介紹,並針對兩者做點小小的比較。
Form.Opacity屬性設定不透明度 以Form.Opacity屬性為例,該屬性值所設定的是0~1之間的雙精度浮點數(Double),用以表示表單的不透明度比例。如下圖所示,該值越小越透明、越大則越不透明。
完整範例如下:
VB.NET
C#
Form.TransparencyKey屬性設定透明的顏色 要用Form.TransparencyKey屬性來設定表單透明度,只須把該屬性設為欲透明的顏色,則當表單上存在著被設為透明色的顏色區塊時,該顏色區塊就會被視為是透明的。用該方法設定的透明效果跟用Form.Opacity的方法是不同的。該方法的透明是完全的透明(透明度100%),且若用滑鼠點選在表單中透明的區塊上,滑鼠的點選動作會穿透目前的表單點選到目前表單下方的視窗,而Form.Opacity的透明除非是設為完全透明,不然無法穿透目前的表單。
**P.S.**Form.Opacity = 0 雖然也可以完全穿透目前表單,但是表單上面的元件也會跟著透明。
Form.Opacity VS Form.TransparencyKey 可調整透明度比例 點選表單透明區塊可穿透表單 可設定表單一部份為透明區塊,只會影響跟設定相同顏色的區塊 點選表單不完全透明區塊(Form.Opacity != 0)不可穿透表單 透明度會影響整個表單上的元件 不可調整透明度比例 Conclusion 本篇介紹了WinForm中改變表單透明度的兩種方法。雖然兩種方法都可以達到透明表單的效果,但在實際的應用上,我個人覺得用Form.TransparencyKey來設定表單透明度較為實用。雖此方法無法調整透明程度,但是其所具備的穿透透明區塊與可透明表單中部分區塊的特性,大幅增加了應用上的彈性。
read morePosts
[C#][VB.NET].NET捷徑(ShortCut)控制
Function GetLnkWorkingDirectory(ByVal shortCutFile As String) As String Return GetLnkObj(shortCutFile).WorkingDirectory End Function
Function GetLnkPath(ByVal shortCutFile As String) As String Return GetLnkObj(shortCutFile).Path End Function C# private String GetLnkArguments(string shortCutFile) { return GetLnkObj(shortCutFile).Arguments; }
private String GetLnkWorkingDirectory(string shortCutFile) { return GetLnkObj(shortCutFile).WorkingDirectory; }
private String GetLnkPath(string shortCutFile) { return GetLnkObj(shortCutFile).Path; } 這邊須注意的是,使用此方法建立捷徑時,若捷徑檔不存在於指定位置,則須先建立個空的捷徑檔案,才可取得ShellLinkObject物件。VB.NET If Not My.Computer.FileSystem.FileExists(shortCutFile) Then File.Create(shortCutFile).Close() End If C# if (!My.Computer.FileSystem.FileExists(shortCutFile)) { File.Create(shortCutFile).Close(); } 取得ShellLinkObject物件後,捷徑的建立與寫入也就只是設定對應的屬性值後呼叫Save方法。VB.NET lnkObj = GetLnkObj(shortCutFile) With lnkObj .Arguments = arguments .
read more