473,785 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specify custom disabled text color in ToolStripProfes sionalRendere

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

Public Class MyRenderer
Inherits Windows.Forms.T oolStripProfess ionalRenderer
Protected Overrides Sub OnRenderItemTex t(ByVal e As
System.Windows. Forms.ToolStrip ItemTextRenderE ventArgs)
If (Not e.Item.Enabled) Then
e.TextColor = Color.Red
End If
MyBase.OnRender ItemText(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.T extRenderer.Dra wText. 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 ToolStripTextDi rection.Inherit .

Any workarounds or ways to simplify drawing the text myself?

Thanks for any help!
Lance

Jan 19 '07 #1
6 3735
Hi Lance,

Based on my understanding, you wanted to create a customized
ToolStripRender inherits from ToolStripProfes sionalRenderer to customize
the text display color of each item. However, you find that when the item
is disabled, your setting in ToolStripProfes sionalRenderer. OnRenderItemTex t
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
OnRenderItemTex t, "MyBase.OnRende rItemText(e)" in your code will call the
ToolStripRender er.OnRenderItem Text method which is implemented by .Net
Framework. Below is the full code of "ToolStripRende rer.OnRenderIte mText"
method:

Protected Overridable Sub OnRenderItemTex t(ByVal e As
ToolStripItemTe xtRenderEventAr gs)
If (Not Me.RendererOver ride Is Nothing) Then
Me.RendererOver ride.OnRenderIt emText(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.Enabl ed, color1, SystemColors.Gr ayText)
'<<<this statement will ignore our setting
If (((e.TextDirect ion <ToolStripTextD irection.Horizo ntal)
AndAlso (rectangle1.Wid th 0)) AndAlso (rectangle1.Hei ght 0)) Then
Dim size1 As Size = LayoutUtils.Fli pSize(rectangle 1.Size)
Using bitmap1 As Bitmap = New Bitmap(size1.Wi dth,
size1.Height, PixelFormat.For mat32bppPArgb)
Using graphics2 As Graphics =
Graphics.FromIm age(bitmap1)
graphics2.TextR enderingHint =
TextRenderingHi nt.AntiAlias
TextRenderer.Dr awText(graphics 2, text1,
font1, New Rectangle(Point .Empty, size1), color1, flags1)
bitmap1.RotateF lip(IIf((e.Text Direction =
ToolStripTextDi rection.Vertica l90), RotateFlipType. Rotate90FlipNon e,
RotateFlipType. Rotate270FlipNo ne))
graphics1.DrawI mage(bitmap1, rectangle1)
Return
End Using
End Using
End If
TextRenderer.Dr awText(graphics 1, 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.OnRende rItemText(e)", but simulate
"ToolStripRende rer.OnRenderIte mText" code logic in your method, like this:

Public Class MyRenderer
Inherits Windows.Forms.T oolStripProfess ionalRenderer
Protected Overrides Sub OnRenderItemTex t(ByVal e As
System.Windows. Forms.ToolStrip ItemTextRenderE ventArgs)
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.Enabl ed, color1, SystemColors.Gr ayText)

TextRenderer.Dr awText(graphics 1, text1, font1, rectangle1,
color1, flags1)
End Sub
End Class

Note: I did not include the logic of
ToolStripProfes sionalRenderer. OnRenderItemTex t 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
ToolStripRender er.OnRenderItem Text() code logic below:

Public Class MyRenderer
Inherits Windows.Forms.T oolStripProfess ionalRenderer

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 OnRenderItemTex t(ByVal e As
System.Windows. Forms.ToolStrip ItemTextRenderE ventArgs)

If (TypeOf e.Item Is ToolStripMenuIt em AndAlso (e.Item.Selecte d
OrElse e.Item.Pressed) ) Then
e.DefaultTextCo lor = e.Item.ForeColo r
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.Enabl ed, color1, SystemColors.Gr ayText)

