Convert VB.net to powershell

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.
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
doom7344
Posts: 8
Last visit: Thu May 09, 2019 10:26 am

Convert VB.net to powershell

Post by doom7344 »

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
Attachments
vbnetcode.txt
(2.57 KiB) Downloaded 183 times
379423.png
379423.png (5.93 KiB) Viewed 5999 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Convert VB.net to powershell

Post by jvierra »

Sorry but this is not a forum for free code conversion. With a little experience you can translate it or you can contact a developer with that skill.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Convert VB.net to powershell

Post by jvierra »

I will give you a hint:

"object sender" is "$this" in Powerhell. "EventArgs e" is "$_" in Powershell.
User avatar
doom7344
Posts: 8
Last visit: Thu May 09, 2019 10:26 am

Re: Convert VB.net to powershell

Post by doom7344 »

Oh sorry I understand:)

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Convert VB.net to powershell

Post by jvierra »

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.
  1. $dataGridView1_CellPainting = {
  2.     if ($_.RowIndex -ge 0 -and $_.ColumnIndex -ge 0 -and $_.IsSelected) {
  3.         $_.Handled = $true
  4.         $_.PaintBackground($_.CellBounds, $true)
  5.  
  6.         if($sw = $txtSearch.Text){
  7.             $val = $_.FormattedValue
  8.             $sindx = $val.ToLower().IndexOf($sw.ToLower())
  9.             $sCount = 1
  10.             while ($sindx -ge 0){
  11.                 $hl_rect = new Rectangle()
  12.                 $hl_rect.Y = $_.CellBounds.Y + 2
  13.                 $hl_rect.Height = $_.CellBounds.Height - 5
  14.                
  15.                 $sBefore = $val.Substring(0, sindx)
  16.                 $sWord = $val.Substring(sindx, sw.Length)
  17.                 $s1 = [System.Windows.Forms.TextRenderer]::MeasureText($_.Graphics, $sBefore, $_.CellStyl$_.Font, $_.CellBounds.Size)
  18.                 $s2 = [System.Windows.Forms.TextRenderer]::MeasureText($_.Graphics, $sWord, $_.CellStyl$_.Font, $_.CellBounds.Size)
  19.                
  20.                 if ($s1.Width -gt 5) {
  21.                     $hl_rect.X = $_.CellBounds.X + s1.Width - 5
  22.                     $hl_rect.Width = s2.Width - 6
  23.                 } else {
  24.                     $hl_rect.X = $_.CellBounds.X + 2
  25.                     $hl_rect.Width = $s2.Width - 6
  26.                 }
  27.                
  28.                 SolidBrush hl_brush = default(SolidBrush)
  29.                 if ((($_.State & DataGridViewElementStates.Selected) != DataGridViewElementStates.None)) {
  30.                     hl_brush = new SolidBrush(Color.DarkGoldenrod)
  31.                 } else {
  32.                     hl_brush = new SolidBrush(Color.Yellow)
  33.                 }
  34.                
  35.                 $_.Graphics.FillRectangle(hl_brush, hl_rect)
  36.                
  37.                 hl_brush.Dispose()
  38.                 sindx = val.ToLower().IndexOf(sw.ToLower(), sCount++)
  39.             }
  40.         }
  41.        
  42.         $_.PaintContent($_.CellBounds)
  43.     }
  44. }
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.
User avatar
doom7344
Posts: 8
Last visit: Thu May 09, 2019 10:26 am

Re: Convert VB.net to powershell

Post by doom7344 »

Your really nice!! thank a lot!

I will start from there.
User avatar
doom7344
Posts: 8
Last visit: Thu May 09, 2019 10:26 am

Re: Convert VB.net to powershell

Post by doom7344 »

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;
}

}
User avatar
doom7344
Posts: 8
Last visit: Thu May 09, 2019 10:26 am

Re: Convert VB.net to powershell

Post by doom7344 »

got it to work!! :)

here's my working demo.
Attachments
HIGHLIGHT DATAGRIDVIEW.psf
(28.36 KiB) Downloaded 154 times
HIGHLIGHT DATAGRIDVIEW.ps1
(13.78 KiB) Downloaded 184 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Convert VB.net to powershell

Post by jvierra »

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.
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked