visual studio - 新文本框数组转到函数

时间:2022-09-21 02:28:48

I was wanting to create a function to update each textbox, progress bar and label text checking NAS storage sizze.

我想创建一个功能来更新每个文本框,进度条和标签文本检查NAS存储sizze。

Currently This works for the text that is not commented out . I was wanting to add more txtNAS, Progressbar's and Labels but I have to code it every time I find this rather annoying.

目前这适用于未注释掉的文本。我想添加更多的txtNAS,Progressbar和标签,但每次我觉得这很烦人时我都要编码。

I was thinking there should be a faster way to do this like an array . I have tried to do this but failed so far any ideas ?

我当时认为应该有更快的方式像数组一样做这个。我试过这样做但到目前为止任何想法都失败了?

Sub Submit()


' Dim i As Integer

'New method I tried

  '  i = 1
   ' Do Until i = 18

      '  findsize("txtNAS" & i & ".text", "ProgressBar" & i, "Label" & i)

      '  i = i + 1

    'Loop

'End of New method I tried

    Do

        findsize(txtNAS1.Text, ProgressBar1, Label1)

        findsize(txtNAS2.Text, ProgressBar2, Label2)

        findsize(txtNAS3.Text, ProgressBar3, Label3)

        findsize(txtNAS4.Text, ProgressBar4, Label4)

        findsize(txtNAS5.Text, ProgressBar5, Label5)

        findsize(txtNAS6.Text, ProgressBar6, Label6)

        findsize(txtNAS7.Text, ProgressBar7, Label7)

        findsize(txtNAS8.Text, ProgressBar8, Label8)

        findsize(txtNAS9.Text, ProgressBar9, Label9)

        findsize(txtNAS10.Text, ProgressBar10, Label10)

        findsize(txtNAS11.Text, ProgressBar11, Label11)

        findsize(txtNAS12.Text, ProgressBar12, Label12)

        findsize(txtNAS13.Text, ProgressBar13, Label13)

        findsize(txtNAS14.Text, ProgressBar14, Label14)

        findsize(txtNAS15.Text, ProgressBar15, Label15)

        findsize(txtNAS16.Text, ProgressBar16, Label16)

        findsize(txtNAS17.Text, ProgressBar17, Label17)

        findsize(txtNAS18.Text, ProgressBar18, Label18)

        pause(10)

    Loop

End Sub

Function findsize(ByVal strNAS As String, ByRef progressbar As ProgressBar, ByVal Label As Label)


    strNAS = Replace(strNAS, " ", "")

    If strNAS = "" Then

        GoTo endsub

    End If

    Dim objFSOd = CreateObject("Scripting.FileSystemObject")
    Dim strdrivelists
    Dim founddrive
    Dim strDriveLetter
    Dim strDrive


    For Each oDrives In objFSOd.Drives
        If oDrives.DriveType = 1 Or 2 Or 3 Or 4 Or 5 Or 6 Then
            strdrivelists = strdrivelists & oDrives.DriveLetter & ","

        End If
    Next


    founddrive = 0

    For i = 65 To 90
        strDrive = Chr(i)
        If InStr(strdrivelists, strDrive) Then

        Else

            founddrive = founddrive + 1

            If founddrive = 1 Then

                strDriveLetter = strDrive & ":"

            End If
        End If
    Next

    Dim maperror

    maperror = MapDrive(strDriveLetter, strNAS)


    If maperror = False Then

        GoTo endsub
    End If

    Dim FolderSizeMB, totalsize, AvailableSpace

    Dim oFS, drive, provalue
    oFS = CreateObject("Scripting.FileSystemObject")
    drive = oFS.GetDrive(oFS.GetDriveName(strDriveLetter))

    totalsize = drive.totalsize


    provalue = drive.totalsize - drive.AvailableSpace

    totalsize = totalsize / 1073741824

    AvailableSpace = drive.AvailableSpace / 1073741824


    progressbar.Maximum = drive.totalsize / 1000



    Label.Text = provalue / 1073741824 & "/" & totalsize & " GB Free Space " & AvailableSpace



    FolderSizeMB = FormatNumber(drive.FreeSpace / (1024 * 1024), 2)
    FolderSizeMB = Replace(FolderSizeMB, ",", "")


    progressbar.Value = provalue / 1000

    RemoveDriveMapped(strDriveLetter)

    strdrivelists = ""

