11 Replies - 16553 Views - Last Post: 29 August 2008 - 01:46 PM Rate Topic: -----

#1 dobbersp  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 10-June 08

Enabled textbox will not update ForeColor

Posted 10 June 2008 - 03:24 PM

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:
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 June 2008 - 03:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Enabled textbox will not update ForeColor

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: Enabled textbox will not update ForeColor

Posted 10 June 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.
Was This Post Helpful? 0
  • +
  • -

#3 dobbersp  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 10-June 08

Re: Enabled textbox will not update ForeColor

Posted 10 June 2008 - 03:42 PM

View Postjayman9, on 10 Jun, 2008 - 03:36 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: Enabled textbox will not update ForeColor

Posted 10 June 2008 - 04:12 PM

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.
Was This Post Helpful? 0
  • +
  • -

#5 dobbersp  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 10-June 08

Re: Enabled textbox will not update ForeColor

Posted 10 June 2008 - 04:49 PM

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?
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: Enabled textbox will not update ForeColor

Posted 10 June 2008 - 06:04 PM

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.
Was This Post Helpful? 0
  • +
  • -

#7 dobbersp  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 10-June 08

Re: Enabled textbox will not update ForeColor

Posted 10 June 2008 - 07:21 PM

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.
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: Enabled textbox will not update ForeColor

Posted 10 June 2008 - 07:47 PM

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.

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.
Was This Post Helpful? 0
  • +
  • -

#9 dobbersp  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 10-June 08

Re: Enabled textbox will not update ForeColor

Posted 10 June 2008 - 08:02 PM

coincidentally, I had already tried that exact same approach after your first reply to this thread.

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



It didnt work. :(


this is the entire subroutine that executes the changes. It doesn't toggle the read-only attribute:
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


Was This Post Helpful? 1

#13 LArendt  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 03-July 08

Re: Enabled textbox will not update ForeColor

Posted 03 July 2008 - 10:49 AM

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:

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

Was This Post Helpful? 1

#14 dobbersp  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 10-June 08

Re: Enabled textbox will not update ForeColor

Posted 06 July 2008 - 04:43 PM

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: 06 July 2008 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#15 sunielreddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-August 08

Re: Enabled textbox will not update ForeColor

Posted 29 August 2008 - 01:46 PM

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")
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1