0

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
Hoesl
  • 13
  • 6
  • What is the content of the file that is passed to $test? – Karolina Ochlik Aug 19 '20 at 13:47
  • 1
    What is the content of all your vars? As posted the form would fail to render properly at all. AS for [y goal now is to remove or create new boxes, while the form.ShowDialog() was already used.], you clear the picture box and repopulate it, not the form. As far as [if there is another way of checking if new coordinates have been added to the txt-file or removed], you'd have to maintain version to compare then make a decision on those results. – postanote Aug 20 '20 at 00:25
  • @GrzegorzOchlik Strings, that contain a series of numbers, which two of them are the x and y coordinate for one of the red dots – Hoesl Aug 20 '20 at 05:40
  • @postanote What do you mean exactly? $img represents the red dot, while $img2 represents the background, which the red dots have to be displayed onto and $test contains the string, which give the coordinates for each dot – Hoesl Aug 20 '20 at 05:42
  • 1
    You had no explanation of what $path\$path2\test was, what was in them, etc. Leaving us to guess. Again, you should not have to close the form to update the picture box, as you should have to close it to update a textbox or a data grid or other element. Once I change those to an image, then ok, but the root of your question is that file and that answer is still the same, maintain a current and past and compare as needed the overwrite the current past with the recent change one. – postanote Aug 20 '20 at 05:51
  • Thanks, I realized I had some errors in my thought process, that kind of hindered everything and made me expect things work different than they do. Updating the Location of existing pictureBoxes seems to work for my goal. Thanks again – Hoesl Aug 20 '20 at 06:23

0 Answers0