Can someone are able to perfectly translate this VB.net code in Powershell?: I realy need this code for my Powershell project
the code is in a txt file in attachment.
see attachment pic to see the context usage
Thanks
Convert VB.net to powershell
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.
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.
Convert VB.net to powershell
- Attachments
-
- vbnetcode.txt
- (2.57 KiB) Downloaded 183 times
-
- 379423.png (5.93 KiB) Viewed 6001 times
Re: Convert VB.net to powershell
Here is a starter for you. I have converted half of the code. Use it as a template for what you need to do to convert the rest. This will be a good learning excersize and help you to better understand forms and PowerShell.
It is actually easier to start with a C# example.
Compare the original to this to see how to proceed. Yu will have to learn how to look up the Windows Net classes and use then in PowerShell.
It is actually easier to start with a C# example.
- $dataGridView1_CellPainting = {
- if ($_.RowIndex -ge 0 -and $_.ColumnIndex -ge 0 -and $_.IsSelected) {
- $_.Handled = $true
- $_.PaintBackground($_.CellBounds, $true)
- if($sw = $txtSearch.Text){
- $val = $_.FormattedValue
- $sindx = $val.ToLower().IndexOf($sw.ToLower())
- $sCount = 1
- while ($sindx -ge 0){
- $hl_rect = new Rectangle()
- $hl_rect.Y = $_.CellBounds.Y + 2
- $hl_rect.Height = $_.CellBounds.Height - 5
- $sBefore = $val.Substring(0, sindx)
- $sWord = $val.Substring(sindx, sw.Length)
- $s1 = [System.Windows.Forms.TextRenderer]::MeasureText($_.Graphics, $sBefore, $_.CellStyl$_.Font, $_.CellBounds.Size)
- $s2 = [System.Windows.Forms.TextRenderer]::MeasureText($_.Graphics, $sWord, $_.CellStyl$_.Font, $_.CellBounds.Size)
- if ($s1.Width -gt 5) {
- $hl_rect.X = $_.CellBounds.X + s1.Width - 5
- $hl_rect.Width = s2.Width - 6
- } else {
- $hl_rect.X = $_.CellBounds.X + 2
- $hl_rect.Width = $s2.Width - 6
- }
- SolidBrush hl_brush = default(SolidBrush)
- if ((($_.State & DataGridViewElementStates.Selected) != DataGridViewElementStates.None)) {
- hl_brush = new SolidBrush(Color.DarkGoldenrod)
- } else {
- hl_brush = new SolidBrush(Color.Yellow)
- }
- $_.Graphics.FillRectangle(hl_brush, hl_rect)
- hl_brush.Dispose()
- sindx = val.ToLower().IndexOf(sw.ToLower(), sCount++)
- }
- }
- $_.PaintContent($_.CellBounds)
- }
- }
Re: Convert VB.net to powershell
Hi, I almost done the conversion, I'm just sticking on this part on the form load:
bool IsSelected = false;
private void SearchStringPosition(string Searchstring)
{
IsSelected = true;
}
--------------------------------------------------------------------------------------------------------------------------
Is it ok to transform it into a functon like this?, but I have a problem with the "bool IsSelected = false" ??
$form1_Load={
bool IsSelected = false;
function SearchStringPosition
{
param ([string] $Searchstring)
IsSelected = true;
}
}
bool IsSelected = false;
private void SearchStringPosition(string Searchstring)
{
IsSelected = true;
}
--------------------------------------------------------------------------------------------------------------------------
Is it ok to transform it into a functon like this?, but I have a problem with the "bool IsSelected = false" ??
$form1_Load={
bool IsSelected = false;
function SearchStringPosition
{
param ([string] $Searchstring)
IsSelected = true;
}
}
Re: Convert VB.net to powershell
got it to work!!
here's my working demo.
here's my working demo.
- Attachments
-
- HIGHLIGHT DATAGRIDVIEW.psf
- (28.36 KiB) Downloaded 154 times
-
- HIGHLIGHT DATAGRIDVIEW.ps1
- (13.78 KiB) Downloaded 184 times
Re: Convert VB.net to powershell
Great. I looked at you code and it looks good. Now you know hoe to customize the rendering of all controls which can help extend and decorate forms in unique and interesting ways.
Happy PosHing.
Happy PosHing.