none
Powershell and Forms RRS feed

  • Question

  • I need some help. I am creating a form that emurates all the drives with some input boxes so items can be change such as lables. It disaplys on the form correctly looks nice but I have don't have any idea how to get repeaded data like that on a form into an array so that I can reconfigure the drives.  Any sugestions would be great.

    I did find out that adding  "$objArray += $objDriveLetter" at the end seems to capture the input boxes but it is not in a useful format.

    Function DiskDrives{

        $RowSize = 100
        $RowSize = 0
        $global:i = 0
        $Drives = @("C","D","L","I","T","U")
        $DriveLabels = @("","DATA","LOGS","INSTANCE","TEMP","BACKUP")
        $objArray = @()
        $x = @()
        $global:QUIT = $false
        #Get Drives
        $Disks = get-disk | sort Number


        Add-Type -AssemblyName System.Windows.Forms
        $Form = New-Object system.Windows.Forms.Form
        $Form.Text = "Drive layout Specs"
        $Form.Size = New-Object System.Drawing.Size(350,350)
        $Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::Italic)
        # Font styles are: Regular, Bold, Italic, Underline, Strikeout
        $Form.Font = $Font

        #Title
        $Label = New-Object System.Windows.Forms.Label
        $Label.Text = "Number     Letter        Label               Size"
        $Label.AutoSize = $True

        Foreach($Disk in $Disks){
            $global:i += 1
            $RowSize += 30

            #Add Drive Number box
            $DriveNum = New-Object System.Windows.Forms.Label
            $DriveNum.Location = New-Object System.Drawing.Size(30,$RowSize)
            $DriveNum.Text = $Disk.Number
            $DriveNum.AutoSize = $True
            $Form.Controls.Add($DriveNum)
            $objArray += $DriveNum

            #Add Drive letter box
            $objDriveLetter = New-Object System.Windows.Forms.TextBox
            $objDriveLetter.Location = New-Object System.Drawing.Size(80,$RowSize)
            $objDriveLetter.Size = New-Object System.Drawing.Size(30,20)
            $objDriveLetter.Text = $Drives[$global:i-1]
            $Form.Controls.Add($objDriveLetter)
            $Form.Topmost = $True
            $objArray += $objDriveLetter

            #Add Drive Label
            $objDriveLabel = New-Object System.Windows.Forms.TextBox
            $objDriveLabel.Location = New-Object System.Drawing.Size(120,$RowSize)
            $objDriveLabel.Size = New-Object System.Drawing.Size(90,20)
            $objDriveLabel.Text = $DriveLabels[$global:i-1]
            $Form.Controls.Add($objDriveLabel)
            $Form.Topmost = $True
            $objArray += $objDriveLabel

            #Add Drive Size
            $DriveSize = New-Object System.Windows.Forms.Label
            $DriveSize.Location = New-Object System.Drawing.Size(235,$RowSize)
            $DriveSize.Text = $Disk.Size / 1GB
            $Form.Controls.Add($DriveSize)
            $objArray += $DriveSize
    }
        $RowSize += 50
        $SubmitButton = New-Object System.Windows.Forms.Button
        $SubmitButton.Location = New-Object System.Drawing.Size(60,$RowSize)
        $SubmitButton.Size = New-Object System.Drawing.Size(75,23)
        $SubmitButton.Text = "Submit"
        $SubmitButton.Add_Click({$Form.Close()})
        $Form.Controls.Add($SubmitButton)

        $CancelButton = New-Object System.Windows.Forms.Button
        $CancelButton.Location = New-Object System.Drawing.Size(150,$RowSize)
        $CancelButton.Size = New-Object System.Drawing.Size(75,23)
        $CancelButton.Text = "Cancel"
        $CancelButton.Add_Click({$global:QUIT = $true;$Form.Close()})
        $Form.Controls.Add($CancelButton)

        Form.Controls.Add($Label)
        $Form.ShowDialog()


        if($global:QUIT -eq $true){Exit}

    }

    • Moved by Bill_Stewart Friday, November 20, 2015 7:27 PM This is not "teach me all details of Windows forms" forum
    Monday, August 17, 2015 7:03 PM

