tab name color

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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 1 year and 5 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
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

tab name color

Post by Domtar »

is there some way to set the color of the name of a tabPage depending on a condition?

my TabControl has several tabPages, where each one is a computer in AD. I'd like to have the following;

if the PC is in AD, set the Tab name forecolor to black and the backcolor to green, if not in AD, set the backcolor red and the forecolor white

can this be done?

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

Re: tab name color

Post by jvierra »

You can use the "Paint" event to paint custom tabs. It is a bit of a difficult programming task if you have no experience programming to the Windows Net "Graphics" object.

I do not have any examples for tabs. The only samples I have are for the DataGridView. You can search for examples to see if anyone has posted examples. I suggest searching for "TabControl Paint Event"

Every object in WinForms sends a "Paint" event but I am not sure the tab part can be isolated. Give it a try by outputting the object name in the "Paint" event code.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: tab name color

Post by jvierra »

Here is one example that will get you started.

https://social.msdn.microsoft.com/Forum ... msdesigner
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: tab name color

Post by Domtar »

OK I believe I got all the required code except that I do not know how to raise the event for that

here's a piece of code to illustrate what I tried. I know the event is not raised because the label1 text property does not get updated

pretty sure I'm missing a small piece somewhere.

Code: Select all

$tabcontrol1_DrawItem=[System.Windows.Forms.DrawItemEventHandler]{
	if ($tabcontrol1.SelectedIndex -eq 0) {
				$label1.Text = 'Draw event was raised'
		$_.Graphics.FillRectangle('Green', $_.Bounds)
	}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: tab name color

Post by jvierra »

We don't "raise" events. The system raises events based on external influences like mouse clicks or keyboard input. Some events are raised by the system when the GUI needs attention. The "Paint" event delive3red as a "DrawItem event for the form when items need to be drawn is handled by the WinForms system. When you add the DrawItem event it is called before the system draws or paints the item. In the case of the TabControl you get the graphic object for the tab control. This lets you draw on the control window. You then have to calculate what you want to draw and where you want to draw it. This will require some knowledge of mathematics and raster graphics drawing.

I recommend learning how Windows and WinForms work with graphics. As I noted above, you are in the arena with advanced programming and developing.

If I had an example, I would post it. I started a quick example but then realized that it would take me an hour to do a demo that would be understandable. I may do that eventually but can't do it now.

Consider this a good excuse for you to learn Windows graphics. You can also continue to search for other examples. They will not give you the code you seek. They will almost always be in C# and C# cannot be directly turned into PowerShell script. This will give you a chance to learn C# and how to recode C# examples as PowerShell code.
Good luck and have fun. It is something that all Windows GUI programmers have to go through.
This topic is 1 year and 5 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