Label font color change doesn't take effect

S

STom

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
 
M

Morten Wennevik

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;
 
S

STom

No, the testing condition does work, I can debug step right through the
code.

I'll try the changes you suggest.

sTom
 
S

STom

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
 
M

Morten Wennevik

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?
 
S

STom

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
 
M

Morten Wennevik

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,

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top