我强烈怀疑我不想做的事情,但我可以使用确认。
我有一个我创建的类库来处理一些常见的Subs&我经常使用的功能(以某种形式或方式)。
其中一些子&函数可以花很长时间(通常在记录以某种方式迭代和处理的情况下)。在这些情况下,我通常会让函数更新进度窗口(文本和步进进度条)。
既然我已经将它们列入类库中,我显然无法预料到存在特定的形式或控件。但我想保留这些功能。所以我希望做的是为RichTextBox&创建可选的输入变量。 ProgressBar将会更新,以便我可以传入对应该用于状态的控件的引用。
这可能吗?如果是这样,怎么样?当我尝试将任何输入定义为Windows.Forms.<anything>
Intellisense退出时(强烈暗示我无法将它们键入为控件)。
谢谢!
更新:我尝试添加以下引用的函数,System.Windows.Forms.<X>
Public Function ResOut(ByVal D As DataTable, ByVal epath As String, ByVal SAName As String, ByVal Parent As String, ByRef PBar As System.Windows.Forms.ProgressBar, _
ByRef RTB As System.Windows.Forms.RichTextBox) As String
'
Dim res As String = ""
Dim E As New Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook = Nothing
Dim ws As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Dim x As Long = 0
Dim f As Long = 1
Dim s As New JMLib.Status
Dim Rng As Microsoft.Office.Interop.Excel.Range
'Define the range
Rng = ws.Range("A1:" & ColNumToStr(D.Columns.Count, epath) & D.Rows.Count)
'Create the array
Dim OArr(D.Rows.Count, x) As Object
'Create a workbook for the data and capture the workbook and sheet for ease of reference
Try
wb = E.Workbooks.Add
ws = wb.Worksheets(1)
Catch ex As Exception
res = "Encountered an error while creating the new workbook to export the results to. No data can be returned."
EL.AddErr(res & " ResOut was called by " & Parent & ". Error Details: " & ex.Message, epath)
End Try
'Fill in headers
If res = "" Then
Try
For Each c As DataColumn In D.Columns
ws.Range("A1").Offset(0, x).Value = c.ColumnName
x = x + 1
Next
Catch ex As Exception
res = "Encountered an error while filling in the column headers. This will prevent any data from being returned."
EL.AddErr(res & " ResOut was called by " & Parent & ". Error Details: " & ex.Message, epath)
End Try
End If
'Setup the step & frequency for the Step Progress bar
'Dim t() As Long = s.StatSetup(QR.Rows.Count, 58, "Query Runner\ResOut\" & QName, Replace(My.Settings.EPath, "<user>", Environment.UserName) & DStamp() & " Query Scheduler Log.txt")
'f = t(0)
'SProg.Step = t(1)
'Convert the datatable to a 2D array
If res = "" Then
Try
'Fill it
x = 0
For r As Long = 0 To D.Rows.Count - 1 Step 1
Dim dr As DataRow = D.Rows(r)
For c As Integer = 0 To D.Columns.Count - 1 Step 1
OArr(r, c) = dr.Item(c)
Next
x = x + 1
Next
Catch ex As Exception
res = "Encountered an error while outputing the " & x + 1 & "-th record of " & D.Rows.Count & ". No data will be output."
EL.AddErr(res & " ResOut was called by " & Parent & ". Error Details: " & ex.Message, epath)
End Try
End If
'output the array to the target range
If res = "" Then
Try
Rng.Value = OArr
'Save the workbook
wb.SaveAs(SAName)
wb.Close(SaveChanges:=False)
Catch ex As Exception
res = "Encountered an error during the export of the results. Some data may have been exported. Review the contents of the Excel workbook that will be visible following this message for more" _
& " details."
E.Visible = True
wb.Activate()
EL.AddErr(res & " ResOut was called by " & Parent & ". Error Details: " & ex.Message, epath)
End Try
Else
'Close the workbook without saving
wb.Close(SaveChanges:=False)
End If
'Cleanup the application
Try
E.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(E)
E = Nothing
wb = Nothing
ws = Nothing
s = Nothing
Rng = Nothing
OArr = Nothing
f = Nothing
x = Nothing
Catch ex As Exception
EL.AddErr("Encountered an error while cleaning up the resources used in JMLib\ResOut. ResOut was called by " & Parent & ". Error Details: " & ex.Message, epath)
End Try
Return res
End Function
答案 0 :(得分:0)
我是通过将对进度条/文本框的引用添加到类中来实现的。
调用类时,您可以设置引用。
在课堂上如果引用ISNOT NOTHING我更新进度条/文本框。
没有具体关于在用户编写的类中 - 这是一些库代码的例子:
Sub DisplayParcels(aAPNs() As String,...., Optional ProgBar As ProgressBar = Nothing)
...
If ProgBar IsNot Nothing Then
ProgBar.Maximum = aAPNs.Length
ProgBar.Value = 0
ProgBar.Visible = True
End If
...
For Each sAPN In aAPNs
...
If ProgBar IsNot Nothing Then ProgBar.Value = iCnt
Next
...
If ProgBar IsNot Nothing Then ProgBar.Visible = False
End Sub