0

What properties need to be applied to change the forecolor and backcolor of the text on Tabpage?

See Picture: https://i.stack.imgur.com/lYb03.jpg

Here is my Code:

$TabControl_Main = New-Object System.Windows.Forms.TabControl
$TabControl_Main.Location = New-Object System.Drawing.Size(20,550)
$TabControl_Main.Size =  New-Object System.Drawing.Size(850,270)
$form_MainForm.Controls.Add($TabControl_Main)

$TabPage1 = New-Object System.Windows.Forms.TabPage
$TabPage1.Location = New-Object System.Drawing.Size(20,550)
$TabPage1.Size =  New-Object System.Drawing.Size(850,270)
$TabPage1.Text = "Processes"       
$TabControl_Main.Controls.Add($TabPage1)
Techno
  • 37
  • 8
  • `$TabControl_Main.BackColor = New-Object System.Drawing.Color("Red")` should work as an example – TobyU Nov 21 '18 at 14:40
  • I want to change the color on each Text on each Tabs :). This code you wrote here cause the following error: "New-Object : A constructor was not found. Cannot find an appropriate constructor for type System.Dra wing.Color." – Techno Nov 21 '18 at 14:49
  • 1
    `$TabControl_Main.BackColor = [System.Drawing.Color]::Red` should do it. – Theo Nov 21 '18 at 15:15
  • Not working. :( – Techno Nov 23 '18 at 12:41

1 Answers1

0

You have to create an event and draw the area. Here is some code based on this example in c#, credits @Fun Mun Pieng.

# assign a color for each tab
$PageColor  = @{0 = "lightgreen";
                1 = "yellow";
                2 = "lightblue"}

# define the event
$tabControl_Drawing = {
    param([object]$Sender, [System.EventArgs]$e)

    $Background = new-object Drawing.SolidBrush $PageColor[$e.Index]
    $Foreground = new-object Drawing.SolidBrush black

    $tabGraphics = $e.Graphics
    $tabBounds = $e.Bounds
    $tabGraphics.FillRectangle($Background,$tabBounds)
    $tabTextSize = $tabGraphics.MeasureString($sender.TabPages[$e.Index].text, $e.Font)

    $tabGraphics.DrawString($Sender.TabPages[$e.Index].Text,$e.Font,$Foreground,$tabBounds.Left + ($tabBounds.Width - $tabTextSize.Width) / 2,$tabBounds.Top + ($tabBounds.Height -$tabTextSize.Height) / 2 +1)
    $e.DrawFocusRectangle()
}
# add the event
$TabControl_Main.add_DrawItem($tabControl_Drawing)

A little easier to use is HotTrack:

$TabControl_Main.HotTrack = $true

You will see the effect when you execute your script with powershell instead of powershell ISE.

BackColor does nothing. To use the words of MSDN:

BackColor > This member is not meaningful for this control.

edit: added the code.

T-Me
  • 1,814
  • 1
  • 9
  • 22
  • I tested this, I can't see changes :\ I want this in Powershell: https://stackoverflow.com/questions/30039627/how-do-i-change-the-color-of-each-tab – Techno Nov 23 '18 at 12:44
  • @Techno added some code that should do what you want. For HotTrack it's important to execute your script with Powershell or copy it into a powershell seassion. In ISE you wont see the changes. – T-Me Nov 26 '18 at 11:45