Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,097 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,869 people online right now. Registration is fast and FREE... Join Now!




Enabled textbox will not update ForeColor

 
Reply to this topicStart new topic

Enabled textbox will not update ForeColor

dobbersp
10 Jun, 2008 - 02:24 PM
Post #1

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 18



Thanked: 1 times
My Contributions
I have a textbox on a form that I want to dynamically change the color of based on its value.
(it will keep track of a number, and if that number is above a certain value I want the text to turn red.)

The textbox is enabled and read-only.

I am using VB 2008 Express Edition, and my code looks as follows:
CODE
If Val(BPPosQual.Text) > 35 Then
            BPPosQual.ForeColor = Color.Red
        Else
            BPPosQual.ForeColor = Color.Black
        End If


When trying to debug this problem, I put messagebox.show() inside of the if statement, and had it output the color, which ended up being as i expected (red above 35, and black anywhere else)

I have another text box with the exact same code(aside from the textbox name) that works. So I am thoroughly perplexed. If anyone can offer some explanation as to why, I would be grateful.

Thanks.

This post has been edited by dobbersp: 10 Jun, 2008 - 02:24 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Enabled Textbox Will Not Update ForeColor
10 Jun, 2008 - 02:36 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
The problem is because it is Read Only. If you want to display text that a user cannot edit, then use a Label control.

Textboxes are best used when accepting input from a user. Labels are used to only display information that the user cannot edit.
User is offlineProfile CardPM
+Quote Post

dobbersp
RE: Enabled Textbox Will Not Update ForeColor
10 Jun, 2008 - 02:42 PM
Post #3

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 18



Thanked: 1 times
My Contributions
QUOTE(jayman9 @ 10 Jun, 2008 - 03:36 PM) *

The problem is because it is Read Only. If you want to display text that a user cannot edit, then use a Label control.

Textboxes are best used when accepting input from a user. Labels are used to only display information that the user cannot edit.


regardless of whether i should be using a label or a textbox, I want to know why it does not work. I have another read only text box that does function with the identical code, so the read only aspect cannot be the reason.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Enabled Textbox Will Not Update ForeColor
10 Jun, 2008 - 03:12 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
I just told you why you can't change the color, it is because it is Read Only. If a TextBox is marked as Read Only, then you cannot change the attributes of the text.


User is offlineProfile CardPM
+Quote Post

dobbersp
RE: Enabled Textbox Will Not Update ForeColor
10 Jun, 2008 - 03:49 PM
Post #5

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 18



Thanked: 1 times
My Contributions
you must be mistaken:

Attached Image



This is a screen grab of the application in debug. As you can see, the total textbox (which is identical in attributes to the stats and qualities textboxes) changes colors dynamically. It is read-only.

As I understand it, you cannot change the contents of the readonly textbox, but you can alter some of the attributes.
I'm still new to VB, so I'm willing to admit that I do not know the underlying actions that occur when a textbox refreshes and that sort of thing, but this baffles me that one textbox will dynamically refresh and the other will not.

Any Ideas?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Enabled Textbox Will Not Update ForeColor
10 Jun, 2008 - 05:04 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Can you post a screen shot of the properties for your Totals textbox, the one that is supposedly working correctly?

Expand it out so I can see all the properties.
User is offlineProfile CardPM
+Quote Post

dobbersp
RE: Enabled Textbox Will Not Update ForeColor
10 Jun, 2008 - 06:21 PM
Post #7

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 18



Thanked: 1 times
My Contributions
Sure.

Here you go:

Attached Image


I had to stitch three screen shots together to get the whole list of attributes.
As you can see, it's read only.

As of right now, I have resorted to changing the background color of the boxes instead of the foreground (which is a workaround that i dislike, but it works sorta)

Also of note, I changed the box that didnt dynamically change colors to a read-only=false , and it changed colors. This supports your idea that the readonly is the reason that it cannot change colors, but I don't want it to be editable.
I would use a label but it is already far enough entrenched in my program for me to seek alternatives, and the fact that the total textbox works gives me hope.

