VSTO 基础随笔
1.往组合框添加条目
1 With Me .ComboBox1.Items 2 .Add( "Ports" ) 3 .Add( "Igor" ) 4 End With
2.文本框Select Case
1 Private Sub Fname1() Handles ComboBox1.SelectedValueChanged 2 Select Case ComboBox1.SelectedItem 3 Case "Drivers" 4 Me .ListBox1.Items.Clear() 5 Me .ListBox1.Items.Add("Drivers" ) 6 Case "Ports" 7 Dim i As Integer = 0 8 Dim m = My .Computer.Ports.SerialPortNames.Count 9 Me .ListBox1.Items.Clear() 10 For i = 0 To m - 1 11 Me .ListBox1.Items.Add(CStr ( My.Computer.Ports.SerialPortNames(i))) 12 Next 13 End Select 14 End Sub
3.CType对象转换
1 Private Sub ThisWorkbook_Startup() Handles Me.Startup 2 Try 3 Dim SelectedRange As Excel.Range = _ 4 Application.InputBox(Prompt:= _ 5 "Select the cell or cells to add random value." , _ 6 Title:= "random values" , _ 7 [Default]:= CType (Application.Selection, Excel.Range).Address, Type:=8) 8 SelectedRange.Value = "=rand()" 9 Catch ex As Exception 10 11 End Try 12 End Sub
也可以:
1 Private Sub ThisWorkbook_Startup() Handles Me.Startup 2 Try 3 Dim Selection As Excel.Range = Me.Application.Selection 4 Dim SelectedRange As Excel.Range = _ 5 Application.InputBox(Prompt:= _ 6 "Select the cell or cells to add random value.", _ 7 Title:="random values", _ 8 [Default]:=Selection.Address, Type:=8) 9 SelectedRange.Value = "=rand()" 10 Catch ex As Exception 11 12 End Try 13 End Sub
4. 函数创建及调用
1 Private Sub ThisWorkbook_Startup() Handles Me.Startup 2 Try 3 Dim i As Double 4 Dim Selection As Excel.Range = Me.Application.Selection 5 Dim SelectedRange As Excel.Range = _ 6 Application.InputBox(Prompt:= _ 7 "Select the cell or cells to add random value.", _ 8 Title:="random values", _ 9 [Default]:=Selection.Address, Type:=8) 10 i = Rnd() 11 SelectedRange.Value = myFunction(i) 12 Catch ex As Exception 13 14 End Try 15 End Sub 16 17 Function myFunction(ByVal j As Double) As Double 18 myFunction = 5 * j 19 Exit Function 20 End Function