user3459348
user3459348

Reputation: 1

visual studio - New textbox Array going to a function

I was wanting to create a function to update each textbox, progress bar and label text checking NAS storage 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.

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

Upvotes: 0

Views: 91

Answers (2)

Rich
Rich

Reputation: 4170

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

DirectCast MSDN

New Version - Updated to address 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

Upvotes: 1

Victor Zakharov
Victor Zakharov

Reputation: 26434

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.

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.

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

Upvotes: 0

Related Questions