i appreciate your help so far, and as always, ideas as to how i can get this textbox to work as the total one does would be greatly appreciated.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Enabled Textbox Will Not Update ForeColor
10 Jun, 2008 - 06:47 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
A better work around would be to set the Read Only property to false programmatically, change the forecolor, and then set the Read Only property to true when you are done. This will give you the effect you are looking for.

CODE

If Val(BPPosQual.Text) > 35 Then
            BPPosQual.ReadOnly = False
            BPPosQual.ForeColor = Color.Red
            BPPosQual.ReadOnly = True
        Else
            BPPosQual.ReadOnly = False
            BPPosQual.ForeColor = Color.Black
            BPPosQual.ReadOnly = True
End If


I am wondering if you are not already doing that with the textbox that is working correctly.
User is offlineProfile CardPM
+Quote Post

dobbersp
RE: Enabled Textbox Will Not Update ForeColor
10 Jun, 2008 - 07:02 PM
Post #9

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 18



Thanked: 1 times
My Contributions
coincidentally, I had already tried that exact same approach after your first reply to this thread.

CODE

BPPosQual.ReadOnly = False
            BPPosQual.ForeColor = Color.Red
            BPPosQual.ReadOnly = True


It didnt work. sad.gif


this is the entire subroutine that executes the changes. It doesn't toggle the read-only attribute:
CODE

Private Sub UpdateBuildPoints()
        'add all BP fileds together
        BPTotal.Text = (Val(BPStats.Text) + Val(BPRace.Text) + Val(BPEdge.Text) + Val(BPMagic.Text) + Val(BPSkills.Text) + Val(BPKnow.Text) + Val(BPPosQual.Text) + Val(BPNegQual.Text) + Val(BPResources.Text) + Val(BPContacts.Text)).ToString
        'Font Color for total
        If Val(BPTotal.Text) > MaxBuildPoints Then
            BPTotal.ForeColor = Color.Red
        Else
            BPTotal.ForeColor = Color.Black
        End If

        'font color for stats

        If Val(BPStats.Text) > MaxBuildPoints / 2 Then
            BPStats.ForeColor = Color.Red
        Else
            BPStats.ForeColor = Color.Black
        End If

        'Font Color for positive qualities
        If Val(BPPosQual.Text) > 35 Then
            BPPosQual.ForeColor = Color.Red
        Else
            BPPosQual.ForeColor = Color.Black
        End If

        'Font Color for negative qualities
        If Val(BPNegQual.Text) < -35 Then
            BPNegQual.ForeColor = Color.Red
        Else
            BPNegQual.ForeColor = Color.Black
        End If

    End Sub

User is offlineProfile CardPM
+Quote Post

LArendt
RE: Enabled Textbox Will Not Update ForeColor
3 Jul, 2008 - 09:49 AM
Post #10

New D.I.C Head
*

Joined: 3 Jul, 2008
Posts: 1



Thanked: 1 times
My Contributions
I just encountered the same issue when using a TextBox set to ReadOnly.

It seems that the TextBox.ForeColor is ignored unles you also set the TextBox.BackColor.

Suppose I have a TextBox called Messages on some Forms dialog, and I want all text in the TextBox to be Red, then I get arround this issue by doing the following:

CODE
this.Messages.ForeColor = Color.Red;
this.Messages.BackColor = this.Messages.BackColor; // leave it unchanged


User is offlineProfile CardPM
+Quote Post

dobbersp
RE: Enabled Textbox Will Not Update ForeColor
6 Jul, 2008 - 03:43 PM
Post #11

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 18



Thanked: 1 times
My Contributions
thanks a lot!!
problem solved.

I had tried calls to refresh the components and everything. I suppose that when the background color is updated it will redraw the component.

thanks again.

This post has been edited by dobbersp: 6 Jul, 2008 - 04:46 PM
User is offlineProfile CardPM
+Quote Post

sunielreddy
RE: Enabled Textbox Will Not Update ForeColor
29 Aug, 2008 - 12:46 PM
Post #12

New D.I.C Head
*

Joined: 29 Aug, 2008
Posts: 1

If TextBox's ReadOnly property is "true", postback data won't be loaded e.g it essentially means TextBox being readonly from server-side standpoint (client-side changes will be ignored).

if you want to make it readonly do it lik

textbox.attributes.add("readonly","readonly")
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:18AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month