Question
Windows Forms
- dcolleville
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
- Messages : 4
- Remerciements reçus 0
Dans mon boulot, je dois créer une interface qui permet aux utilisateurs d'installer eux-même leur imprimante réseau. Avec cette interface, ils n'ont pas à se poser de question pour savoir quelle est le nom de l'imprimante, quel est le serveur,...
Je développe sur un Windows 7 x64 avec 8 Go de RAM, pour des PC client en win 7 x86 avec 4 Go de RAM.
J'ai choisi d'utiliser les Windows Forms de Powershell pour faire une interface conviviale.
Principe simple:
- une fenêtre avec un champs de texte libre (pour le numéro d'imprimante)
- 2 bouton radio \"oui\" et \"non\" pour savoir si l'imprimante installée est par défaut
- 2 boutons \"OK\" et \"Annuler\", pour valider l'installation ou annuler...
J'ai quelques problèmes dont ceux là:
1) je veux que lorsqu'on appuie sur \"OK\" le traitement se fasse (test, installation de l'imprimante, par défaut ?) et une fois fini, la fenêtre se ferme
BEN ça marche pas...
Le fenêtre se ferme, mais le process powershell.exe tourne en tache de fond et plante la bécane
2) les files d'impression ont des noms assez différentes: IMP-xxxx, IMP-xxxx-X64, IMPxxxx ... . Le seul truc qu'elles ont en commun, c'est le numéro d'imprimante.
Comment tester la bonne file d'impression sans que cela ralentisse le déroulement ?
c'est tout pour l'instant !
Ah oui, le code:
clear
#Generated Form Function
function GenerateForm {
##########################################################################
# #
# Initialisation des Formes #
# #
##########################################################################
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname(\"System.Windows.Forms\") | Out-Null
[reflection.assembly]::loadwithpartialname(\"System.Drawing\") | Out-Null
#endregion
#region Generated Form Objects
# Forme Principale
$InstallationImprimanteReseau = New-Object System.Windows.Forms.Form
$BoutonRadio_Non = New-Object System.Windows.Forms.RadioButton
$BoutonRadio_Oui = New-Object System.Windows.Forms.RadioButton
$Label_Defaut = New-Object System.Windows.Forms.Label
$Label_NumImprimante = New-Object System.Windows.Forms.Label
$Champs_NumImprimante = New-Object System.Windows.Forms.TextBox
$Bouton_Annuler = New-Object System.Windows.Forms.Button
$Bouton_OK = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
# Forme Message d'erreur Mauvais Numéro d'Imprimante
$ErreurNumImprimante = New-Object System.Windows.Forms.Form
$ErreurNumImprimante_Label_MauvaisNumero = New-Object System.Windows.Forms.Label
$ErreurNumImprimante_Bouton_OK = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
##########################################################################
# #
# Initialisation Variables #
# #
##########################################################################
#Tableau des imprimantes installées sur le PC
$tab = (Get-WmiObject -Class Win32_Printer).ShareName
##########################################################################
# #
# Gestion des Evenements #
# #
##########################################################################
#
# Forme Principale
#
$Bouton_OK_OnClick=
{
#Variable pour récuperer la saisie du champs TextBox
$Num=$Champs_NumImprimante.get_text()
#$test=0
#$error.Clear()
Switch -Regex ($Num)
{
\"[a-zA-Z]\" {$test=0}
\"15\d{3}\" {$test=1}
\"16\d{3}\" {$test=1}
\"17\d{3}\" {$test=1}
\"18\d{3}\" {$test=1}
\"30\d{3}\" {$test=1}
default{$test=0}
}
#clear
#Write-Host 'Num ='$Num
#Write-Host 'Test= '$Test
If ($test -match 1)
{
#Tableau des noms d'imprimantes possibles
$PrinterName = \"\\impression-srv1\IMP-$Num\",\"\\impression-srv1\IMP-$Num-X64\"
Write-Host $PrinterName[0]
Write-Host $PrinterName[1]
# comment tester les differents noms d'imprimante ?
#Installation Imprimante 1
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection($PrinterName[0])
# ici la fenêtre se ferme mais le process reste en cours de fonctionnement
$InstallationImprimanteReseau.Close()
}
Else
{
# $ErreurNumImprimante.ShowDialog()
}
}
$handler_label2_Click= {}
$Bouton_Annuler_OnClick=
{
$InstallationImprimanteReseau.Close()
}
#
# Forme Fenetre d'erreur Mauvais Numero d'Imprimante
#
$ErreurNumImprimante_Bouton_OK_OnClick=
{
$ErreurNumImprimante.Close()
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$ErreurNumImprimante.WindowState = $InitialFormWindowState
}
##########################################################################
# #
# Definition / Création des Objets Formes #
# #
##########################################################################
#region Generated Form Code
#
# Fenetre Principale
#
$InstallationImprimanteReseau.CausesValidation = $False
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 203
$System_Drawing_Size.Width = 444
$InstallationImprimanteReseau.ClientSize = $System_Drawing_Size
$InstallationImprimanteReseau.DataBindings.DefaultDataSourceUpdateMode = 0
$InstallationImprimanteReseau.Name = \"InstallationImprimanteReseau\"
$InstallationImprimanteReseau.Text = \"Installation Imprimante Réseau\"
#
# Bouton Radio \"NON\"
#
$BoutonRadio_Non.Checked = $True
$BoutonRadio_Non.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 270
$System_Drawing_Point.Y = 70
$BoutonRadio_Non.Location = $System_Drawing_Point
$BoutonRadio_Non.Name = \"BoutonRadio_Non\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.Width = 55
$BoutonRadio_Non.Size = $System_Drawing_Size
$BoutonRadio_Non.TabIndex = 2
$BoutonRadio_Non.TabStop = $True
$BoutonRadio_Non.Text = \"Non\"
$BoutonRadio_Non.UseVisualStyleBackColor = $True
$InstallationImprimanteReseau.Controls.Add($BoutonRadio_Non)
#
# Bouton Radio \"OUI\"
#
$BoutonRadio_Oui.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 195
$System_Drawing_Point.Y = 70
$BoutonRadio_Oui.Location = $System_Drawing_Point
$BoutonRadio_Oui.Name = \"BoutonRadio_Oui\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.Width = 55
$BoutonRadio_Oui.Size = $System_Drawing_Size
$BoutonRadio_Oui.TabIndex = 1
$BoutonRadio_Oui.TabStop = $True
$BoutonRadio_Oui.Text = \"Oui\"
$BoutonRadio_Oui.UseVisualStyleBackColor = $True
$InstallationImprimanteReseau.Controls.Add($BoutonRadio_Oui)
#
# Label \"Par Defaut\"
#
$Label_Defaut.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 100
$System_Drawing_Point.Y = 75
$Label_Defaut.Location = $System_Drawing_Point
$Label_Defaut.Name = \"Label_Defaut\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.Width = 70
$Label_Defaut.Size = $System_Drawing_Size
$Label_Defaut.TabIndex = 6
$Label_Defaut.Text = \"Par Defaut\"
$Label_Defaut.add_Click($handler_label2_Click)
$InstallationImprimanteReseau.Controls.Add($Label_Defaut)
#
# Label \"Numéro d'Imprimante\"
#
$Label_NumImprimante.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 45
$System_Drawing_Point.Y = 30
$Label_NumImprimante.Location = $System_Drawing_Point
$Label_NumImprimante.Name = \"Label_NumImprimante\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.Width = 130
$Label_NumImprimante.Size = $System_Drawing_Size
$Label_NumImprimante.TabIndex = 5
$Label_NumImprimante.Text = \"Numéro de l'imprimante\"
$InstallationImprimanteReseau.Controls.Add($Label_NumImprimante)
#
# Champs de saisie \"Numéro d'Imprimante\"
#
$Champs_NumImprimante.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 180
$System_Drawing_Point.Y = 30
$Champs_NumImprimante.Location = $System_Drawing_Point
$Champs_NumImprimante.Name = \"Champs_NumImprimante\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 179
$Champs_NumImprimante.Size = $System_Drawing_Size
$Champs_NumImprimante.TabIndex = 0
$InstallationImprimanteReseau.Controls.Add($Champs_NumImprimante)
#
# Bouton \"ANNULER\"
#
$Bouton_Annuler.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 276
$System_Drawing_Point.Y = 150
$Bouton_Annuler.Location = $System_Drawing_Point
$Bouton_Annuler.Name = \"Bouton_Annuler\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 110
$Bouton_Annuler.Size = $System_Drawing_Size
$Bouton_Annuler.TabIndex = 4
$Bouton_Annuler.Text = \"Annuler\"
$Bouton_Annuler.UseVisualStyleBackColor = $True
$Bouton_Annuler.add_Click($Bouton_Annuler_OnClick)
$InstallationImprimanteReseau.Controls.Add($Bouton_Annuler)
#
# Bouton \"OK\"
#
$Bouton_OK.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 45
$System_Drawing_Point.Y = 150
$Bouton_OK.Location = $System_Drawing_Point
$Bouton_OK.Name = \"Bouton_OK\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 110
$Bouton_OK.Size = $System_Drawing_Size
$Bouton_OK.TabIndex = 3
$Bouton_OK.Text = \"OK\"
$Bouton_OK.UseVisualStyleBackColor = $True
$Bouton_OK.add_Click($Bouton_OK_OnClick)
$InstallationImprimanteReseau.Controls.Add($Bouton_OK)
#
# Fenetre Message d'erreur Numero d'Imprimante
#
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 81
$System_Drawing_Size.Width = 189
$ErreurNumImprimante.ClientSize = $System_Drawing_Size
$ErreurNumImprimante.DataBindings.DefaultDataSourceUpdateMode = 0
$ErreurNumImprimante.Name = \"ErreurNumImprimante\"
$ErreurNumImprimante.Text = \"Erreur !!\"
#
# Fenetre Message d'erreur Numero d'Imprimante - Label \"Mauvais Numero d'Imprimante\"
#
$ErreurNumImprimante_Label_MauvaisNumero.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 9
$ErreurNumImprimante_Label_MauvaisNumero.Location = $System_Drawing_Point
$ErreurNumImprimante_Label_MauvaisNumero.Name = \"ErreurNumImprimante_Label_MauvaisNumero\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 28
$System_Drawing_Size.Width = 165
$ErreurNumImprimante_Label_MauvaisNumero.Size = $System_Drawing_Size
$ErreurNumImprimante_Label_MauvaisNumero.TabIndex = 1
$ErreurNumImprimante_Label_MauvaisNumero.Text = \"Mauvais Numéro d''Imprimante\"
$ErreurNumImprimante.Controls.Add($ErreurNumImprimante_Label_MauvaisNumero)
#
# Fenetre Message d'erreur Numero d'Imprimante - Bouton \"OK\"
#
$ErreurNumImprimante_Bouton_OK.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 50
$System_Drawing_Point.Y = 40
$ErreurNumImprimante_Bouton_OK.Location = $System_Drawing_Point
$ErreurNumImprimante_Bouton_OK.Name = \"ErreurNumImprimante_Bouton_OK\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$ErreurNumImprimante_Bouton_OK.Size = $System_Drawing_Size
$ErreurNumImprimante_Bouton_OK.TabIndex = 0
$ErreurNumImprimante_Bouton_OK.Text = \"OK\"
$ErreurNumImprimante_Bouton_OK.UseVisualStyleBackColor = $True
$ErreurNumImprimante_Bouton_OK.add_Click($ErreurNumImprimante_Bouton_OK_OnClick)
$ErreurNumImprimante.Controls.Add($ErreurNumImprimante_Bouton_OK)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $InstallationImprimanteReseau.WindowState
#$InitialFormWindowState = $ErreurNumImprimante.WindowState
#Init the OnLoad event to correct the initial state of the form
$InstallationImprimanteReseau.add_Load($OnLoadForm_StateCorrection)
#$ErreurNumImprimante.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$InstallationImprimanteReseau.ShowDialog()| Out-Null
#$ErreurNumImprimante.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm<br><br>Message édité par: dcolleville, à: 21/10/13 15:18
Connexion ou Créer un compte pour participer à la conversation.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
- Messages : 6311
- Remerciements reçus 68
dcolleville écrit:
Jusqu'ici cela me semble normal.Le fenêtre se ferme, mais le process powershell.exe tourne en tache de fond
Si tu utilises ShowDialog, utilise la valeur de retour de ton bouton 'OK' une fois ton traitement terminé.
dcolleville écrit:
Pour le moment dissocie ton traitement de l'IHM.Comment tester la bonne file d'impression sans que cela ralentisse le déroulement ?
Et je ne suis pas sûr d'avoir compris ton pb...
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- dcolleville
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
- Messages : 4
- Remerciements reçus 0
Jusqu'ici cela me semble normal.
Si tu utilises ShowDialog, utilise la valeur de retour de ton bouton 'OK' une fois ton traitement terminé.
Bonjour,
A quoi cela me sert de récupérer la valeur de retour du bouton ? Et comment la récupérer ??
Connexion ou Créer un compte pour participer à la conversation.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
- Messages : 6311
- Remerciements reçus 68
L'appelant connait le choix fait par l'utilisateur.A quoi cela me sert de récupérer la valeur de retour du bouton ?
Et ShowDialog attend en interne que la valeur change. Valeur configurée sur chaque bouton .
En utilisant ce principe de base tu facilites la compréhension de ton code et sa maintenance.
Mais il reste possible d'avoir d'autre comportement et d'autres problèmes...
dcolleville écrit:
[code:1]Et comment la récupérer ??
$DialogResult=$MyForm.ShowDialog()
if ($DialogResult -eq \"Cancel\"«»)
{\"Annulation\"}
#else \"OK\"
[/code:1]
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- dcolleville
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
- Messages : 4
- Remerciements reçus 0
Sauf que lorsque pour le bouton \"Annuler\", j'ai definit ça:
$ErreurNumImprimante_Bouton_OK_OnClick=
{
$ErreurNumImprimante.Close()
}
La boite de dialogue se ferme, je n'ai pas de soucis.
Pourquoi alors le même
dans le code du bouton \"Ok\", à la fin du traitement ne fonctionne pas ??$InstallationImprimanteReseau.Close()
Le traitement se fait:
- recupération du numéro d'imprimante
- création de la chaine serveur + nom imprimante + numéro
- installation de l'imprimante
Sauf que le $InstallationImprimanteReseau.Close() à la fin ne marche pas...
Connexion ou Créer un compte pour participer à la conversation.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
- Messages : 6311
- Remerciements reçus 68
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- Windows Forms