endsub:

End Function

Function MapDrive(ByVal strDriveLetter, ByVal strNAS)

    On Error Resume Next

    Dim objNetwork As Object

    Err.Clear()

    objNetwork = CreateObject("Wscript.Network")
    objNetwork.MapNetworkDrive(strDriveLetter, strNAS, False)


    If Err.Number <> 0 Then
        MapDrive = False

    Else

        MapDrive = True
    End If

    objNetwork = Nothing
End Function

' This function removes the Mapped Drive
Function RemoveDriveMapped(ByVal strDriveLetter)

    On Error Resume Next

    Dim objNetwork

    Err.Clear()

    objNetwork = CreateObject("Wscript.Network")
    objNetwork.RemoveNetworkDrive(strDriveLetter, True)


    If Err.Number <> 0 Then

        MsgBox(Err.Description & Err.Number)

    End If

    objNetwork = Nothing

End Function



Public Sub pause(ByRef duration As Integer)
    Dim Current As Integer
    Current = VB.Timer()
    Do Until VB.Timer() - Current >= duration
        System.Windows.Forms.Application.DoEvents()
    Loop
End Sub

2 个解决方案

#1


1  

Yeah, you can merge the commands into a for loop by utilizing the DirectCast command which can convert strings to objects.

是的,您可以使用DirectCast命令将命令合并到for循环中,该命令可以将字符串转换为对象。

DirectCast MSDN

DirectCast MSDN

New Version - Updated to address VB.Net

新版本 - 更新为地址VB.Net

    For i = 1 To 18
        Dim txts, tObj : txts = "txtNAS" : Dim PBs, pObj : PBs = "ProgressBar" : Dim lbls, lObj : lObj = "Label"
        tObj = DirectCast(txts & i, TextBox) : pObj = DirectCast(PBs & i, ProgressBar) : lObj = DirectCast(lbls & i, Label)
        findsize(tObj.Text, pObj, lObj)
    Next

Old Version

旧版

Do

    findsize(txtNAS1.Text, ProgressBar1, Label1)

    findsize(txtNAS2.Text, ProgressBar2, Label2)

    findsize(txtNAS3.Text, ProgressBar3, Label3)

    findsize(txtNAS4.Text, ProgressBar4, Label4)

    findsize(txtNAS5.Text, ProgressBar5, Label5)

    findsize(txtNAS6.Text, ProgressBar6, Label6)

    findsize(txtNAS7.Text, ProgressBar7, Label7)

    findsize(txtNAS8.Text, ProgressBar8, Label8)

    findsize(txtNAS9.Text, ProgressBar9, Label9)

    findsize(txtNAS10.Text, ProgressBar10, Label10)

    findsize(txtNAS11.Text, ProgressBar11, Label11)

    findsize(txtNAS12.Text, ProgressBar12, Label12)

    findsize(txtNAS13.Text, ProgressBar13, Label13)

    findsize(txtNAS14.Text, ProgressBar14, Label14)

    findsize(txtNAS15.Text, ProgressBar15, Label15)

    findsize(txtNAS16.Text, ProgressBar16, Label16)

    findsize(txtNAS17.Text, ProgressBar17, Label17)

    findsize(txtNAS18.Text, ProgressBar18, Label18)

    pause(10)

Loop

End Sub

#2


0  

You can organize your UI slightly better to improve coding experience. Put a group of controls, for which you would later call findsize(...) under the same parent, a panel for example, or a groupbox, whichever makes more sense to you.

