11 Replies - 18479 Views - Last Post: 07 March 2009 - 02:31 AM Rate Topic: -----

#1 GHyman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-November 07

Need font to change red when the number is negative

Post icon  Posted 10 November 2007 - 04:06 PM

I have written a code that when the click the calculation the button a inputbox pop up and you enter in the income and then you enter in the expense.

The problem that I am having is I want the number in the profit lose label to turn red if the the number is negative.

I used this code and it turned all the labels red except the one that I wanted.
lbl.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold);
lbl.ForeColor = Color.Red;



any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Need font to change red when the number is negative

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Need font to change red when the number is negative

Posted 10 November 2007 - 06:39 PM

I am going to assume this is .NET related, so I am moving it to the VB.NET forum.

If this is for VB6, let me know and I will move it to the Visual Basic forum.

Please use :code: tags when posting
Was This Post Helpful? 0
  • +
  • -

#3 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: Need font to change red when the number is negative

Posted 11 November 2007 - 09:14 AM

well it should probably be something like
if lblnumber < 0 then
lblnumber.forecolor = color.red


Was This Post Helpful? 0
  • +
  • -

#4 modernic2008  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 25-October 07

Re: Need font to change red when the number is negative

Posted 11 November 2007 - 10:12 AM

I don't think that would be as easy as falling off a log. I didn't hear about a data type that deals with negative numbers. Off the top of my head, Integer only deals with whole numbers and Double only deals with positive numbers. They ARE limited, actually, but the last number is great enough I guess. If users will not insert except numbers, then this could work:
If TextBox1.Text.StartsWith("-") = True Then
			lblnumber.ForeColor = Color.Red
		Else
			lblnumber.ForeColor = Color.Black
		End If



By that code, whenever lblnumber (Which I dealt with as a textbox.) starts with a "-", TA TA TA TAH, the input becomes red. If lblnumber starts with another character, then the input becomes black.

This has its own withdraws, though. This doesn't depend on the value of the input, it depends on what character starts the input. Thus, if some so clever user wrote something like "- means minus," lblnumber.Forecolor would still turn to red. It's not a big deal, huh?

This post has been edited by modernic2008: 11 November 2007 - 10:16 AM

Was This Post Helpful? 1

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Need font to change red when the number is negative

Posted 11 November 2007 - 10:50 AM

Or you could just simply check to see if it's less than zero:

If TextBox1.Text < 0  Then
		lblnumber.ForeColor = Color.Red
Else
	  lblnumber.ForeColor = Color.Black
End If



Also, with your solution you only need the StartsWith("-") there was no need to add the = True, like this

If TextBox1.Text.StartsWith("-")  Then
	 lblnumber.ForeColor = Color.Red
Else
	  lblnumber.ForeColor = Color.Black
End If


Was This Post Helpful? 0
  • +
  • -

#6 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: Need font to change red when the number is negative

Posted 11 November 2007 - 10:52 AM

psychocoder, i like the way you did it.
Was This Post Helpful? 0
  • +
  • -

#7 modernic2008  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 25-October 07

Re: Need font to change red when the number is negative

Posted 12 November 2007 - 05:50 AM

View PostPsychoCoder, on 11 Nov, 2007 - 10:50 AM, said:

Or you could just simply check to see if it's less than zero:

If TextBox1.Text < 0  Then
		lblnumber.ForeColor = Color.Red
Else
	  lblnumber.ForeColor = Color.Black
End If



I tried doing that, and the compiler displayed an error message. As I told you, both integers and doubles seem to include only number characters. (and a "." in the case of doubles.) A "-" would start an error as it's not a number character or even a "."! If there is a data type that deals with a "-" as a part of the number, please tell me about it.

Thanks.

This post has been edited by modernic2008: 12 November 2007 - 06:04 AM

Was This Post Helpful? 0
  • +
  • -

#8 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: Need font to change red when the number is negative

Posted 12 November 2007 - 11:00 AM

ok numbers that can be negative are:
decimal
double
single
integer
long
short
Was This Post Helpful? 0
  • +
  • -

#9 modernic2008  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 25-October 07

Re: Need font to change red when the number is negative

Posted 12 November 2007 - 02:00 PM

View Postferrari12508, on 12 Nov, 2007 - 11:00 AM, said:

ok numbers that can be negative are:
decimal
double
single
integer
long
short

How to make their value negative?

The "-" sign doesn't work. Well, to be candid, when I write a "-" in the textbox, the compiler doesn't display an error message; when I write a number after it, however, the problem begins.
Was This Post Helpful? 0
  • +
  • -

#10 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: Need font to change red when the number is negative

Posted 12 November 2007 - 02:20 PM

are u kidding me? you seriously need to google this and see that they can be negative.
Was This Post Helpful? 0
  • +
  • -

#11 modernic2008  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 25-October 07

Re: Need font to change red when the number is negative

Posted 13 November 2007 - 05:08 AM

View Postferrari12508, on 12 Nov, 2007 - 02:20 PM, said:

are u kidding me? you seriously need to google this and see that they can be negative.


Well, I don't know. I just hope it worked well with you, Ghyman.

This post has been edited by modernic2008: 13 November 2007 - 05:08 AM

Was This Post Helpful? 0
  • +
  • -

#12 WarMacre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 09

Re: Need font to change red when the number is negative

Posted 07 March 2009 - 02:31 AM

I know this post is old, but I just had to reply. Some people just don't seems to understand types or type casting.

Asking if a string (textbox.text) is less than zero makes no sense. Essentially, the CLR thinks you are asking if it is 'false', not a negative number.

To check if a NUMBER is negative, first make sure that it is a number.

"125" is not a number!!! 125 is a number. "125" is a string and has to be converted to a number to be evaluated as one.

Example:

if cint(textbox1.text) < 0 then
blah....


CInt converts to an integer. Other number type converters include Cdbl (to a Double), Csng (to a Single), Clng (to a Long), cdec (to a Decimal), etc.

I would always recommend turning Option Strict on as warnings such as implicite conversions from one type to another (bad!!!) will show up PRE-COMPILE as errors, making you a much better programmer. ;)

KNOW THY TYPE PEOPLE!!!

Good Luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1