How to color a part of the text c#?

Payroll Program Help Needed (Visual Basic .Net)? Part 1?

  • If there is a loss, the Profit or Loss BackColor is red and its ForeColor is white. If there is a Profit, the BackColor is light-grey and the ForeColor is Black.The Commission BackColor is light-grey and the ForeColor is black. Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click Dim decSellingPrice As Decimal Dim decCost As Decimal Dim decProfit As Decimal Dim decLoss As Decimal If (lblProfitorLoss.Text = decProfit & Not 0) Then lblProfitorLoss.BackColor = Color.LightGray Else lblProfitorLoss.BackColor = Color.Red End If If (lblProfitorLoss.Text = decProfit & Not 0) Then lblProfitorLoss.ForeColor = Color.Black Else lblProfitorLoss.ForeColor = Color.White End If End Sub This is the color change part I think it is right. Please comment on it.

  • Answer:

    There are many things you can do to improve this. I am also a bit puzzled by one thing. First the improvements.... 1) Since the two main if statements have the same test condition, you can combine them and cut out a lot of lines. If (lblProfitorLoss.Text = decProfit & Not 0) Then lblProfitorLoss.BackColor = Color.LightGray lblProfitorLoss.ForeColor = Color.Black Else blProfitorLoss.BackColor = Color.Red lblProfitorLoss.ForeColor = Color.White End if 2) Remember when you compare something like text with something like a decimal, you will want to explicitly convert your text to decimal. If (Convert.ToDouble(lblProfitorLoss.Text) = decProfit) Then ... 3) Related to number 2, you can also then put in exception handling in case what is entered in the label is not a number it will get caught by the conversion. Now the last thing, I don't think I have ever seen the use of & NOT 0 in your if statements. You might want to take a look at that again and make sure the syntax is correct. It might cause a misevaluation. That should help you improve that sub. Good luck!

bigboywa... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.