All replies

  • Use a DatagridView to edit tables of items.


    \_(ツ)_/

    Monday, August 17, 2015 8:40 PM
  • Hi Edycus,

    Didn't get what is your requirement here.

    There is a missing $ onlast 3rd line  $Form.Controls.Add($Label)

    Try these.

    Creating a Custom Input Box

    https://technet.microsoft.com/en-us/library/dn792463.aspx

    https://technet.microsoft.com/en-us/library/ff730941.aspx


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.



    • Edited by Satyajit321 Thursday, August 20, 2015 12:29 PM
    Thursday, August 20, 2015 12:05 PM
  • Hi Edycus,

    Guess I got what you are saying now.

    Basically whats wrong in your code is "$objArray +=" is creating new object for each attribute of each instance.

    Its like single column having different datatypes, hence when you extract the output is jumbled and complex.

    Instead of creating a new object everytime, create attributes for the fields and increament by one object per drive found in the loop.

    Check this sample script without your forms cluttering the code:

    It creates a object template which you can use to create new PSObject and assign the values for drive properties that you like. Once done add it to an array and increment the array count. Repeat the step by creating a new PSObject from Template.

    #DriveInfoTemplate
    $DriveIT = New-Object –TypeName PSObject
    
    
    $para1 = "Letter"
    $para2 = "Label"
    $para3 = "Size"
    
    $DriveIT | Add-Member –MemberType NoteProperty –Name $para1 -Value $null
    $DriveIT | Add-Member –MemberType NoteProperty –Name $para2 -Value $null
    $DriveIT | Add-Member –MemberType NoteProperty –Name $para3 -Value $null
    
    #Creating Blank array for holding the result
    $objResult = @()
    
    
    
    1..4 | foreach{
    
    #Getting the new values using some code
    $value1 = "C:"
    $value2 = "OSDrive-$_"
    $value3 = 255*$_
    
    #Creating new object for every item using template
    $objTemp = $DriveIT | Select-Object *
    
    #Assigning values to the object
     $objTemp.$para1 = $value1
     $objTemp.$para2 = $value2
     $objTemp.$para3 = $value3
    
    #Assigning the obj into the array and incrementing
    $objResult += $objTemp
    }
    
    $objResult | ft -AutoSize

    Output:

    Note that the array we have is a PSCUstomObject

    PS > PSObjectTemplate.ps1
    
    Letter Label     Size
    ------ -----     ----
    C:     OSDrive-1  255
    C:     OSDrive-2  510
    C:     OSDrive-3  765
    C:     OSDrive-4 1020
    
    
    
    
       TypeName: Selected.System.Management.Automation.PSCustomObject
    
    Name   MemberType   Definition                   
    ----   ----------   ----------                   
    Label  NoteProperty System.String Label=OSDrive-1
    Letter NoteProperty System.String Letter=C:      
    Size   NoteProperty System.Int32 Size=255        


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Friday, August 21, 2015 4:53 AM
    Friday, August 21, 2015 4:11 AM
  • Get-Drive is not associated with a drive letter. You want to get the volume information.

    \_(ツ)_/

    Friday, August 21, 2015 4:28 AM
  • Here is how to build a form:

    Function DiskDrives {
    	$Disks = Get-Volume | select driveletter, Filesystemlabel, drivetype, size
    	
    	Add-Type -AssemblyName System.Windows.Forms
    	$Form = New-Object system.Windows.Forms.Form
    	$Form.Text = 'Drive layout Specs'
    	$Form.Size = '650,400'
    	#$Form.Font=New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::Italic)
    	
    	$datagridview = New-Object System.Windows.Forms.DataGridView
    	$Form.Controls.Add($datagridview)
    	$datagridview.Anchor='top,left,right,bottom'
    	$datagridview.Location = '10,10'
    	$datagridview.Size = '600,280'
    	$datagridview.DataSource = [collections.arraylist]$Disks
    	
    	$SubmitButton = New-Object System.Windows.Forms.Button
    	$Form.Controls.Add($SubmitButton)
    	$SubmitButton.Anchor = 'bottom,right'
    	$SubmitButton.Location='470,300'
    	$SubmitButton.Size = '75,23'
    	$SubmitButton.Text = 'Submit'
    	$SubmitButton.DialogResult = 'Ok'
    	
    	$CancelButton = New-Object System.Windows.Forms.Button
    	$Form.Controls.Add($CancelButton)
    	$CancelButton.Anchor = 'bottom,right'
    	$CancelButton.Location = '545,300'
    	$CancelButton.Size = '75,23'
    	$CancelButton.Text = 'Cancel'
    	$CancelButton.DialogResult = 'Cancel'
    	
    	$Form.ShowDialog()
    }
    DiskDrives
    

    This can be used to create tables of most kinds of data.  THe contents of the grid can be exported to CDSV or file and to a database very easily.


    \_(ツ)_/

    Friday, August 21, 2015 5:27 AM