您可以稍微更好地组织UI,以改善编码体验。放置一组控件,稍后您可以在同一父控件,例如面板或组合框下调用findsize(...),无论哪个更有意义。

findsize(txtNAS1.Text, ProgressBar1, Label1)

The loop through all panels (you can create a custom class, which inherits from Panel, if you also have other panels with different behavior), and call findsize for Panel1.Controls.OfType(Of TextBox), same for progress bar and a label.

循环遍历所有面板(您可以创建一个自定义类,继承自Panel,如果您还有其他具有不同行为的面板),并为Panel1.Controls.OfType(Of TextBox)调用findsize,同样用于进度条和标签。

Let me know if you want more information on this approach.

如果您想了解有关此方法的更多信息,请与我们联系。

#1


1  

Yeah, you can merge the commands into a for loop by utilizing the DirectCast command which can convert strings to objects.

是的,您可以使用DirectCast命令将命令合并到for循环中,该命令可以将字符串转换为对象。

DirectCast MSDN

DirectCast MSDN

New Version - Updated to address VB.Net

新版本 - 更新为地址VB.Net

    For i = 1 To 18
        Dim txts, tObj : txts = "txtNAS" : Dim PBs, pObj : PBs = "ProgressBar" : Dim lbls, lObj : lObj = "Label"
        tObj = DirectCast(txts & i, TextBox) : pObj = DirectCast(PBs & i, ProgressBar) : lObj = DirectCast(lbls & i, Label)
        findsize(tObj.Text, pObj, lObj)
    Next

Old Version

旧版

Do

    findsize(txtNAS1.Text, ProgressBar1, Label1)

    findsize(txtNAS2.Text, ProgressBar2, Label2)

    findsize(txtNAS3.Text, ProgressBar3, Label3)

    findsize(txtNAS4.Text, ProgressBar4, Label4)

    findsize(txtNAS5.Text, ProgressBar5, Label5)

    findsize(txtNAS6.Text, ProgressBar6, Label6)

    findsize(txtNAS7.Text, ProgressBar7, Label7)

    findsize(txtNAS8.Text, ProgressBar8, Label8)

    findsize(txtNAS9.Text, ProgressBar9, Label9)

    findsize(txtNAS10.Text, ProgressBar10, Label10)

    findsize(txtNAS11.Text, ProgressBar11, Label11)

    findsize(txtNAS12.Text, ProgressBar12, Label12)

    findsize(txtNAS13.Text, ProgressBar13, Label13)

    findsize(txtNAS14.Text, ProgressBar14, Label14)

    findsize(txtNAS15.Text, ProgressBar15, Label15)

    findsize(txtNAS16.Text, ProgressBar16, Label16)

    findsize(txtNAS17.Text, ProgressBar17, Label17)

    findsize(txtNAS18.Text, ProgressBar18, Label18)

    pause(10)

Loop

End Sub

#2


0  

You can organize your UI slightly better to improve coding experience. Put a group of controls, for which you would later call findsize(...) under the same parent, a panel for example, or a groupbox, whichever makes more sense to you.

您可以稍微更好地组织UI,以改善编码体验。放置一组控件,稍后您可以在同一父控件,例如面板或组合框下调用findsize(...),无论哪个更有意义。

findsize(txtNAS1.Text, ProgressBar1, Label1)

The loop through all panels (you can create a custom class, which inherits from Panel, if you also have other panels with different behavior), and call findsize for Panel1.Controls.OfType(Of TextBox), same for progress bar and a label.

循环遍历所有面板(您可以创建一个自定义类,继承自Panel,如果您还有其他具有不同行为的面板),并为Panel1.Controls.OfType(Of TextBox)调用findsize,同样用于进度条和标签。

Let me know if you want more information on this approach.

如果您想了解有关此方法的更多信息,请与我们联系。