If (((e.TextDirect ion <ToolStripTextD irection.Horizo ntal)
AndAlso (rectangle1.Wid th 0)) AndAlso (rectangle1.Hei ght 0)) Then
Dim size1 As Size = FlipSize(rectan gle1.Size)
Using bitmap1 As Bitmap = New Bitmap(size1.Wi dth,
size1.Height, PixelFormat.For mat32bppPArgb)
Using graphics2 As Graphics =
Graphics.FromIm age(bitmap1)
graphics2.TextR enderingHint =
TextRenderingHi nt.AntiAlias
TextRenderer.Dr awText(graphics 2, text1,
font1, New Rectangle(Point .Empty, size1), color1, flags1)
bitmap1.RotateF lip(IIf((e.Text Direction =
ToolStripTextDi rection.Vertica l90), RotateFlipType. Rotate90FlipNon e,
RotateFlipType. Rotate270FlipNo ne))
graphics1.DrawI mage(bitmap1, rectangle1)
Return
End Using
End Using
End If

TextRenderer.Dr awText(graphics 1, text1, font1, rectangle1,
color1, flags1)
End Sub
End Class

Note: the original LayoutUtils.Fli pSize() 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
ToolStripProfes sionalRenderer. OnRenderItemTex t() method code logic in the
front of our OnRenderItemTex t() 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 OnRenderItemTex t(ToolStripItem TextRenderEvent Args e)
{
if ( !e.Item.Enabled )
{
e.Item.Enabled = true;
e.TextColor = Color.WhiteSmok e
e.TextFormat = TextFormatFlags .
base.OnRenderIt emText( e );
e.Item.Enabled = false;
}
else
base.OnRenderIt emText( e );
}

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

protected override void OnRenderItemTex t(ToolStripItem TextRenderEvent Args e)
{
if ( !e.Item.Enabled )
{
e.Item.Enabled = true;
e.TextColor = Color.WhiteSmok e;
base.OnRenderIt emText( e );
e.Item.Enabled = false;
}
else
base.OnRenderIt emText( e );
}
---
Posted via DotNetSlackers. com
Mar 22 '07 #6
Just do this...

protected override void OnRenderItemTex t(ToolStripItem TextRenderEvent Args e)
{
if ( !e.Item.Enabled )
{
e.Item.Enabled = true;
e.TextColor = Color.WhiteSmok e;
base.OnRenderIt emText( e );
e.Item.Enabled = false;
}
else
base.OnRenderIt emText( 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
2036
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 / clients, and the application has an Access DB out on their network. How can I create a deployment routine where I can have a dialog box prompt for the network share (i.e. \\server\dbfolder) at install-time? Once network the share is specified...
5
2212
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 code when this custom property is set. Or perhaps there is a way to create a custom method?
4
5678
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 class's OnPaint & OnPaintBackground). My button has a shape of rectangle with rounded corners and is filled with gradient brush, where user specifies the gradient colors. The dilema I have now is how to paint the button when it's disabled (Enabled =...
5
6733
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 text. Here's the code from my latest attempt. I initially tried just setting this.Text = "E&xit" but that didn't work. Any ideas? public class ButtonExit : Button {
8
7837
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 VALUE ENTERED BY THE USER EXISTS IN THE DB , then THE ERROR MESSAGE OF THE COMPARE VALIDATOR SHOULD BE DISPLAYED. For this, I used the reference artiicle "http://msdn.microsoft.com/library/default.asp?url=/library/en-...
6
3521
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 1.1 Configuration. I have also put a reference to the control on my ASP .NET project. The view that I get is only a disabled text area. Below is the code that I'm using:
5
19704
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 similar to how it works in a text box. For example, if I put focus on a TextBox and then switch my language on the language bar to Japanese or Chinese, several buttons appear on the language bar including Input Mode, Input Style, Conversion Mode,...
1
3681
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 to draw both items as i want(disabled in gray, seperator as a line). the problem is the user can click on the items, both the line(since it's actually a '-' item) of the seperator item, and the disabled item. i know i should overrides the WndProc...
3
1160
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 disabled because the text I write with DrawText is g.DrawString(text,usedFont, new SolidBrush(this.ForeColor),startPosition); How can I solve this problem? How can I render a disabled label?
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.