Question GUI Affichage de données des users

Plus d'informations
il y a 7 ans 3 mois #28076 par Philippe
salut hichammadd

j'ai un peu de temps pour regarder mais j'ai trouver

comme tu la fais pour fieldsDetail ou tu a rajouter _Child, il te faut faire pareil avec fieldsConfig dans les fonctions Do-RenderUserDetail_child et $btnMoreInfo_Click
il faut aussi doubler l'initialisation de la variable au début du script

je te rappel ou rajoute une modif à faire dans Get-Users qui m'a poser un soucis (le [0])
[code:1] $user.Add($script:attributesToFields[$attribute][0], $result.properties.$attribute[0])
[/code:1]

voici le script entier si tu ne la pas fais évolué entre temps :
[code:1][void] [System.Reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\"«»)
[void] [System.Reflection.Assembly]::LoadWithPartialName(\"System.Drawing\"«»)

[System.Windows.Forms.Application]::EnableVisualStyles()

####### ## Global Variables #######
# working hash table of users being operated against (lookup results are stored in this)
$script:workingUsers = @{}

# list of fields to be included in results table and in detail form for individual account
$script:fieldsTable = @(\"uid\", \"sn\", \"mail\"«»)
$script:fieldsDetail = @(\"uid\", \"mail\"«»)
$script:fieldsDetail_Child = @(\"DN\", \"uid\", \"cn\", \"sn\", \"mail\"«»)
# fieldsConfig is a hash of field names with an array of (numLines, scrollEnabled, textbox)
$script:fieldsConfig = [ordered]@{
\"uid\" = (1, $False, $Null);
\"mail\" = (1, $False, $Null)
}
$script:fieldsConfig_Child = [ordered]@{
\"DN\" = (1, $False, $Null);
\"uid\" = (1, $False, $Null);
\"cn\" = (1, $False, $Null);
\"sn\" = (1, $False, $Null);
\"mail\" = (1, $False, $Null)
}


# attribute mapping to field label, and boolean indicated inclusion in new user form
$script:attributesToFields = @{
\"adspath\" = (\"DN\", $False);
\"uid\" = (\"uid\", $False);
\"cn\" = (\"cn\", $True);
\"sn\" = (\"sn\", $True);
\"mail\" = (\"mail\", $True);
}

$script:buttonWidth = 150
$script:buttonHeight = 23
$script:buttonBaseX = 620

#######
## Helper Functions
#######

function Get-Users ($query = \"\"«») {
If ($query.Trim() -eq \"\"«») {
return @{}
}
$query = \"*\" + $query + \"*\"
$LDAP_Query = New-Object System.DirectoryServices.DirectorySearcher($Root, \"(&(objectclass=*)(uid=$query))\"«»)
$results = $LDAP_Query.findall()
$users = @{}
ForEach ($result in $results) {
$user = @{}
ForEach ($attribute in $script:attributesToFields.Keys) {
If ($user.Keys -notContains $script:attributesToFields[$attribute][0]) {
$user.Add($script:attributesToFields[$attribute][0], $result.properties.$attribute[0])
}
}
$users.Add($result.properties.uid[0], $user)
}
return $users

}


function Do-AccountLookup ($strQuery) {
$script:workingUsers = Get-Users $strQuery
$objListView.Items.Clear()
$script:workingUsers.Keys | ForEach-Object {
$current_user = $_
$objListviewItem = New-Object System.Windows.Forms.ListViewItem($script:workingUsers.get_Item($current_user).get_Item($script:fieldsTable[0]))
ForEach ($field in $script:fieldsTable[1..($script:fieldsTable.Length - 1)]) {
$objListviewItem.SubItems.Add([string]$script:workingUsers.get_Item($current_user).get_Item($field)) | Out-Null
}
$objListview.Items.Add($objListviewItem) | Out-Null
}
}

#######
## GUI Event Handlers
#######

# handlers for static buttons

[System.EventHandler] $hndlrDoAccountLookup = {
$objListview.Items.Clear()
$Domaine = \"LDAP://ldap.forumsys.com:389/dc=example,dc=com\"
$Root = New-Object -TypeName System.DirectoryServices.DirectoryEntry($Domaine, $null, $null, 'FastBind')
$query = $txtQuery.Text
Do-AccountLookup($query)

}

######### GUI Helpers #######

function Get-CurrentUser () {

$currentUser = $Null
$currentIdx = $objListView.SelectedIndices[0]

if ($currentIdx -ne $Null) {
$currentUser = @{}
$currentuid = $objListView.SelectedItems[0].Text
$currentUser.Add(\"Index\", $currentIdx)
ForEach ($attr in $script:fieldsConfig.Keys) {
$currentUser.Add($attr, $script:workingUsers.$currentuid.$attr)
}
}
return $currentUser
}


Function Do-RenderUserDetail($currentUser = $Null) {

if ($currentUser -eq $Null) {

$currentUser = Get-CurrentUser
}

if ($currentUser -ne $Null) {
$uid = $currentUser.uid

foreach ($key in $script:fieldsDetail) {
if ($key -eq \"DN\"«») {
$script:fieldsConfig.Get_Item($key)[2].Text = $script:workingUsers.get_Item($uid).get_Item($key) -replace \"LDAP://ldap.forumsys.com:389/\", \"\"
}
else {
$script:fieldsConfig.Get_Item($key)[2].Text = $script:workingUsers.get_Item($uid).get_Item($key)
}
$script:fieldsConfig.Get_Item($key)[2].ReadOnly = $True
}
}
else {
foreach ($key in $script:fieldsDetail) {
$script:fieldsConfig.Get_Item($key)[2].Text = \"\"
}
}
}

Function Do-RenderUserDetail_child($currentUser = $Null) {

if ($currentUser -eq $Null) {
$currentUser = Get-CurrentUser
}

if ($currentUser -ne $Null) {
$uid = $currentUser.uid

foreach ($key in $script:fieldsDetail_Child) {
if ($key -eq \"DN\"«») {
$script:fieldsConfig_Child.Get_Item($key)[2].Text = $script:workingUsers.get_Item($uid).get_Item($key) -replace \"LDAP://ldap.forumsys.com:389/\", \"\"
}
else {
$script:fieldsConfig_Child.Get_Item($key)[2].Text = $script:workingUsers.get_Item($uid).get_Item($key)
}
$script:fieldsConfig_Child.Get_Item($key)[2].ReadOnly = $True
}
}
else {
foreach ($key in $script:fieldsDetail_Child) {
$script:fieldsConfig_Child.Get_Item($key)[2].Text = \"\"
}
}
}

function Sort-ListView {
# from etechgoodness.wordpress.com/2014/02/25/s...t-a-custom-comparer/
param([parameter(Position = 0)][UInt32]$Column)
$Numeric = $true # determine how to sort
if ($Script:LastColumnClicked -eq $Column) {
$Script:LastColumnAscending = -not $Script:LastColumnAscending
}
else {
$Script:LastColumnAscending = $true
}
$Script:LastColumnClicked = $Column
$ListItems = @(@(@())) # three-dimensional array; column 1 indexes the other columns,column 2 is the value to be
# sorted on, and column 3 is the System.Windows.Forms.ListViewItem object

foreach ($ListItem in $objListView.Items) {
# if all items are numeric, can use a numeric sort
if ($Numeric -ne $false) {
try {
$Test = [Double]$ListItem.SubItems.Text[$Column]
}
catch {
$Numeric = $false # a non-numeric item was found, so sort will occur as a string
}
}
$ListItems += , @($ListItem.SubItems.Text[$Column], $ListItem)
}

$EvalExpression = {
if ($Numeric)
{ return [Double]$_[0] }
else
{ return [String]$_[0] }
}

$ListItems = $ListItems | Sort-Object -Property @{Expression = $EvalExpression; Ascending = $Script:LastColumnAscending}

$objListView.BeginUpdate()
$objListView.Items.Clear()
foreach ($ListItem in $ListItems) {
$objListView.Items.Add($ListItem[1])
}
$objListView.EndUpdate()
}

#######
## GUI Initialization
#######

$frmUI = New-Object System.Windows.Forms.Form
$frmUI.Text = \"User Account Managment\"
$frmUI.Size = New-Object System.Drawing.Size(610, 400)
$frmUI.StartPosition = \"CenterScreen\"
$frmUI.KeyPreview = $True
$frmUI.MaximumSize = $frmUI.Size
$frmUI.MinimumSize = $frmUI.Size

# first row - lookup input / button
$lblQuery = New-Object System.Windows.Forms.Label
$lblQuery.Location = New-Object System.Drawing.Size(5, 5)
$lblQuery.Size = New-Object System.Drawing.Size(140, 20)
$lblQuery.Text = \"Username / Name:\"
$frmUI.Controls.Add($lblQuery)

$txtQuery = New-Object System.Windows.Forms.TextBox
$txtQuery.Location = New-Object System.Drawing.Size(145, 5)
$txtQuery.Size = New-Object System.Drawing.Size(270, 20)
$frmUI.Controls.Add($txtQuery)
$txtQuery.Select()

$btnLookup = New-Object System.Windows.Forms.Button
$btnLookup.Location = New-Object System.Drawing.Size(($buttonBaseX - $buttonWidth - 40), 5)
$btnLookup.Size = New-Object System.Drawing.Size($buttonWidth, $buttonHeight)
$btnLookup.Text = \"Lookup\"
$btnLookup.Add_Click($hndlrDoAccountLookup)
$frmUI.Controls.Add($btnLookup)

# second row - lookup results
$lblResult = New-Object System.Windows.Forms.Label
$lblResult.Location = New-Object System.Drawing.Size(5, 45)
$lblResult.Size = New-Object System.Drawing.Size(400, 15)
$lblResult.Text = \"Lookup Results\"
$frmUI.Controls.Add($lblResult)

$objListview = New-Object System.Windows.Forms.Listview
$objListview.Location = New-Object System.Drawing.Size(5, 60)
$objListview.Size = New-Object System.Drawing.Size(590, 140)
$objListview.Height = 140
$objListview.View = [System.Windows.Forms.View]::«»Details
$objListView.MultiSelect = $False
$objListView.FullRowSelect = $True
foreach ($field in $script:fieldsTable) {
$col = $objListview.Columns.Add($field)
$col.Width = 180
}
$objListView.add_ColumnClick( {Sort-ListView $_.Column})
$frmUI.Controls.Add($objListview)
$objListview.Add_SelectedIndexChanged( {Do-RenderUserDetail})

# third row - individual account details
$lblAccount = New-Object System.Windows.Forms.Label
$lblAccount.Location = New-Object System.Drawing.Size(5, 210)
$lblAccount.Size = New-Object System.Drawing.Size(400, 15)
$lblAccount.Text = \"Account Details\"
$frmUI.Controls.Add($lblAccount)

$details_baseheight_Child = 30
$details_baseheight = 230
$details_rowheight = 25
$details_rowwidth = 480

# build out the text fields to hold the account details
$i = 0
ForEach ($fieldName in $script:fieldsDetail) {

$lbl = New-Object System.Windows.Forms.Label
$lbl.Location = New-Object System.Drawing.Size(5, ($details_baseheight + ($details_rowheight * $i)))
$lbl.Size = New-Object System.Drawing.Size(100, $details_rowheight)
$lbl.Text = $fieldName + \":\"
$frmUI.Controls.Add($lbl)
$txt = New-Object System.Windows.Forms.TextBox
$txt.Location = New-Object System.Drawing.Size(105, ($details_baseheight + ($details_rowheight * $i)))
$rows = $script:fieldsConfig.Get_Item($fieldName)[0]
$scrollable = $script:fieldsConfig.Get_Item($fieldName)[1]
$txt.Size = New-Object System.Drawing.Size($details_rowwidth, (15 * $rows))
$txt.ReadOnly = $True
if ($rows -gt 1) {
$txt.MultiLine = $True
}
if ($scrollable) {
$txt.ScrollBars = \"Vertical\"
}
$script:fieldsConfig.Get_Item($fieldName)[2] = $txt
$frmUI.Controls.Add($txt)
$i += 1
}

###############################################################################

$btnMoreInfo_Click = {
$frmMoreInfo = New-Object 'System.Windows.Forms.Form'
$buttonOK = New-Object 'System.Windows.Forms.Button'
$frmMoreInfo.SuspendLayout()
#
# formChildForm
#

$frmMoreInfo.AcceptButton = $buttonOK
$frmMoreInfo.AutoScaleDimensions = '6, 13'
$frmMoreInfo.AutoScaleMode = 'Font'
$frmMoreInfo.ClientSize = '450, 220' #'383, 383'
$frmMoreInfo.FormBorderStyle = 'FixedDialog'
$frmMoreInfo.Margin = '5, 5, 5, 5'
$frmMoreInfo.MaximizeBox = $False
$frmMoreInfo.MinimizeBox = $False
$frmMoreInfo.Name = 'formChildForm'
$frmMoreInfo.StartPosition = 'CenterScreen'
$frmMoreInfo.Text = 'User Details'
$frmMoreInfo.ResumeLayout()
#
# buttonOK
#
$buttonOK.Anchor = 'Bottom, Left, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = '273, 200'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '98, 33'
$buttonOK.TabIndex = 0
$buttonOK.Text = '&OK'
$buttonOK.UseVisualStyleBackColor = $True
$buttonOK.add_Click($frmUI.ResumeLayout())
$frmMoreInfo.Controls.Add($buttonOK)

#$buttonOK_Click = {} # ==>> ici je pense

$i = 0
ForEach ($fieldName in $script:fieldsDetail_Child) {

$lbl = New-Object System.Windows.Forms.Label
$lbl.Location = New-Object System.Drawing.Size(5, ($details_baseheight_Child + ($details_rowheight * $i)))
$lbl.Size = New-Object System.Drawing.Size(100, $details_rowheight)
$lbl.Text = $fieldName + \":\"
$frmMoreInfo.Controls.Add($lbl)
$txt = New-Object System.Windows.Forms.TextBox
$txt.Location = New-Object System.Drawing.Size(105, ($details_baseheight_Child + ($details_rowheight * $i)))
$rows = $script:fieldsConfig_Child.Get_Item($fieldName)[0]
$scrollable = $script:fieldsConfig_Child.Get_Item($fieldName)[1]
$txt.Size = New-Object System.Drawing.Size($details_rowwidth, (15 * $rows))
$txt.ReadOnly = $True
if ($rows -gt 1) {
$txt.MultiLine = $True
}
if ($scrollable) {
$txt.ScrollBars = \"Vertical\"
}
$script:fieldsConfig_Child.Get_Item($fieldName)[2] = $txt
$frmMoreInfo.Controls.Add($txt)
$i += 1
}
Do-RenderUserDetail_child

#$frmMoreInfo.Add_KeyDown({if ($_.KeyCode -eq \"Enter\"«») {$hndlrDoAccountLookup.Invoke($Null, [EventArgs]::Empty)}})
#$frmMoreInfo.Add_KeyDown({if ($_.KeyCode -eq \"Escape\"«») {$frmMoreInfo.Close()}})

[void]$frmMoreInfo.ShowDialog()
$frmMoreInfo.dispose()
}

$btnMoreInfo = New-Object 'System.Windows.Forms.Button'
$btnMoreInfo.Location = '250, 300'
$btnMoreInfo.Name = 'buttonOpenChildForm'
$btnMoreInfo.Size = '144, 37'
$btnMoreInfo.TabIndex = 0
$btnMoreInfo.Text = 'Show more Details'
$btnMoreInfo.UseVisualStyleBackColor = $True
$btnMoreInfo.add_Click($btnMoreInfo_Click)
$frmUI.ResumeLayout()
$frmUI.Controls.Add($btnMoreInfo)

###############################################################################

# misc gui stuff
$frmUI.Add_KeyDown( {if ($_.KeyCode -eq \"Enter\"«») {$hndlrDoAccountLookup.Invoke($Null, [EventArgs]::Empty)}})
$frmUI.Add_KeyDown( {if ($_.KeyCode -eq \"Escape\"«») {$frmUI.Close()}})
$frmUI.Add_Shown( {$frmUI.Activate()})
[void]$frmUI.ShowDialog()
$frmUI.dispose()
[/code:1]

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

Plus d'informations
il y a 7 ans 3 mois #28087 par Hicham Madini
6ratgus t'es génial! Merci infiniment!

Encore une question et deux petits soucis.

question: c'est quoi le rôle de la fonction dispose() ?

Soucis 1:
le boutton \"show more details\" est actif avant même de selectionner un profil.

soucis 2:
1- je selectionne un profil dans la listview
2- je clique sur le boutton \"show more details\"
3- je clique sur ok pour fermer la fenêtre \"User details\"
aprés le profil que j'avais selectionné se deselectionne et donc je perds l'orientation.

Merci d'avance pour ton aide!

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

Plus d'informations
il y a 7 ans 3 mois #28088 par Hicham Madini
Bon pour le premier soucis c'est réglé:

avant $btnMoreInfo.add_Click($btnMoreInfo_Click) j'ai ajouté $btnMoreInfo.Enabled = $False

et dans ...Add_SelectedIndexChanged...
$objListview.Add_SelectedIndexChanged( {Do-RenderUserDetail;$btnMoreInfo.Enabled = $True})

Je continue ....

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

Plus d'informations
il y a 7 ans 3 mois #28089 par Hicham Madini
Re,

deuxième soucis réglé, j'ai désactivé le HideSelection

[code:1]$objListview.HideSelection = $False
$objListview.Add_SelectedIndexChanged( {Do-RenderUserDetail;$btnMoreInfo.Enabled = $True})[/code:1]

La selection devient grise mais mieux qu'avant.

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

Plus d'informations
il y a 7 ans 3 mois #28093 par Philippe
faut vraiment que tu te reconvertisse dans la programmation
peut être a tu déjà la bonne logique si tu programme les tours numériques !!

Soucis 1:
le boutton \"show more details\" est actif avant même de selectionner un profil.

pas mieux !!

soucis 2:
en sorti de affichage des detail le profil que j'avais selectionné se deselectionne et donc je perds l'orientation.

j'avais remarquer aussi

et pour l'instant j'ai pas mieux que toi

question: c'est quoi le rôle de la fonction dispose() ?

dispose détruit objet form ou autre et libère la mémoire utilise

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

Plus d'informations
il y a 7 ans 3 mois #28103 par Hicham Madini
slt 6ratgus,

sorry j'étais malade ces derniers jours :(

oui je commence à m'intérésser à la progrmmation et surtout Powershell! mon but c'est de devenir un initié chez vous :D

Merci pour l'explication de dispose()!

Bon j'ai réussi à doubler la GUI (Form), j'ai pris le même serveur, mais ca fonctionne avec deux serveurs différents voir attachement!
(côté design c'est pas notre sujet ;) )
Je trouve que le nombre de lignes du code augmente rapidement. Donc j'ai pensé le reduire en créant une fonction qui recoit des paramêtres et crée automatiquement les formes. que penses tu?

La pièce jointe Forum_double.ps1 est absente ou indisponible

Pièces jointes :

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

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