Datagridview

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
owinsloe
Posts: 96
Joined: Tue Jul 10, 2012 10:37 am

Datagridview

Post by owinsloe »

David...thanks...I have ended up with this...$datagridview1_EditingControlShowing=[System.Windows.Forms.DataGridViewEditingControlShowingEventHandler]{#Event Argument: $_ = [System.Windows.Forms.DataGridViewEditingControlShowingEventArgs] #TODO: Place custom script here if ( $datagridview1.CurrentCell.ColumnIndex -eq 2){ if ( $_.Control -is [System.Windows.Forms.TextBox]){ $_.Control.UseSystemPasswordChar = $true; } $_.Control.add_KeyPress($txt_KeyPress) }else{ if ( $_.Control -is [System.Windows.Forms.TextBox]){ $_.Control.UseSystemPasswordChar = $false } $_.Control.remove_KeyPress($txt_KeyPress) }}$txt_KeyPress={ $i = $datagridview1.CurrentCell.RowIndex # Need row to assign password characters to non visible password cell if ( $_.KeyChar -ge 32 -and $_.KeyChar -le 126 ){ $_.KeyChar='*' $datagridview1.rows[$i].cells[3].value+=$_.KeyChar }}The only down side (which I'm prepared to live with) is that the systempasswordchar uses wingdings 'l' char and when you click out of the current cell the characters change to the "*". I tried changing the column style to use the wingding font and replace the keycar with 'l' but got the following error...ERROR: New-Object : A positional parameter cannot be found that accepts argument '='.MainForm.Temp.ps1 (2954): ERROR: At C:scriptscnwfileadminMainForm.Temp.ps1:2954 char:60ERROR: + $System_Windows_Forms_DataGridViewCellStyle_5 = New-Object <<<< 'System.Windows.Forms.DataGridViewCellStyle' $System_Windows_Forms_DataGridViewCellStERROR: yle_5.Font = "Wingdings, 7.8pt"ERROR: + CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingExceptionERROR: + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommandERROR:so, pretty happy with the current behaviour thanks to you guys....cheers

jvierra
Posts: 14668
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Datagridview

Post by jvierra »

You really don't have to overwork it so much. Just use the password mask and teh validated event to set up the column.

Code: Select all

	
$datagridview1_EditingControlShowing=[System.Windows.Forms.DataGridViewEditingControlShowingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewEditingControlShowingEventArgs]
 #TODO: Place custom script here
 if($datagridview1.CurrentCell.ColumnIndex -eq 1){ 
     $_.Control.UseSystemPasswordChar = $true;
 }else{
  $_.Control.UseSystemPasswordChar = $false;
 }
}
	

$datagridview1_CellValidated=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
 #TODO: Place custom script here
 if($datagridview1.CurrentCell.ColumnIndex -eq 1){
  $textbox1.Text=$datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value
  $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value='*' * 10
 }
}

The problem tht you are fighting is that teh edit control is created for each column just before teh column is entered and destroyed immediately after it is exited. The value captured is saved in teh 'Cells' 'Value' property. To mask th e vlaue just replace its contents. The control will take care of everything else in a competely normal fashion.

In Windows programming you always need to as more questions whenever it takes more than a few lines to accomplish any reasonably
request.

The above sets the new control into pasword edit mode. This is actually done each time the control is created (aplied) and needs to be unset each time the control is reapplied if the column is not the one wanted.

When the cell is validated its contents are available as teh value of the cell just before the edit control is removed so if we can set the value to some arbitrary string. I use a fixed lenght to as password lenght should never be allowed to be known.

One place to save the real value would be in the Cell's 'Tag'

jvierra
Posts: 14668
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Datagridview

Post by jvierra »

The bit I forgot to mention is that, since you are using teh 'validated' event all key trnslations will have taken place so there is no need to fixup the keystrokes. If we intercept each keytroke then we will not get the results of the keyboard handlers interaction.

It almost always best to use 'validated' and 'validating' events for data capture and translation although the 'Formatting' events can be used also.

Locked