Question Problème GUI

Plus d'informations
il y a 8 ans 11 mois #23476 par Julien B.
Problème GUI a été créé par Julien B.
Hi EveryOne. it's my first post !

Bon voila je commence tout juste le powershell (avec les cours) et on a un exercice. qui consiste à télécharger et afficher une image depuis un URL ainsi que le upvote. en complément on doit avoir un bouton qui ferme la fenêtre (ça ok) et un bouton qui rafraîchis l'image et les upvotes. et la je coince.
J'ai crée une form qui affiche bien ces éléments quand je click sur rafraîchir ça m'ouvre un popup mais j'arrive pas à gérer une fois dedans.

J'ai une erreur


Impossible d’appeler une méthode dans une expression Null.
Au caractère C:\Users\Julien\Desktop\Tp Graphique V2.ps1:117 : 1
+ $Buttonpop.Add_Click({$Popup.close()})
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull


Ma question quelqu'un pourrait m'orienté sur l'algo pour rafraîchir ?
et pour ajouter un peu de fun, j'aimerais que quand je clique sur un bouton dans mon popup ça ouvre un deuxième popup mais ça modifie le test du bouton du premier popup.

[code:1]
#####Modules######
Import-Module BitsTransfer

#####Variables######
$User=$env:username
$Url=\"9gag.com/gag/aLDnNMz\";
$UrlContent = Invoke-WebRequest -URI $URL
$UrlPix = ((Invoke-WebRequest $Url).AllElements | Where{$_.TagName -eq \"img\"} | Where{$_.class -eq \"badge-item-img\"}).src
$Upvote = ((($UrlContent.links | where-object {$_.innerText -match \"points\"}).InnerText).split(\" \"«»))[1]
Start-BitsTransfer -Source $UrlPix -Destination C:\Temp\9gag.jpg
$PixFile = \"c:\temp\9gag.jpg\"

#####Functions######

function Button_Refresh {
$Upvote = ((($UrlContent.links | where-object {$_.innerText -match \"points\"}).InnerText).split(\" \"«»))[1]
$LabelUpvote = New-Object System.Windows.Forms.Label
$LabelUpvote.Location = New-Object System.Drawing.Size(360,460)
$LabelUpvote.Size = New-Object System.Drawing.Size (75,30)
$LabelUpvote.TextAlign = \"MiddleCenter\"
$LabelUpvote.Forecolor = \"Green\"
$LabelUpvote.Text = \"$Upvote upvotes\"
$form1.Controls.Add($LabelUpvote)
}

function GenerateForm {
[void][reflection.assembly]::loadwithpartialname(\"System.Windows.Forms\"«»)

#Check temporary files
If(-not(Test-Path \"c:\temp\"«»)){
mkdir C:\temp
}
cd c:\temp

#Create the picture

$Pix = [System.Drawing.Image]::Fromfile($PixFile)
#Copy-item $Pix -Destination \"c:\temp\9gag.jpg\"

#Build form
$form1 = New-Object System.Windows.Forms.Form
$form1.Text =\" Hello $User\"
$form1.StartPosition = 4
$Width=$Pix.Size.Width
$form1.Width = $Width
$Height=$Pix.Size.Height + 180
$form1.Height = $Height

#Add Picture
$PictureBox = new-object Windows.Forms.PictureBox
$PictureBox.Width = $Pix.Size.Width
$PictureBox.Height = $Pix.Size.Height
$PictureBox.Image = $Pix
$form1.Controls.add($PictureBox)

#Add Close_Button
$CloseButton = New-Object System.windows.Forms.Button
$CloseButton.Location = New-Object System.Drawing.Size(305,500)
$CloseButton.Size = New-Object System.Drawing.Size(75,30)
$CloseButton.Forecolor = \"Blue\"
$CloseButton.Text = \"Close\"
$CloseButton.Add_Click({$form1.Close()})
$form1.Controls.Add($CloseButton)

#Add Label_Upvote
$LabelUpvote = New-Object System.Windows.Forms.Label
$LabelUpvote.Location = New-Object System.Drawing.Size(360,460)
$LabelUpvote.Size = New-Object System.Drawing.Size (75,30)
$LabelUpvote.TextAlign = \"MiddleCenter\"
$LabelUpvote.Forecolor = \"red\"
$LabelUpvote.Text = \"$Upvote upvotes\"
$form1.Controls.Add($LabelUpvote)

#Add Refresh_Button
$RefreshButton = New-Object System.windows.Forms.Button
$RefreshButton.Location = New-Object System.Drawing.Size(250,460)
$RefreshButton.Size = New-Object System.Drawing.Size(75,30)
$RefreshButton.Font = \"Italic\"
$RefreshButton.Text = \"Refresh me\"

##Popup refresh##
$RefreshButton.Add_Click({
$Popup = New-Object System.Windows.Forms.Form
$Popup.Size = \"260,180\"
$Popup.Text=\"Confirm\"
$Popup.StartPosition = \"CenterScreen\"
$Popup.Visible = $True

$Buttonpop = New-Object System.Windows.Forms.Button
$Buttonpop.Text = \"this windows is useless you can close it\"
$Buttonpop.AutoSize = $True
$Buttonpop.Location = \"15,60\"

###Popup2###

$Buttonpop.Add_Click({
$Popup2 = New-Object System.Windows.Forms.Form
$Popup2.Size = \"260,180\"
$Popup2.Text=\"Easy\"
$Popup2.StartPosition = \"CenterScreen\"
$Popup2.Visible = $true

$LabelPopUp = New-Object System.Windows.Forms.Label
$LabelPopUp.Size = \"240,160\"
$LabelPopUp.TextAlign = \"MiddleCenter\"
$LabelPopUp.Text = \"Its too easy to teasing you... Click on cross \"

$Buttonpop.Add_Click({$Popup.close()})
$Popup2.Controls.Add($LabelPopUp)

})

$Popup.Controls.Add($Buttonpop)
})

$form1.Controls.Add($RefreshButton)

#Show the form
$form1.ShowDialog()
}

#####Main#####

generateform
[/code:1]

Voila merci de l'aide !<br><br>Message édité par: Flyeur, à: 15/04/17 18:59

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 11 mois #23477 par Laurent Dardenne
Réponse de Laurent Dardenne sur le sujet Re:Problème GUI
Salut,
si je me souviens bien, il faut pr\&quot;ciser la portée script: dans l'eventhandler :
[code:1]
$Buttonpop.Add_Click({$script:«»Popup.close()})
[/code:1]

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 11 mois #23478 par Laurent Dardenne
Réponse de Laurent Dardenne sur le sujet Re:Problème GUI
Plus d'infos .

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 10 mois #23502 par Julien B.
Réponse de Julien B. sur le sujet Re:Problème GUI
désolé pour la réponse tardive.
J'ai essayé et ca a fonctionner merci !

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.050 secondes
Propulsé par Kunena