473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Specify custom disabled text color in ToolStripProfessionalRendere

I want to specify a custom color for disabled text that is used by a class
that inherits from ToolStripProfessionalRenderer. The code would look
something like this:

Public Class MyRenderer
Inherits Windows.Forms.ToolStripProfessionalRenderer
Protected Overrides Sub OnRenderItemText(ByVal e As
System.Windows.Forms.ToolStripItemTextRenderEventA rgs)
If (Not e.Item.Enabled) Then
e.TextColor = Color.Red
End If
MyBase.OnRenderItemText(e)
End Sub
End Class

The problem is that e.TextColor seems to be ignored if e.Item.Enabled is
false. The only other option that I can think of is to use a method like
Windows.Forms.TextRenderer.DrawText. The problem with that approach is that
then I would need to account for e.TextDirection which can get complex,
especially if the value is ToolStripTextDirection.Inherit.

Any workarounds or ways to simplify drawing the text myself?

Thanks for any help!
Lance

Jan 19 '07 #1
6 3701
Hi Lance,

Based on my understanding, you wanted to create a customized
ToolStripRender inherits from ToolStripProfessionalRenderer to customize
the text display color of each item. However, you find that when the item
is disabled, your setting in ToolStripProfessionalRenderer.OnRenderItemText
method will be ignored.

Yes, I have written a little sample application regarding your scenario and
I can reproduce this behavior.

Further research shows that after invoking your override version of
OnRenderItemText, "MyBase.OnRenderItemText(e)" in your code will call the
ToolStripRenderer.OnRenderItemText method which is implemented by .Net
Framework. Below is the full code of "ToolStripRenderer.OnRenderItemText"
method:

Protected Overridable Sub OnRenderItemText(ByVal e As
ToolStripItemTextRenderEventArgs)
If (Not Me.RendererOverride Is Nothing) Then
Me.RendererOverride.OnRenderItemText(e)
Else
Dim item1 As ToolStripItem = e.Item
Dim graphics1 As Graphics = e.Graphics
Dim color1 As Color = e.TextColor
Dim font1 As Font = e.TextFont
Dim text1 As String = e.Text
Dim rectangle1 As Rectangle = e.TextRectangle
Dim flags1 As TextFormatFlags = e.TextFormat
color1 = IIf(item1.Enabled, color1, SystemColors.GrayText)
'<<<this statement will ignore our setting
If (((e.TextDirection <ToolStripTextDirection.Horizontal)
AndAlso (rectangle1.Width 0)) AndAlso (rectangle1.Height 0)) Then
Dim size1 As Size = LayoutUtils.FlipSize(rectangle1.Size)
Using bitmap1 As Bitmap = New Bitmap(size1.Width,
size1.Height, PixelFormat.Format32bppPArgb)
Using graphics2 As Graphics =
Graphics.FromImage(bitmap1)
graphics2.TextRenderingHint =
TextRenderingHint.AntiAlias
TextRenderer.DrawText(graphics2, text1,
font1, New Rectangle(Point.Empty, size1), color1, flags1)
bitmap1.RotateFlip(IIf((e.TextDirection =
ToolStripTextDirection.Vertical90), RotateFlipType.Rotate90FlipNone,
RotateFlipType.Rotate270FlipNone))
graphics1.DrawImage(bitmap1, rectangle1)
Return
End Using
End Using
End If
TextRenderer.DrawText(graphics1, text1, font1, rectangle1,
color1, flags1)
End If
End Sub

Please see the line I marked with "<<<", this code will ignore our color
setting when item is disabled. This means .Net Winform will not use our
provided text color setting if the item is disabled, so that it can keep a
consistent appearance. This behavior is by design.

If you really want to workaround this design behavior, you should not call
the "MyBase.OnRenderItemText(e)", but simulate
"ToolStripRenderer.OnRenderItemText" code logic in your method, like this:

Public Class MyRenderer
Inherits Windows.Forms.ToolStripProfessionalRenderer
Protected Overrides Sub OnRenderItemText(ByVal e As
System.Windows.Forms.ToolStripItemTextRenderEventA rgs)
If (Not e.Item.Enabled) Then
e.TextColor = Color.Red
End If

Dim item1 As ToolStripItem = e.Item
Dim graphics1 As Graphics = e.Graphics
Dim color1 As Color = e.TextColor
Dim font1 As Font = e.TextFont
Dim text1 As String = e.Text
Dim rectangle1 As Rectangle = e.TextRectangle
Dim flags1 As TextFormatFlags = e.TextFormat
'color1 = IIf(item1.Enabled, color1, SystemColors.GrayText)

TextRenderer.DrawText(graphics1, text1, font1, rectangle1,
color1, flags1)
End Sub
End Class

Note: I did not include the logic of
ToolStripProfessionalRenderer.OnRenderItemText method logic in this code
snippet, you may consider add its code in either.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 22 '07 #2
Hi Lance,

Thanks for your feedback!

To consider the e.TextDirection, I think we may repeat the
ToolStripRenderer.OnRenderItemText() code logic below:

Public Class MyRenderer
Inherits Windows.Forms.ToolStripProfessionalRenderer

