0
lblPercentageOfStudents.Text = "Percentage of Students in " & cboDegree.Text & ": " & decPercentageOfStudents.ToString("N2") & "%"

In this code I have the color of the message that displays in the label set to "gold" but I'd like to change the color of the data stored in the decPercentageOfStudents variable to "white." How do I change the color of the data stored in the decPercentageOfStudents variable without changing the color of the whole message in the label? Thank you! :-)

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Jeremy
  • 113
  • 2
  • 14
  • You're going to need some more context here. For starters, you can't change the color of the value in a *variable*. If you're talking about manipulating a form control object, it *may* be possible. Is this for Excel, Access or some other application using VBA? – David Zemens Nov 02 '15 at 03:25
  • quick tinkering in Excel seems like this probably isn't possible on an `MSForms.Label` object, which uses only the `.Forecolor` property to set the color of the label's text/Caption. If `lblPercentageofStudents` is an object that represents something that can render rich text (like an inline shape in a Word document or Excel spreadsheet), then it is certainly possible. – David Zemens Nov 02 '15 at 03:29
  • Is this text going into an Excel cell? Are you using a VBA macro or VBScript? – xXhRQ8sD2L7Z Nov 02 '15 at 03:29
  • And, the `ToString` method isn't VBA, so I would suggest revising your tags to appropriate VB.net or whatever you're actually using which isn't VBA. – David Zemens Nov 02 '15 at 03:30
  • @DavidZemens No, it's not for Excel or Access. It's in a windows form in Visual Studio 2012. – Jeremy Nov 02 '15 at 03:32
  • What actual programming language are you using? Fix your question. include context so that someone who actually knows that language can assist you. What *is* `lblPercentageOfStudents` object? Etc. I can't help you with VSTO assuming VB.net or something else but perhaps someone else will be able to. – David Zemens Nov 02 '15 at 03:38
  • Okay, thank you for your help. If it's not possible, that's okay; I can remove the decPercentageOfStudents variable from the lblPercentageOfStudents label and display the data stored in the decPercentageOfStudents variable in a different label. Thank you! :-) – Jeremy Nov 02 '15 at 03:41
  • @DavidZemens It's Visual Basic 2012 and I'm writing the code in Visual Studio 2012. The lblPercentageOfStudents is a label object in Visual Basic 2012. – Jeremy Nov 02 '15 at 03:43
  • OK then. Well I'm not 100% certain because I don't use VSTO or pure VB there, but AFAIK MSForms are fairly similar across the board so I suspect it's not possible, and your workaround of using a separate label should work. – David Zemens Nov 02 '15 at 03:45
  • Possible duplicate of [Changing one Words color in label.text](http://stackoverflow.com/questions/10233557/changing-one-words-color-in-label-text) – David Wilson Nov 03 '15 at 10:06

1 Answers1

0

You can't do it with just a label. It's simply not possible. You either need multiple labels or a rich text control.

This may help:

Change color of text within a WinForms RichTextBox

Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Is it possible to change the color in the decPercentageOfStudents variable using an "If" statement if the value in the variable met a certain condition, such as ">= 0"? – Jeremy Nov 02 '15 at 05:04
  • No, it's not possible. Variables don't have colors. Labels have colors. Labels only have one color. You need multiple labels or a rich text control. – Joel Coehoorn Nov 02 '15 at 05:15
  • String values don't have colors. **Labels** have colors, and again: _they can only have one color for the whole label_. You need multiple labels, or a rich text control of some kind. – Joel Coehoorn Nov 02 '15 at 17:28
  • Okay, so code like this would not work: decPercentageOfStudents = (intNumberOfStudents / intTotalStudents) * 100 If decPercentageOfStudents >= 0 Then decPercentageOfStudents.ForeColor = Color.White Else decPercentageOfStudents.ForeColor = Color.White End If lblPercentageOfStudents.Text = "Percentage of Students in " & cboDegree.Text & ": " & decPercentageOfStudents.ToString("N2") & "%" – Jeremy Nov 02 '15 at 17:49
  • As I've said several times already: that won't work. You need separate labels for each part of the text that uses a different color. – Joel Coehoorn Nov 02 '15 at 19:59