Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2005

Tip: Looking for answers? Try searching our database.

Label font color change doesn't take effect

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
STom - 16 Jan 2005 00:26 GMT
I pass my label control by reference into a function in order to change the font color. If the value in the label is a negative number, I will change the color to red like this;

lbl.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!)
lbl.Font = New System.Drawing.Font(lbl.Font, FontStyle.Bold)
lbl.ForeColor = System.Drawing.Color.Red

However, the font color doesn't not take effect, its color is still black. Is there some sort of screen refresh I need to do to get this to work?

Thanks.

STom
Morten Wennevik - 16 Jan 2005 01:51 GMT
Hi STom.

I suspect your testing condition isn't working correctly so your code will  
never run.

There is nothing wrong with your code other than perhaps one surplus code  
line (first and second line can be joined) and some typos.  No need to  
call refresh as the Label will repaint itself.

lbl.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold);
lbl.ForeColor = Color.Red;

Signature

Happy Coding!
Morten Wennevik [C# MVP]

STom - 16 Jan 2005 02:02 GMT
No, the testing condition does work, I can debug step right through the
code.

I'll try the changes you suggest.

sTom
> Hi STom.
>
[quoted text clipped - 7 lines]
> lbl.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold);
> lbl.ForeColor = Color.Red;
STom - 16 Jan 2005 02:23 GMT
Correction, the color of the font is changing, however, the lines of code above it:

Here, instead of having a minus value, I use parenthesis. The text ends up keeping the minus value and the ( ) get lost somewhere.
lbl.Text = "(" & lbl.Text.Replace("-", "") & ")"

STom

> Hi STom.
>
[quoted text clipped - 7 lines]
> lbl.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold);
> lbl.ForeColor = Color.Red;
Morten Wennevik - 16 Jan 2005 09:15 GMT
Well, other than your line being VB.NET code there is nothing wrong with  
it.
This code should work just fine in C#:

lbl.Text = "(" + lbl.Text.Replace("-", "") + ")";
lbl.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold);
lbl.ForeColor = Color.Red;

I can successfully change "-123" to "(123)" in red fat bold types.
Can you show us the entire method? or a small but complete sample that  
demonstrates your problem?

> lbl.Text = "(" & lbl.Text.Replace("-", "") & ")"

Signature

Happy Coding!
Morten Wennevik [C# MVP]

STom - 16 Jan 2005 13:10 GMT
Morten,

The way I call this method is like:
 Me.FormatDollarsLabelForNegative(lblExpenseChgValue)

This method is called whenever the application first opens AND whenever a
new calculation (elsewhere in the app) is performed that would change these
values.

What I see happening is that after the line of code above, if I look at the
'lblExpenseChgValue.Text' value in the command window, I will see:
($508,305). This is the correct format. However, the color of the font seems
to come out correctly but the '-' sign appears adjacent to the number
instead of the '( )'.

So, in the code below if I change Red to Green, the color of the font will
be green. It seems that the change to the 'text' property is the problem,
not the color of the font.

The bizarre thing is, that once this window is open, if I perform a
recalculation which results in these formatting functions to be called
again, the value that appears is now correct both in format and in color.
So, it appears it is just the initial loading of the screen that is the
problem.

Private Sub FormatDollarsLabelForNegative(ByRef lbl As
System.Windows.Forms.Label)
       'the label value will already be string, we need to convert it from
string
       'back to decimal
       Dim _ldValue As Decimal
       Dim _lsReplace As String

'this method removes an current parenthesis that might already be in place
_lsReplace = RemoveParenthesis(lbl.Text)
       _lsReplace = _lsReplace.Replace("$", "")
       _lsReplace = _lsReplace.Replace(",", "")

       _ldValue = CDec(_lsReplace)

       If _ldValue < CDec(0.0) Then
           'need to remove the minus value
           lbl.Text = "(" & lbl.Text.Replace("-", "") & ")"
           lbl.Font = New System.Drawing.Font("Microsoft Sans Serif",
8.25!, FontStyle.Bold)
           lbl.ForeColor = System.Drawing.Color.Red
       Else
           lbl.Font = New System.Drawing.Font("Microsoft Sans Serif",
8.25!, FontStyle.Bold)
           lbl.ForeColor = System.Drawing.Color.Black
       End If
   End Sub

> Well, other than your line being VB.NET code there is nothing wrong with
> it.
[quoted text clipped - 9 lines]
>
>> lbl.Text = "(" & lbl.Text.Replace("-", "") & ")"
Morten Wennevik - 17 Jan 2005 11:05 GMT
Hi STom,

I think this may be a case of the Command Window not showing what is  
displayed on screen.
For instance the command window will show \r\n as "\r\n" but on screen  
they will break the line.  In other cases it may even display a wrong  
value.

I'm not sure why there would be a '-' in the command window, but there  
have been cases of odd behaviour before.

> Morten,
>
[quoted text clipped - 72 lines]
>> Happy Coding!
>> Morten Wennevik [C# MVP]

Signature

Happy Coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.