Hello guys I'm currently coding an IHM in powershell to configure switches. I'm using a combobox to select a model. The combobx is generated using a dataview generated by a datasource, and the datasource is generated from a csv file by a foreach.
My problem is the following : I can only recover a single column.
Combobox:
$list1 = New-Object System.Windows.Forms.Combobox
$list1.Location = New-Object Drawing.Point 9,45
$list1.Size = New-Object System.Drawing.Size(250,30)
$list1.DropDownStyle = "DropDownList"
$list1.BindingContext = New-Object System.Windows.Forms.BindingContext
$list1.DataSource = $vu1
$list1.ValueMember = "Model"
$list1.DisplayMember = "Model"
$list1.SelectedValue = ""
$mainfrm.controls.add($list1)
Reading infos
Write-Host "$list1.SelectedValue"
Edit foreach and creation of the dataview
foreach:
$ImportData = import-csv "E:\PS\A faire\equipement.csv" -Delimiter ';' | Select Model,Type,Port,Firmware,Comware
$table1 = New-Object system.Data.DataTable
$colonne1 = New-Object system.Data.DataColumn Model,([string])
$table1.columns.add($colonne1)
$colonne2 = New-Object system.Data.DataColumn Port,([string])
$table1.columns.add($colonne2)
$colonne3 = New-Object system.Data.DataColumn Comware,([string])
$table1.columns.add($colonne3)
$colonne4 = New-Object system.Data.DataColumn Firmware,([string])
$table1.columns.add($colonne4)
foreach ($data in $ImportData)
{
$model = $data.Model
$type = $data.Type
$port = $data.Port
$firmware = $data.Firmware
$comware= $data.Comware
$ligne1 = $table1.NewRow()
$ligne1.Model = "$model "+"$type"
$ligne1.Port = "$port"
$ligne1.Comware = "$comware"
$ligne1.Firmware = "$firmware"
$table1.Rows.Add($ligne1)
}
Dataview
$vu1 = New-Object System.Data.DataView($table1)
Instead of using 4 colums in the datasource I put everything in one and then a split.
Code after :
$ligne1.Model = "$model "+"$type " + "$port " + "$comware " + "$firmware "
Then you can use a split() to have a usable value.
Related
I have created desktop application with generate qr code like below
I want make a script using batch script or powershell with the qrcode.exe based on value enter url and Qr Image Name using command line.the output should be QR Code Image too.
can try with this but first import the QRCodeGenerator before running the script
#Import-Module QRCodeGenerator
[string] $webname, [string] $url, [string] $output = ".\images\"
$webname = 'Google'
$url = 'https://www.google.com'
$outputfolder = $outputfolder + $webname +'.JPG'
New-PSOneQRCodeURI -URI $url -Width 15 -OutPath $output
and then the output result will be showing as JPG type.
Install-Module -Name QRCodeGenerator
Import-Module QRCodeGenerator
Add-Type -assembly System.Windows.Forms
$qr_base_form = New-Object System.Windows.Forms.Form
$qr_base_form.Height = 150
$qr_base_form.Width = 350
$qr_base_form.Text = "QR Code Generator"
$qr_base_form.AutoSize = $true
$qr_label_url = New-Object System.Windows.Forms.Label
$qr_label_url.Location = '10,10'
$qr_label_url.Size = '100,15'
$qr_label_url.Text = "URL:"
$qr_input_url = New-Object System.Windows.Forms.TextBox
$qr_input_url.Location = '10,30'
$qr_input_url.Size = '100,25'
$qr_label_name = New-Object System.Windows.Forms.Label
$qr_label_name.Location = '10,70'
$qr_label_name.Size = '100,15'
$qr_label_name.Text = "Name:"
$qr_input_name = New-Object System.Windows.Forms.TextBox
$qr_input_name.Location = '10,90'
$qr_input_name.Size = '100,25'
$qr_png_viewer = New-Object System.Windows.Forms.PictureBox
$qr_png_viewer.Image = $img
$qr_png_viewer.SizeMode = "Autosize"
$qr_png_viewer.Anchor = "Bottom, left"
$qr_png_viewer.Location = '150,10'
$qr_button_create = New-Object System.Windows.Forms.Button
$qr_button_create.Location = '150,150'
$qr_button_create.Size = '100,25'
$qr_button_create.Text = "Create Code"
$qr_button_create.Add_Click({
$path = "H:\LIVE\" + "$name"+ ".jpg"
$urllink = $qr_input_url.Text
$name = $qr_input_name.Text
New-PSOneQRCodeURI -URI "$urllink" -Width 15 -OutPath "$path"
$img = $path
})
$qr_base_form.Controls.Add($qr_label_url)
$qr_base_form.Controls.Add($qr_input_url)
$qr_base_form.Controls.Add($qr_label_name)
$qr_base_form.Controls.Add($qr_input_name)
$qr_base_form.Controls.Add($qr_png_viewer)
$qr_base_form.Controls.Add($qr_button_create)
$qr_base_form.ShowDialog()
This code should work for you. Just try changing the variables and try running it. GUI is also included.
I need help with my winform script including a listview. I have 6 items and I try to show them in a specific column.
For example, I have 6 items from $a to $f and 6 columns (1 to 6) in my listview. I need to show each of them in specific one:
($a in column 1)
($b in column 2)
($c in column 3)
...etc
Actually, all items are output in the same column, and I really don't understand what I'm doing wrong, please help !
Here's an example of my code:
Form = New-Object Windows.Forms.Form
$Form.Text = "New Test"
$Form.Width = 1550
$Form.Height = 800
$Listview = New-Object System.Windows.Forms.ListView
$Listview.Location = New-Object System.Drawing.Size(15,10)
$Listview.Size = New-Object System.Drawing.Size(550,10)
$Listview.AutoResizeColumns([System.Windows.Forms.ColumnHeaderAutoResizeStyle]::ColumnContent)
$Listview.View = "Details"
$Listview.FullRowSelect = $true
$Listview.GridLines = $true
$Listview.Height = 650
$Listview.Width =1500
$Listview.AllowColumnReorder = $true
$Listview.Sorting = [System.Windows.Forms.SortOrder]::None
[void]$Listview.Columns.Add('Date',150)
[void]$Listview.Columns.Add('Auteur',150)
[void]$Listview.Columns.Add('Tache',500)
[void]$Listview.Columns.Add('Status',100)
[void]$Listview.Columns.Add('Assigné',150)
[void]$Listview.Columns.Add('Commentaire',500)
$oButton = New-Object Windows.Forms.Button
$oButton.Text = "List"
$oButton.Top = 700
$oButton.Left = 350
$oButton.Width = 150
$oButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
$oButton.add_click({
$a3 = Get-Date
$b3 = Get-Date
$c3 = Get-Date
$d3 = Get-Date
$e3 = Get-Date
$f3 = Get-Date
$Entry1 = New-Object System.Windows.Forms.ListViewItem($_.Date)
$Entry2 = New-Object System.Windows.Forms.ListViewItem($_.Auteur)
$Entry3 = New-Object System.Windows.Forms.ListViewItem($_.Tache)
$Entry4 = New-Object System.Windows.Forms.ListViewItem($_.Status)
$Entry5 = New-Object System.Windows.Forms.ListViewItem($_.Assigné)
$Entry6 = New-Object System.Windows.Forms.ListViewItem($_.Commentaire)
[void]$Listview.Items.Add($Entry1)
[void]$Entry1.SubItems.Add($a3.ToString())
[void]$Listview.Items.Add($Entry2)
[void]$Entry2.SubItems.Add($b3.ToString())
[void]$Listview.Items.Add($Entry3)
[void]$Entry3.SubItems.Add($c3.ToString())
[void]$Listview.Items.Add($Entry4)
[void]$Entry4.SubItems.Add($d3.ToString())
[void]$Listview.Items.Add($Entry5)
[void]$Entry5.SubItems.Add($e3.ToString())
[void]$Listview.Items.Add($Entry6)
[void]$Entry6.SubItems.Add($f3.ToString())
})
$Form.Add_Shown({$Form.Activate()})
$Form.controls.add($oButton)
$Form.controls.add($Listview)
$Form.ShowDialog()
To add a row to the listview, just add the data with '=' to the entry's Text-property. This will go to the first column.
To add to the other columns, you have to put them in subitems. Unfortunelty, after testing and searching, I found that you can't have a linebreak in the text of a listviewitem. To have linebreaks, you will have to switch to a DataGridView.
Below is a sample of how to add to the ListView. This have been tested in your code, and it works
[void]$Listview.Items.Add($Entry1)
$Entry1.Text = $a3.ToString()
[void]$Entry1.SubItems.Add("Claudé")
[void]$Entry1.SubItems.Add("Shop")
[void]$Entry1.SubItems.Add("Done")
[void]$Entry1.SubItems.Add($a3.AddDays(-3).ToString())
[void]$Entry1.SubItems.Add("Need more cake")
So I'm trying to insert red dots into some kind of map, which I do with creating new pictureboxes after reading their coordinates out of an txt-file.
My goal now is to remove or create new boxes, while the form.ShowDialog() was already used.
I found a way of closing the whole form and running everything again, which kind of works but is very ugly in my opinion. Was wondering if there is another way of checking if new coordinates have been added to the txt-file or removed and if that is the case, creating or removing the corresponding boxes.
(I tried .Refresh but that seems to do nothing at all)
function MakeForm {
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form
$form.Text = "Image Viewer"
$form.Width = 270;
$form.Height = 270;
$path = (Get-Item "Insert Path here")
$path2 = (Get-Item "Insert Path here")
$img = [System.Drawing.Image]::Fromfile($path);
$img2 = [System.Drawing.Image]::FromFile($path2);
$test = Get-Content -Path "Insert Path here"
$test
$pictureBoxes = New-Object 'System.Collections.Generic.List[Windows.Forms.PictureBox]'
$i=1
Foreach ($Line in $test){
if ($Line -like "*in 2*"){
$i++
$Split1= $Line.Split(" ")
$x=$Split1[5]
$y=$Split1[6]
$pictureBox = New-Object Windows.Forms.PictureBox
$pictureBox.Width = $img.Size.Width;
$pictureBox.Height = $img.Size.Height;
$pictureBox.Location = New-object System.Drawing.Size($x,$y)
$pictureBox.Image = $img;
$form.Controls.Add($pictureBox)
$pictureBoxes.Add($pictureBox)
Write-Host $pictureBoxes[$i]
$form.Add_Shown( { $form.Activate() } )
}
}
$pictureBox20 = New-Object Windows.Forms.PictureBox
$pictureBox20.Width = $img2.Size.Width;
$pictureBox20.Height = $img2.Size.Height;
$pictureBox20.Image = $img2;
$form.Controls.Add($pictureBox20)
$button1 = New-Object System.Windows.Forms.Button
$button1.Width=25
$button1.Height=223
$button1.Location = New-Object System.Drawing.Point(223,0)
$form.Controls.Add($button1)
$button1.Add_Click({
#Button for removing a box for testing purposes
$form.Controls.Remove($pictureBoxes[3])
})
$button2 = New-Object System.Windows.Forms.Button
$button2.Width=25
$button2.Height=223
$button2.Location = New-Object System.Drawing.Point(260,0)
$form.Controls.Add($button2)
$button2.Add_Click({
#This Button should refresh the whole form, if possible without doing everything again
$form.Close()
$form.Dispose()
MakeForm
})
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
}
MakeForm
In order to shift this output table over, I had to create an empty column which puts hyphens where the blank space should be (see picture). I'd like to have the same output as the picture, but without the hyphens, in other words an empty column only.
How can I offset this table, without the need for an empty column?
My code:
$outputTable = New-Object system.Data.DataTable “$TableName”
#servername totaldbs activedbs passivedbs preferencecountlist mounteddbs dismounteddbs dagname
$col0 = New-Object system.Data.DataColumn " ",([string])
$col1 = New-Object system.Data.DataColumn ServerName,([string])
$col2 = New-Object system.Data.DataColumn TotalDbs,([string])
$col3 = New-Object system.Data.DataColumn ActiveDbs,([string])
$col4 = New-Object system.Data.DataColumn PassiveDbs,([string])
$col5 = New-Object system.Data.DataColumn PreferenceCountList,([string])
$col6 = New-Object system.Data.DataColumn MountedDbs,([string])
$col7 = New-Object system.Data.DataColumn DismountedDbs,([string])
$col8 = New-Object system.Data.DataColumn DagName,([string])
$outputTable.columns.add($col0)
$outputTable.columns.add($col1)
$outputTable.columns.add($col2)
$outputTable.columns.add($col3)
$outputTable.columns.add($col4)
$outputTable.columns.add($col5)
$outputTable.columns.add($col6)
$outputTable.columns.add($col7)
$outputTable.columns.add($col8)
foreach($result in $results)
{
[string]$resultSplit = $result;
$resultSplit1 = $resultSplit.Split("|"); # -split "|";
$row = $outputTable.NewRow()
if ($resultSplit1[6] -ne "")
{
$temp6 = $resultSplit1[6];
}
else
{
$temp6 = "0";
}
$temp7 = $resultSplit1[7];
$row.ServerName = $resultSplit1[0];
$row.TotalDbs = $resultSplit1[1];
$row.ActiveDbs = $resultSplit1[2];
$row.PassiveDbs = $resultSplit1[3];
$row.PreferenceCountList = $resultSplit1[4];
$row.MountedDbs = $resultSplit1[5];
if ($resultSplit1[6] -ne "")
{
$row.DismountedDbs = $resultSplit1[6];
}
else
{
$row.DismountedDbs = "0";
}
$row.DagName = $resultSplit1[7];
$outputTable.Rows.Add($row);
}
$outputTable | format-table -AutoSize
($outputTable | format-table -AutoSize | Out-String) -Split '[\r\n]+' | ForEach {" $_"}
Or using .Replace:
($outputTable | format-table -AutoSize | Out-String) -Replace '[\r\n]+', "`r`n "
can anyone show me an example on how to add header check box in a datagridview with powershell? I am searching on the web with similar example but i can find one. I am trying to create a select all checkbox
Your help is very much appreciated. Thanks.
$form = New-Object system.Windows.Forms.Form
$dataGrid1 = New-Object System.Windows.Forms.DataGridView
$dataGrid1.Location = New-Object Drawing.Point 20,90
$dataGrid1.size = New-Object Drawing.Point 260,200
$dataGrid1.MultiSelect = $false
$dataGrid1.ColumnHeadersVisible = $true
$dataGrid1.RowHeadersVisible = $false
$datagridviewcheckboxcolumn1 = New-Object 'System.Windows.Forms.DataGridViewCheckBoxColumn'
$dataGrid1.Columns.Add($datagridviewcheckboxcolumn1) | Out-Null
$form.Controls.Add($dataGrid1)
$form.ShowDialog()
Something like this?
$datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
$Col1 = New-Object 'System.Windows.Forms.DataGridViewCheckBoxColumn'
# datagridview1
$datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
[void]$datagridview1.Columns.Add($Col1)
$datagridview1.Dock = 'Fill'
$datagridview1.Location = '0, 0'
$datagridview1.Name = "datagridview1"
$datagridview1.Size = '284, 262'
$datagridview1.TabIndex = 0
# Col1
$Col1.HeaderText = "Add Header Here"
$Col1.Name = "Add Name Here"`