Private Shared Function FlipSize(ByVal size As Size) As Size
Dim num1 As Integer = size.Width
size.Width = size.Height
size.Height = num1
Return size
End Function

Protected Overrides Sub OnRenderItemText(ByVal e As
System.Windows.Forms.ToolStripItemTextRenderEventA rgs)

If (TypeOf e.Item Is ToolStripMenuItem AndAlso (e.Item.Selected
OrElse e.Item.Pressed)) Then
e.DefaultTextColor = e.Item.ForeColor
End If

If (Not e.Item.Enabled) Then
e.TextColor = Color.Red
End If

Dim item1 As ToolStripItem = e.Item
Dim graphics1 As Graphics = e.Graphics
Dim color1 As Color = e.TextColor
Dim font1 As Font = e.TextFont
Dim text1 As String = e.Text
Dim rectangle1 As Rectangle = e.TextRectangle
Dim flags1 As TextFormatFlags = e.TextFormat
'color1 = IIf(item1.Enabled, color1, SystemColors.GrayText)

If (((e.TextDirection <ToolStripTextDirection.Horizontal)
AndAlso (rectangle1.Width 0)) AndAlso (rectangle1.Height 0)) Then
Dim size1 As Size = FlipSize(rectangle1.Size)
Using bitmap1 As Bitmap = New Bitmap(size1.Width,
size1.Height, PixelFormat.Format32bppPArgb)
Using graphics2 As Graphics =
Graphics.FromImage(bitmap1)
graphics2.TextRenderingHint =
TextRenderingHint.AntiAlias
TextRenderer.DrawText(graphics2, text1,
font1, New Rectangle(Point.Empty, size1), color1, flags1)
bitmap1.RotateFlip(IIf((e.TextDirection =
ToolStripTextDirection.Vertical90), RotateFlipType.Rotate90FlipNone,
RotateFlipType.Rotate270FlipNone))
graphics1.DrawImage(bitmap1, rectangle1)
Return
End Using
End Using
End If

TextRenderer.DrawText(graphics1, text1, font1, rectangle1,
color1, flags1)
End Sub
End Class

Note: the original LayoutUtils.FlipSize() method is a Framework private
method which we can not use. So I repeat its code as the private method of
MyRenderer. Also, I repeated
ToolStripProfessionalRenderer.OnRenderItemText() method code logic in the
front of our OnRenderItemText() method.

Does this meet your need? If I still misunderstood you, please feel free to
tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 23 '07 #3
Hi Jeffrey,

Perfect! Thanks again for your time and effort. You've been a big help.

Lance

Jan 26 '07 #4
Just do this...

protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if ( !e.Item.Enabled )
{
e.Item.Enabled = true;
e.TextColor = Color.WhiteSmoke
e.TextFormat = TextFormatFlags.
base.OnRenderItemText( e );
e.Item.Enabled = false;
}
else
base.OnRenderItemText( e );
}

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Mar 22 '07 #5
Just do this...

protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if ( !e.Item.Enabled )
{
e.Item.Enabled = true;
e.TextColor = Color.WhiteSmoke;
base.OnRenderItemText( e );
e.Item.Enabled = false;
}
else
base.OnRenderItemText( e );
}
---
Posted via DotNetSlackers.com
Mar 22 '07 #6
Just do this...

protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if ( !e.Item.Enabled )
{
e.Item.Enabled = true;
e.TextColor = Color.WhiteSmoke;
base.OnRenderItemText( e );
e.Item.Enabled = false;
}
else
base.OnRenderItemText( e );
}
---
Posted via DotNetSlackers.com
Mar 22 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul | last post by:
Hello everyone: I am developing a VB.Net Windows Application and I am now ready to create the deployment project for it. This application needs to be installable on a different number of users...
5
by: Jason Butera | last post by:
I know that I can read/write custom properties of an object by using the following: Setting: document.all.customProp = "this"; Getting: document.all.customProp; Is there a way I can run...
4
by: | last post by:
Please, help. I created my contol, ButtonX, which subclasses System.Forms.Windows.Button class. I am doing my own paiting, by overriding OnPaint and OnPaintBackground (without calling base...
5
by: Vern | last post by:
I'd like the default text to show up in designer, and allow it to be changed to something else. I was able to change some of the other properties, but for some reason I'm having trouble with the...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
6
by: Budhi Saputra Prasetya | last post by:
Hi All, I'm trying to display .NET Custom Control (created using Inherited Control) on an ASPX page, but no luck. I already registered the Control to Global Assembly Cache through .NET Framework...
5
by: Blue | last post by:
We have a custom word processing type editor built with C# and .NET 2.0 and we need to support typing in languages other than English. I want to be able to use the Windows IME to enter in text...
1
by: Saww | last post by:
Hello lately i have been trying to make a custom combobox with a few targets. 1. to create a disabled items 2. to create a seperator item I already managed both properties items. i also managed...
3
by: rarobbi | last post by:
I created a custom label so I can write characters in apex and also in this way: ex.: Ac. Now I have a problem: when my CustomLabel is in a disabled groupBox , the label does not appear...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.