-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv-gui-test_3.ps1
110 lines (80 loc) · 3.64 KB
/
csv-gui-test_3.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
[reflection.assembly]::load("System.Windows.Forms") | Out-Null
[reflection.assembly]::load("System.Drawing") | Out-Null
# This block of code is a file dialog open box
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
# This uses Environmental Directory Settings such as Desktop
# InitialDirectory = [Environment]::GetFolderPath('Desktop')
# Here you can pick the default directory. If you comment it out,
# then OpenFileDialog seems to go to the last directory you chose a file from
InitialDirectory = "C:\temp\Ricoh"
# Pick file extensions to chose from. The first is default.
Filter = 'Comma Separated Values (*.csv)|*.csv|All Files (*.*)|*.*|SpreadSheet (*.xlsx)|*.xlsx'
}
# The actual dialog open box
$dialogOpen = $FileBrowser.ShowDialog()
# This returns the full path and filename
$pathFileName = $FileBrowser.FileName
# Write-Host $pathFileName
# This returns the filename only
$fileName = $FileBrowser.SafeFileName
# Write-Host $fileName
$OnLoadForm_UpdateGrid= {
# This returns the filename minus extension.
$baseName = Get-Item $pathFileName | Select-Object -ExpandProperty BaseName
# Write-Host $baseName
$tmp = $FileBrowser.InitialDirectory + '\' + $baseName + '.tmp'
Write-Host $tmp
#Make a copy of the file so we can import it and leave the real file free for exporting to
Copy-Item $pathFileName -Destination $tmp
# Load the tempfile into memory so we can work
$tmpFileName = Import-Csv $tmp
#Remove the tempfile now
Remove-Item $tmp
#Select the datasource so we can prep for the dataGridView
$dataGridView1.DataSource=[System.Collections.ArrayList]$tmpFileName
$form.refresh()
}
# This button will save the file
$button1_OnClick= {
$dataGridView1.Rows |Select -Expand DataBoundItem | Export-Csv $pathFileName -NoType
}
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Form Text Goes Here"
$Form.TopMost = $true
$form.KeyPreview = $true
$form.StartPosition = "centerscreen"
$form.FormBorderStyle = 'Fixed3D'
#None, FixedSingle, Fixed3D, FixedDialog, Sizable, FixedToolWindow, SizableToolWindow
$dataGridView1 = New-Object System.Windows.Forms.DataGridView -Property @{
}
$sds_width = 900
$sds_height = 450
$form.Size = New-Object System.Drawing.Size($sds_width,$sds_height)
$form.MinimumSize= New-Object System.Drawing.Size($sds_width,$sds_height)
$form.MaximumSize= New-Object System.Drawing.Size($sds_width,$sds_height)
$form.FormBorderStyle = 'Sizable'
$form.AutoScroll = $true
$dataGridView1.Size = New-Object System.Drawing.Size(($sds_width - 25),($sds_height - 100))
# This is the same feature as AutoFit in Excel
$dataGridView1.AutoSizeColumnsMode = 'DisplayedCells'
# None, ColumnHeader, AllCellsExceptHeader, AllCells, DisplayedCellsExceptHeader, DisplayedCells, Fill
$dataGridViewColumn1 = New-Object System.Windows.Forms.DataGridViewColumn
$dataGridView1.Name = $baseName
$dataGridView1.DataMember = ""
$dataGridView1.TabIndex = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 5
$System_Drawing_Point.Y = 5
$dataGridView1.Location = $System_Drawing_Point
$form.Controls.Add($dataGridView1)
$form.add_Load($OnLoadForm_UpdateGrid)
$button = New-Object Windows.Forms.Button
$button.text = "Save"
$button.Location = New-Object Drawing.Point(5,($dataGridView1.height + 25))
$button.Size = New-Object Drawing.Point(125, 25)
$button.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
$button.TabIndex ="1"
$button.add_Click($button1_OnClick)
$form.controls.add($button)
$form.ShowDialog()
$form.Dispose()