Convert VB.net to powershell

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
Locked
User avatar
doom7344
Posts: 8
Joined: Mon May 15, 2017 8:55 am

Convert VB.net to powershell

Post by doom7344 » Wed Jul 05, 2017 12:22 pm

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 16 times
379423.png
379423.png (5.93 KiB) Viewed 690 times

User avatar
jvierra
Posts: 11489
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert VB.net to powershell

Post by jvierra » Wed Jul 05, 2017 12:27 pm

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.

User avatar
jvierra
Posts: 11489
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert VB.net to powershell

Post by jvierra » Wed Jul 05, 2017 12:29 pm

I will give you a hint:

"object sender" is "$this" in Powerhell. "EventArgs e" is "$_" in Powershell.

User avatar
doom7344
Posts: 8
Joined: Mon May 15, 2017 8:55 am

Re: Convert VB.net to powershell

Post by doom7344 » Wed Jul 05, 2017 3:52 pm

Oh sorry I understand:)

Thanks

User avatar
jvierra
Posts: 11489
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert VB.net to powershell

Post by jvierra » Wed Jul 05, 2017 4:34 pm

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.

Code: Select all

$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)
}
}
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
Joined: Mon May 15, 2017 8:55 am

Re: Convert VB.net to powershell

Post by doom7344 » Thu Jul 06, 2017 5:16 am

Your really nice!! thank a lot!

I will start from there.

User avatar
doom7344
Posts: 8
Joined: Mon May 15, 2017 8:55 am

Re: Convert VB.net to powershell

Post by doom7344 » Fri Jul 07, 2017 8:01 am

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
Joined: Mon May 15, 2017 8:55 am

Re: Convert VB.net to powershell

Post by doom7344 » Mon Jul 17, 2017 6:51 am

got it to work!! :)

here's my working demo.
Attachments
HIGHLIGHT DATAGRIDVIEW.psf
(28.36 KiB) Downloaded 6 times
HIGHLIGHT DATAGRIDVIEW.ps1
(13.78 KiB) Downloaded 8 times

User avatar
jvierra
Posts: 11489
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert VB.net to powershell

Post by jvierra » Tue Jul 18, 2017 4:18 pm

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.

Locked