PDA

View Full Version : OT: Visual Basic (2010)



KJ1I
10-26-2014, 07:50 PM
Yes, I know this isn't a programming forum. But after 26 years in a cubicle, I'm not interested in doing all that much programming.

So, I'm working on a program to do calculations for one of the projects I'm working on in the shop. I'm using VB Studio 2010 (because it's what I've got, and no, I'm not interested in using FreeB# or SuperCme or whatever). In validating all the inputs to make sure there are no typos, I have a text box used to display error messages, both system (... Catch ... FormatException ...) and programmed (... value X must be greater than value Y ...). The message text (txtErrorText.Text = "Bad Boy") is set when an error is detected. As part of the routine, I want to change the color of both the text and background of the text box. So here is the relevant part of the code:

...
If booleanErrorOccurred Then
txtErrorText.BackColor = Color.Red
txtErrorText.ForeColor = Color.White
End If
...

Here's the problem: the background color changes to whatever "Color." I select, but the color of the text itself (ForeColor) never changes. It is always displayed a black.

Yes, I can live with black error text, but I'd really like to be able to set it to whatever value I want. I just don't understand why one 'set color' works and the other doesn't.

And if no one sees the answer, I'll just go back to searching through all the c**p Google returns.

dp
10-26-2014, 08:00 PM
What is the scope of txtErrorText?

KJ1I
10-26-2014, 08:10 PM
It is defined on the associated form and accessed in -

Public Class Dimension_Calculator
...
Private Sub buttonProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonProcess.Click
...
End Sub
End Class

as are all the other fields (both input and output display) on the form.

iMisspell
10-26-2014, 08:13 PM
WOW its been years (VB6) since ive played with this stuff.
Would it be eazyer to use a 'Label' instead of a text box.

Used to use: http://www.vbforums.com and they where a ton of help.

Just some thoughts if they haven't already crossed your mind.

lwalker
10-26-2014, 08:53 PM
I assume you're using VB.NET? I don't know VB, but I have been working on a program today that does exactly what you are doing to a collection of buttons. It's in C#, but that shouldn't make a difference: the .NET CLR works the same for all supported languages.

Are you printing the error message before or after setting the ForeColor? The control won't update existing text, so the new color only takes effect the next time you write to it.

KJ1I
10-26-2014, 09:24 PM
Are you printing the error message before or after setting the ForeColor?

I'm displaying the message after setting the controls. Remember, the set BackColor works (changes the background to whatever I specify and then returns it to "standard" on "Reset"), its just the set ForeColor that doesn't work.

RichR
10-26-2014, 11:06 PM
Hi Kevin
What happens if you reverse the order, set forecolor and then backcolor.

JRouche
10-26-2014, 11:45 PM
If you dont "name" the color it reverts to the "standard", which is black. You have to call out all, type, script and color. JR

Paul Alciatore
10-27-2014, 12:35 AM
I don't "know", but I would try:

txtErrorText.Color = Color.White

Objects in VB and other OOLs always confuse me and they are never well documented.

KJ1I
10-27-2014, 10:24 AM
Hi Kevin
What happens if you reverse the order, set forecolor and then backcolor.

Yup, I tried that with the exact same result.

I spent some time this morning sifting through all the Google returns for setting Forecolor. I found the topic on Experts Exchange. It turns out the problem is (was) the Enabled property of the text box. I had set it to false so that the end user doesn't see the text box and on the active form it appears invisible unless there is an error message to display. Well, MS, in its infinite wisdom, because they know far, far, better than you do how your program should be written, removed the ability to change the text color in a "disabled" text box (Enabled = False). The example of how to change the text color was about 100 lines long and more work than I was willing to go to. They did offer a shorter solution - set Enabled to True then set Read Only to True (its normally False) and Tab Stop to False (its normally True). Doesn't quite accomplish the exact same protection of the field but its close enough in the instance. The program is for me, in my shop, but you know how it goes - the old programmer mentality raising its head.

lwalker
10-27-2014, 01:12 PM
I assume if you set enabled true, set color, update the textbox, then set enabled false, that doesn't work?

CCWKen
10-27-2014, 03:17 PM
Try a rtf textbox.

KJ1I
10-27-2014, 11:23 PM
I assume if you set enabled true, set color, update the textbox, then set enabled false, that doesn't work?

I hadn't thought to do that. Next time I'm in the program for the inevitable updates, I'll give that a try and see if it works.


Try a rtf textbox.

Tried that. The rtf text box behaves exactly like the 'standard' textbox when Enabled = False.

PS: Thanks for the ideas guys, I do appreciate it. If it doesn't get me there straightaway, at least it gives me a different viewpoint to look from.