VSTO开发-VB.NET笔记
1.字典定义、创建,元素添加,结果输出到数组
Imports System.Collections.Generic
Module Module2
Sub Main()
' 创建字典实例
Dim myDictionary As New Dictionary(Of Integer, String)
' 添加元素到字典
myDictionary.Add(1, "Element1")
myDictionary.Add(2, "Element2")
myDictionary.Add(3, "Element3")
' 获取键的数组
Dim keysArray() As Integer = myDictionary.Keys.ToArray()
' 获取值的数组
Dim valuesArray() As String = myDictionary.Values.ToArray()
' 输出键和值数组的内容
Console.WriteLine("Keys array:")
For Each key As Integer In keysArray
Console.WriteLine(key)
Next
Console.WriteLine("Values array:")
For Each value As String In valuesArray
Console.WriteLine(value)
Next
' 等待用户输入,防止程序立即退出
Console.ReadLine()
End Sub
End Module2.字典汇总用法
Sub CreateHz(Wh As Excel.Worksheet, Rng As Excel.Range)
Dim SourArr As Object = Rng.Value2 Dim d As New Dictionary(Of String, Double)
For i As Int32 = 1 To UBound(SourArr)
If Len(SourArr(i, 1)) > 0 Then
If d.ContainsKey(SourArr(i, 1)) Then
d(SourArr(i, 1)) += SourArr(i, 11)
Else
d.Add(SourArr(i, 1), SourArr(i, 11))
End If
End If
Next
Dim dKeys() As Object = d.Keys.ToArray()
Dim dValues() As Double = d.Values.ToArray()
With Wh
.Range("P3").Resize(d.Keys.Count, 1).Value2 = Xlapp.WorksheetFunction.Transpose(dKeys)
.Range("Q3").Resize(d.Keys.Count, 1).Value2 = Xlapp.WorksheetFunction.Transpose(dValues)
.Range("P2").Resize(d.Keys.Count + 1, 2).Interior.Color = 5296274
.Range("Q3").Resize(d.Keys.Count, 1).NumberFormatLocal = "0.000000"
.Range("M2:T2").EntireColumn.AutoFit()
End With
End Sub3.条件格式:单元格区域中,当单元格值为“无”,填充颜色为黄色。
ActWh.Range("A" & ActRs + 1).Resize(tempCount, UBound(JgArr, 2) + 1).FormatConditions.Delete()
Dim fC As Excel.FormatCondition = ActWh.Range("A" & ActRs + 1).Resize(tempCount, UBound(JgArr, 2) + 1).FormatConditions.Add(Type:=Microsoft.Office.Interop.Excel.XlFormatConditionType.xlCellValue, Operator:=Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlEqual, Formula1:="无")
' 设置条件格式样式,字体颜色为黄色
With fC.Interior
.iColor = RGB(255, 255, 0) ' 黄色
'.Bold = True
End With4.读取文件最后写入时间,转化成版本
Dim lastWriteTime As DateTime = System.IO.File.GetLastWriteTime(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "远东百宝箱V1.dll")) RevVersion = Format(lastWriteTime, "yy.MM.dd.HHmm")
5.InputBox取消后忽略错误代码
Dim Msg As String = String.Format("{0}:{1}", ex.Source, ex.Message)
If Strings.InStr(Msg, "System.Boolean") = 0 Then
MsgBox(Msg, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation)
End If6.ListBox项目读取到一维数组
' 假设 ListBox1 是你的 Listbox 控件的名称
' 创建一个数组,大小与 Listbox 中的项目数相同
Dim itemsArray As String() = New String(ListBox1.Items.Count - 1) {}
' 将 Listbox 中的项目复制到数组中
ListBox1.Items.CopyTo(itemsArray, 0)
' 现在 itemsArray 包含了 Listbox 中的所有项目
' 下面是一个简单的循环,用于遍历数组并打印每个项目
For i As Integer = 0 To itemsArray.Length - 1
Console.WriteLine(itemsArray(i))
Next7.始终在窗体中间显示提示窗体
Dim f As New ShowAutoClose
f.StartPosition = Windows.Forms.FormStartPosition.Manual
f.Left = Me.Left + Me.Width / 2 - f.Width / 2
f.Top = Me.Top + Me.Height / 2 - f.Height / 2
f.P("密度列表不能为空!")8.要在TopMost=True的窗体上显示一个消息窗体,只需要把消息窗体的属性TopMost=True即可。
9.判断ListBox是否有选中项目
在VB.NET中,判断ListBox是否有选中项可以通过检查SelectedIndex属性是否大于等于0来实现。SelectedIndex表示当前选中项的索引,如果没有选中任何项,它的值会是-1。下面是一个基本的代码示例:
' 检查ListBox是否有选中项
If ListBox1.SelectedIndex >= 0 Then
' 有选中项
MessageBox.Show("您已经选中了项。")
Else
' 没有选中任何项
MessageBox.Show("您没有选中任何项。")
End If另外,如果你使用的是多选的ListBox(SelectionMode属性设置为Multiple或MultiExtended),你可以通过检查SelectedItems集合中的元素数量来确定是否有选中项:
' 检查多选的ListBox是否有选中项
If ListBox1.SelectedItems.Count > 0 Then
' 有选中项
MessageBox.Show("您已经选中了项。")
Else
' 没有选中任何项
MessageBox.Show("您没有选中任何项。")
End If《E效》下载地址






