Visual Studio2013提供的Snippet,
包含了許多的項目,
本文介的項目為Windows Form應用程式
12個項目列示如下,
1.ASP.NET MVC4
2.My Code程式碼片段
3.Office程式碼片段
4.Windows Form應用程式
5.Windows系統-記錄,處理序,登錄,服務
6.WPF
7.其他-連接,安全性,工作流程
8.基本-集合,資料類型,檔案系統,數學
9.應用程式-編譯,資源和設定
10.測試
11.程式碼模式-If,For Each,Try Catch,Property等
12.資料-LINQ,XML,設計工具,ADO.NET
依照Visusl Studio2013程式片段管理員中,
列示的項目順序,
把提供的Snippet詳細內容列示在下方,
雖然跟使用Snippet右鍵時,
列示的項目順序不同,
不過可以先查閱一下內容
4.Windows Form應用程式
4.1字型
4.1.1建立字型: 使用 Font 建構函式的三個多載建立字型。
Dim italicFont As New Font("Courier
New",
12, FontStyle.Italic)
|
4.2表單
4.2.1由對話方塊的MDI父表單擷取資訊: 由
MDI 表單擷取資訊。
text = Me.ParentForm.Text
|
4.2.2在執行階段表單加入Windows Form控制項: 在執行階段建立 Windows Form TextBox 控制項的新執行個體。
Dim TextBox1 As New TextBox()
With TextBox1
.Location = New Point(64, 40)
.Size = New Size(100, 20)
.TabIndex = 0
.Text = "TextBox1"
End With
Controls.Add(TextBox1)
|
4.2.3判斷Windows form上選取的控制項: 尋找在表單上選取的控制項。
selectedControl = Me.ActiveControl
|
4.2.4判斷按下的輔助按鍵: 監視輔助按鍵,例如 SHIFT、ALT 和 CTRL 鍵。
Private Sub Me_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If
(Control.ModifierKeys And Keys.Shift) = Keys.Shift Then
End If
End Sub
|
4.2.5找出現用的MDI子表單和控制項: 擷取現用的 MDI 子表單,以及子表單上的現用控制項。
Dim activeChild As Form = Me.ActiveMdiChild
Dim activeControl As Control = Nothing
If Not activeChild IsNot Nothing Then
activeControl = activeChild.ActiveControl
End If
|
4.2.6拖曳表單的工作區以移動視窗: 使用滑鼠拖曳表單的任何部分,即可移動該表單。當表單沒有標題列時,這項功能特別實用。
Dim mouseOffset As Point
Private Sub Me_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
mouseOffset = New Point(-e.X, -e.Y)
End Sub
Private Sub Me_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
If e.Button =
MouseButtons.Left Then
Dim mousePos =
Control.MousePosition
mousePos.Offset(mouseOffset.X,
mouseOffset.Y)
Location = mousePos
End If
End Sub
|
4.2.7建立MDI子表單: 建立及顯示新的 MDI 子表單。
Dim newMDIChild As New Form()
newMDIChild.MdiParent
= Me
newMDIChild.Show()
|
4.2.8建立透明的Window Form: 將表單的透明度設定為透明。
frmTransparentForm.Opacity
= 0.83
|
4.2.9限制控制項可接受的按鍵: 覆寫 Windows Form 控制項預設的 ProcessCmdKey 函式,並限制使用者只能以數值和巡覽鍵輸入。
Class restrictedComboBoxClass
Inherits ComboBox
Const WM_KEYDOWN As Integer = &H100
Protected Overrides Function ProcessCmdKey _
(ByRef msg As Message, _
ByVal keyData As Keys) As Boolean
If msg.Msg =
WM_KEYDOWN Then
Return Not ((keyData >=
Keys.D0 And keyData <=
Keys.D9) _
Or keyData =
Keys.Back Or keyData =
Keys.Left _
Or keyData =
Keys.Right Or keyData = Keys.Up
_
Or keyData =
Keys.Down Or keyData =
Keys.Delete
End If
Return MyBase.ProcessCmdKey(msg,
keyData)
End Function
End Class
|
4.2.10將Windows
Form保留在最上層: 說明 TopMost 屬性如何控制表單是否要保留為最上層。最上層表單會一直浮在其他非最上層表單上方,即使不是現用表單時也一樣。
theTopForm.TopMost
= True
|
4.2.11將滑鼠座標轉換成螢幕座標: 將滑鼠座標由滑鼠事件轉換成螢幕座標。
Private Sub Me_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseDown
Dim screenPoint As Point
screenPoint = Me.PointToScreen(New Point(e.X, e.Y))
End Sub
|
4.2.12從Windows
Form播放嗶聲: 播放嗶聲。
Beep()
|
4.2.13排列MDI子表單: 以階層式配置排列子表單。
Me.LayoutMdi(MdiLayout.Cascade)
|
4.2.14排除表單中的TitleBar: 排除表單中的 TitleBar。
Me.FormBorderStyle =
Windows.Forms.FormBorderStyle.None
|
4.2.15設定Windows Form的畫面位置: 設定表單左上角的位置,以像素為單位。
Me.DesktopLocation =
New Point(100, 100)
|
4.2.16調整Windows Form大小: 調整表單大小。
Me.Size = New Size(100, 100)
|
4.2.17擷取對話方塊的結果: 決定對話方塊關閉的方式。
If dialog.ShowDialog
= DialogResult.OK Then
End If
|
4.2.18顯示Windows Form: 顯示 Windows Form。
Form.Show()
|
4.3剪貼簿
4.3.1由剪貼簿取得字串: 由剪貼簿取得字串,並儲存至字串物件中。
Dim text As String
If My.Computer.Clipboard.ContainsText
Then
text = My.Computer.Clipboard.GetText
End If
|
4.3.2由剪貼簿取得影像: 由剪貼簿取得影像,並儲存至影像物件中。
Dim picture As Image
If
My.Computer.Clipboard.ContainsImage() Then
picture = My.Computer.Clipboard.GetImage
End If
|
4.3.3將字串複製到剪貼簿: 將字串複製到剪貼簿。
My.Computer.Clipboard.SetText("My
String")
|
4.3.4將影像複製到剪貼簿: 將影像複製到剪貼簿。
My.Computer.Clipboard.SetImage(picture)
|
4.3.5將類別執行個體複製到剪貼簿: 將可序列化類別的執行個體複製到剪貼簿。
My.Computer.Clipboard.SetData("Object",
classInstance)
|
4.4控制項和元件
4.4.0.1在Windows
Form控制項中加入工具提示: 在
Windows Form 控制項中加入工具提示。
Dim tooltip As New ToolTip()
tooltip.SetToolTip(Button1,
"ToolTip Text")
|
4.4.1ComBox
4.4.1.1由ArrayList填入ComboBox: 使用 ArrayList 物件當做 ComboBox 控制項的資料來源。
ComboBox1.DataSource
= arrayListName
|
4.4.1.2由資料庫資料表的資料行填入ComboBox: 以來自 DataTable 物件的資料填入 ComboBox。
Me.ComboBox1.DataSource
= DataTable1
Me.ComboBox1.DisplayMember
= "ColumnName"
|
4.4.1.3為ComboBox提供自動文字完成: 為下拉式方塊提供自動文字完成的功能。
ComboBox1.AutoCompleteMode
= AutoCompleteMode.Suggest
ComboBox1.AutoCompleteSource
= AutoCompleteSource.AllUrl
|
4.4.2DataGridView
4.4.2.1鎖定DataGridView的資料行寬度: 鎖定 DataGridView 的資料行寬度。
DataGridView1.AllowUserToResizeColumns
= False
|
4.4.3ListBox
4.4.3.1在Windows
Form ListBox控制項中尋找項目: 在
ListBox 控制項中尋找字串第一次出現的位置。
Dim index As Integer
index =
ListBox1.FindString("Maria")
If index <>
ListBox.NoMatches Then
' Item found.
Else
' Item not found.
End If
|
4.4.3.2將資料庫資料表中的資料行載入至ListBox控制項: 以
DataTable 物件中的資料填入 ListBox。
ListBox1.DataSource
= DataTable1
ListBox1.DisplayMember
= "ColumnName"
|
4.4.3.3清除繫結的ListBox控制項項目: 清除繫結的 ListBox 控制項項目。
ListBox1.DataSource
= Nothing
ListBox1.Items.Clear()
|
4.4.3.4選取Windows Form ListBox控制項中的項目: 以程式設計的方式選取 ListBox 控制項中的項目。
ListBox1.SelectedIndex
= 1
|
4.4.4ListView
4.4.4.1判斷ListView中選取的項目: 判斷 ListView 中選取的項目,並指派給變數。
Dim selectedItem As ListViewItem
If
ListView1.SelectedItems.Count > 0 Then
selectedItem = ListView1.SelectedItems(0)
Else
selectedItem = Nothing
End If
|
4.4.5ProgressBar
4.4.5.1使用ProgressBar追縱反覆運算: 每次反覆運算時,以固定的量遞增 ProgressBar。
With ProgressBar1
.Minimum = 1
.Maximum = 100000
.Value = 1
.Step = 1
For i As Integer = .Minimum To .Maximum
' Perform one step of the action being tracked.
.PerformStep()
Next i
End With
|
4.4.6RadioButton
4.4.6.1由字串陣列建立RadioButton群組: 由字串陣列建立 RadioButton 群組。
Dim radio As RadioButton
Dim y As Integer = 30
For Each button As String In names
radio = New RadioButton()
With radio
.Location = New Point(10, y)
.Text = button
End With
y += 30
Me.GroupBox1.Controls.Add(radio)
Next
|
4.4.7RichTextBox
4.4.7.1在RichTextBox控制項中以粗體格式化字元: 在現有的 RichTextBox 控制項中加入「這是粗體字。」這段文字。
' For more
information on the rich text format characters, see:
http://www.microsoft.com/downloads/details.aspx?FamilyID=e5b8ebc2-6ad6-49f0-8c90-e4f763e3f04f&DisplayLang=en
RichTextBox1.Rtf =
"{\rtf1\ansi This is in \b bold\b0.}"
|
4.4.7.2將RichTextBox的內容儲存至RTF檔: 將
RichText 方塊的內容儲存至 RTF 檔。
RichTextBox1.SaveFile("FileName.rtf")
|
4.4.7.3將RTF檔載入至RichTextBox控制項: 將
RTF 格式的檔案載入 RichTextBox 控制項中。
RichTextBox1.LoadFile("RichTextDocument.rtf")
|
4.4.7.4將游標放置在RichTextBox中: 將游標放置在 RichTextBox 中。
RichTextBox1.Focus()
RichTextBox1.Select(5,
0)
|
4.4.8TextBox
4.4.8.1在多行TextBox中輸入多行字串: 在多行文字方塊中輸入多行字串。
TextBox1.Lines =
names
|
4.4.9TreeView
4.4.9.1判斷TreeView中選取的節點: 判斷 TreeView 控制項中選取的節點,並指派給變數。
Dim selectedNode As TreeNode
selectedNode =
TreeView1.SelectedNode
|
4.4.10月曆
4.4.10.1在Windows
Form MonthCalenar中選取日期範圍: 在
MonthCalendar 控制項中選取日期範圍。
MonthCalendar1.SetSelectionRange(#3/1/2005#,
#3/21/2005#)
|
4.4.11功能表
4.4.11.1由MenuStrip刪除ToolStripMenuItem: 由現有的 MenuStrip 刪除 ToolStripMenuItem。
MenuStrip1.Items.Remove(ToolStripMenuItem1)
|
4.4.11.2在ToolStripMenuItem中加入快速鍵: 在 ToolStripMenuItem 中加入快速鍵。
ToolStripMenuItem1.ShortcutKeys
= F6
|
4.4.11.3在ToolStrinMenuItem中加入核取記號: 在 ToolStripMenuItem 中加入核取記號。
ToolStripMenuItem1.Checked
= True
|
4.4.11.4在Windows
Form中加入內容功能表: 在
Windows Form 中加入內容功能表。
Dim newMenu As New
ContextMenuStrip()
Me.ContextMenuStrip
= newMenu
Dim firstItem As New
ToolStripMenuItem()
Dim secondItem As New
ToolStripMenuItem()
firstItem.Text =
"&First Item"
secondItem.Text =
"&Second Item"
newMenu.Items.Add(firstItem)
newMenu.Items.Add(secondItem)
|
4.4.11.5在Windows
Form中加入功能表項目: 在現有的功能表中加入功能表項目。
Dim
addedMenuStripItem As New ToolStripMenuItem
Dim firstDropDownItem
As New ToolStripMenuItem
Dim
secondDropDownItem As New ToolStripMenuItem
addedMenuStripItem.Text
= "&Menu Name"
firstDropDownItem.Text
= "&First Item"
secondDropDownItem.Text
= "&Second Item"
MenuStrip1.Items.Add(addedMenuStripItem)
addedMenuStripItem.DropDownItems.Add(firstDropDownItem)
addedMenuStripItem.DropDownItems.Add(secondDropDownItem)
|
4.4.12按扭
4.4.12.1使用AutoSize動態調整按紐的大小: 使用 AutoSize 依據按鈕文字的長度動態調整大小。
Button1.AutoSize =
True
|
4.4.12.2測量按紐的文字以動態調整大小: 測量按鈕的文字並重設其寬度,藉以調整大小。
' Use a Graphics
object to measure the button's text.
Then add blanks to leave space on either side.
Dim surface As Graphics = Button1.CreateGraphics
Dim textSize As SizeF = surface.MeasureString(" " &
Button1.Text & " ", Button1.Font)
surface.Dispose()
Button1.Width = CInt(textSize.Width)
|
4.5聲音
4.5.1停止在背景播放音效: 停止在背景播放的音效。
My.Computer.Audio.Stop()
|
4.5.2循環Windows Form上播放的音效: 在背景循環播放音效。
My.Computer.Audio.Play("SoundFile.wav",
AudioPlayMode.BackgroundLoop)
|
4.5.3播放音效: 播放檔案中的音效。
My.Computer.Audio.Play("ringout.wav", AudioPlayMode.WaitToComplete)
|
4.6繪圖
4.6.1在Windos
Form上繪製文字: 在表單上繪製文字。
Dim drawFormat As New StringFormat()
Using formGraphics As Graphics = Me.CreateGraphics(),
_
drawFont As New
System.Drawing.Font("Arial", 16), _
drawBrush As New
SolidBrush(Color.Red)
formGraphics.DrawString("hello",
drawFont, drawBrush, _
150.0, 50.0, drawFormat)
End Using
|
4.6.2在Windos
Form上繪製垂直文字: 在表單上以垂直方向繪製文字。
Dim drawFormat As New StringFormat()
Using formGraphics As Graphics = Me.CreateGraphics(),
_
drawFont As New
System.Drawing.Font("Arial", 16), _
drawBrush As New
SolidBrush(Color.Red)
drawFormat.FormatFlags =
StringFormatFlags.DirectionVertical
formGraphics.DrawString("hello", drawFont, drawBrush, _
150.0, 50.0, drawFormat)
End Using
|
4.6.3在Windos
Form上繪製實心矩形: 在表單上繪製實心矩形。
Using redBrush As New
SolidBrush(Color.Red), _
formGraphics As Graphics = Me.CreateGraphics()
formGraphics.FillRectangle(redBrush, New Rectangle(0, 0,
200, 300))
End Using
|
4.6.4在Windos
Form上繪製實心楕圓形: 在表單上繪製實心橢圓形。
Using brush As New
SolidBrush(Color.Red)
Using formGraphics = Me.CreateGraphics()
formGraphics.FillEllipse(brush, New Rectangle(0, 0,
200, 300))
End Using
End Using
|
4.6.5在Windos
Form上繪製線條: 在表單上繪製線條。
Using redPen As New Pen(Color.Red), _
formGraphics As Graphics = Me.CreateGraphics()
formGraphics.DrawLine(redPen, 0, 0, 200,
200)
End Using
|
4.6.6在執行階段建立點陣圖: 建立及填入點陣圖物件,並且在現有的 Windows Form PictureBox 控制項中顯示。
Dim flag As New Bitmap(10, 10)
Dim x As Integer
Dim y As Integer
' Make the entire bitmap white.
For x = 0 To flag.Height - 1
For y = 0 To flag.Width - 1
flag.SetPixel(x, y, Color.White)
Next
Next
' Draw a diagonal red stripe.
For x = 0 To flag.Height - 1
flag.SetPixel(x, x, Color.Red)
Next
PictureBox1.Image
= flag
|
4.6.7使用GraphicsPath物件建立三角形: 藉由連接一連串的點繪製三角形。
Dim path As New GraphicsPath()
Dim points() As Point = { _
New Point(0, 0), _
New Point(100, 0), _
New Point(100, 100),
_
New Point(0, 0)}
path.AddLines(points)
Dim surface As Graphics = PictureBox1.CreateGraphics
surface.DrawPath(Pens.Black,
path)
|
4.6.8使用漸層填滿繪製圖案: 使用漸層填滿繪製圖案。
Dim rect As New Rectangle(0, 0,
100, 100)
Using gc = Me.CreateGraphics()
Using gradientBrush As New
LinearGradientBrush(rect, Color.Blue, Color.Black,
LinearGradientMode.Horizontal)
gc.FillRectangle(gradientBrush,
rect)
End Using
End Using
|
4.6.9取代為自訂筆刷: 使用影像檔建立 TextureBrush 物件。
Dim texture As New TextureBrush(Image.FromFile("ImageFile.bmp"))
|
4.6.10建立畫筆: 建立 Pen 物件。
Dim tomatoPen As
System.Drawing.Pen
tomatoPen = New System.Drawing.Pen(Color.Tomato)
|
4.6.11建立實心筆刷: 建立 SolidBrush 物件。
Dim aBrush As
System.Drawing.SolidBrush
aBrush = New System.Drawing.SolidBrush(Color.PeachPuff)
|
4.6.12繪製含外框的矩形: 在表單上繪製含外框的矩形。
Using redPen As New Pen(Color.Red), _
formGraphics As Graphics = Me.CreateGraphics()
formGraphics.DrawRectangle(redPen, New Rectangle(0, 0,
200, 300))
End Using
|
4.6.13繪製含外框的楕圖形: 在表單上繪製含外框的橢圓形。
Using redPen As New Pen(Color.Red), _
formGraphics As Graphics = Me.CreateGraphics()
formGraphics.DrawEllipse(redPen, New Rectangle(0, 0,
200, 300))
End Using
|
4.6.14繪製拖放矩形: 在表單上繪製拖放矩形。
Dim originalPoint As Point
Dim lastPoint As Point
Dim mouseIsDown As Boolean
Public Sub MyMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
mouseIsDown = True
originalPoint.X = e.X
originalPoint.Y = e.Y
lastPoint.X = -1
lastPoint.Y = -1
End Sub
Private Sub MyDrawReversibleRectangle(ByVal point1 As Point, ByVal point2 As Point)
Dim rect As Rectangle
point1 = PointToScreen(point1)
point2 = PointToScreen(point2)
If point1.X <
point2.X Then
rect.X = point1.X
rect.Width = point2.X - point1.X
Else
rect.X = point2.X
rect.Width = point1.X - point2.X
End If
If point1.Y <
point2.Y Then
rect.Y = point1.Y
rect.Height = point2.Y - point1.Y
Else
rect.Y = point2.Y
rect.Height = point1.Y - point2.Y
End If
ControlPaint.DrawReversibleFrame(rect, Color.Yellow, FrameStyle.Thick)
End Sub
Public Sub MyMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp
mouseIsDown = False
If lastPoint.X
<> -1 Then
Dim currentPoint As New Point(e.X, e.Y)
MyDrawReversibleRectangle(originalPoint, lastPoint)
End If
lastPoint.X = -1
lastPoint.Y = -1
originalPoint.X = -1
originalPoint.Y = -1
End Sub
Public Sub MyMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseMove
Dim currentPoint As New Point(e.X, e.Y)
If mouseIsDown Then
If lastPoint.X
<> -1 Then
MyDrawReversibleRectangle(originalPoint, lastPoint)
End If
lastPoint = currentPoint
MyDrawReversibleRectangle(originalPoint,
currentPoint)
End If
End Sub
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
mouseIsDown = False
End Sub
|
4.6.15繪製圓形圖: 繪製圓形圖。
' Shows how to call the DrawPieChart method
Public Sub DrawPieChartHelper()
Dim percents = {10,
20, 70}
Dim colors =
{Color.Red, Color.CadetBlue, Color.Khaki}
Using graphics = Me.CreateGraphics()
Dim location As New Point(10, 10)
Dim size As New Size(150, 150)
DrawPieChart(percents, colors,
graphics, location, size)
End Using
End Sub
' Draws a pie chart.
Public Sub DrawPieChart(ByVal percents() As Integer, ByVal colors() As Color,
ByVal surface As Graphics, ByVal location As Point,
ByVal pieSize As Size)
' Check if
sections add up to 100.
Dim sum = 0
For Each percent In percents
sum += percent
Next
If sum <> 100 Then
Throw New
ArgumentException("Percentages do not add up to 100.")
End If
If percents.Length
<> colors.Length Then
Throw New
ArgumentException("There must be the same number of percents and
colors.")
End If
Dim percentTotal = 0
For percent = 0 To percents.Length()
- 1
Using brush As New
SolidBrush(colors(percent))
surface.FillPie(brush,
New
Rectangle(location, pieSize),
CSng(percentTotal *
360 / 100),
CSng(percents(percent)
* 360 / 100))
End Using
percentTotal += percents(percent)
Next
Return
End Sub
|
4.6.16顯示動畫GIF: 顯示動畫 GIF。
PictureBox1.Image
= Image.FromFile("test.gif")
|
相關貼文
VB.Net開發-使用Visual Studio2013提供之Snippet程式碼片段
VB.Net Snippet,利用工具軟體Snippet Editor,自訂常用的程式碼片段
沒有留言:
張貼留言