Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Changing backcolor and forecolor Colors on form based on changes to Text

Posted on 2006-05-06
9
Medium Priority
?
260 Views
Last Modified: 2010-04-23
Hi everyone,

I have a gui form that displays changing stock quotes in text boxes (One per symbol).

I would like to ...

1)  When a quote is changed the backgroud color will change to LightGolden Yellow but only for two seconds where it should then be changed back to original color (white).  This lets your eye catch what has just changed.

2)  When the new quote comes in if it was a down change (based on what was originally in the text box the Forcolor would change to red etc.  If up forcolor is changed to green etc.  These can stay like this till a new quote is captured.

If you could show me by example this would be great.

Let me please express my thanks in advance for your graciuos help in this area.  Hope all is well.

Best regards,
Dynotoe
0
Comment
Question by:Dynotoe
  • 5
  • 4
9 Comments
 
LVL 10

Expert Comment

by:vadim63
ID: 16623363
Public Class Form1

    Dim OldValue As Integer = 0   '  or "decimal"

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text - OldValue > 0 Then
            Me.TextBox1.ForeColor = Color.Green
        Else
            Me.TextBox1.ForeColor = Color.Red
        End If
        OldValue = Me.TextBox1.Text
        Me.TextBox1.BackColor = Color.LightGoldenrodYellow
        Timer1.Interval = 2000   ' set 2 seconds interval
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.TextBox1.BackColor = Color.White
        Timer1.Stop()
    End Sub

End Class
0
 

Author Comment

by:Dynotoe
ID: 16623654
Hi Vadim,

Thanks for your help.   2 Question...

1).Is there a way to virtually set these up if I have MANY MANY number of text boxes to monitor or do I have to maually write these "TextBoxX_TextChanged() evnt handlers for every text box separately?

2).Also do I need a separate Private Sub TimerX_Tick() for each TextBoxX_Changed() event OR is it posible to use just one Timer1_Tick method where I can pass in the text box also.  Remeber that there might be simutaneous quotes coming in so I want them to all work without waiting for let's say a timer_Tick method to finish before it can be used again etc.

Hope you are well and thanks again.

Cheers,

Dynotoe
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16623784
In this case, you can build your own user control. Eg:

Public Class UserControl1
    Dim OldValue As Integer = 0
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text - OldValue > 0 Then
            Me.TextBox1.ForeColor = Color.Green
        Else
            Me.TextBox1.ForeColor = Color.Red
        End If
        OldValue = Me.TextBox1.Text
        Me.TextBox1.BackColor = Color.LightGoldenrodYellow
        Timer1.Interval = 2000   ' set 2 seconds interval
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.TextBox1.BackColor = Color.White
        Timer1.Stop()
    End Sub
End Class

====================================

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim MyTextbox As New UserControl1
        MyTextbox.Width = 100
        Me.Controls.Add(MyTextbox)
    End Sub
End Class
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:vadim63
ID: 16624476
Are you using VB 2005? If so, I have a complete solution for you.
0
 

Author Comment

by:Dynotoe
ID: 16625067
Hi Vadim,

I'm using. 2003.net actually.

Does the User Control have to be it's own Class or can it be part of the Form class that contains sll the TextBoxes?

Do you have this in C# as well?

Great help Vadim.  Very appreciated.

Best,
Dynotoe
0
 

Author Comment

by:Dynotoe
ID: 16625103
Also is it easy to implement the "User Control" given the fact that all the guis forms are set up already with all the text boxes and code exists in the windows designer area for them etc.?

Thx
0
 
LVL 10

Accepted Solution

by:
vadim63 earned 2000 total points
ID: 16625861
Unfortunately, I don't have 2003.net and C#, but VB2005 only.
 
In VB:
 
Open a new Project. Select the 'Windows Control Library' icon from the New Project - Templates Window;

Name it "MyTextBox";

This will create a Solution which contains a Project named MyTextBox, which in turn contains an object named UserControl1.vb;

Highlight the name 'UserControl1.vb' in the Solution Explorer and change it to MyTextBox.vb;

Rename the Class itself. So open the MyTextBox.vb's code window and change the line which reads:

Public Class UserControl1

So that it reads:

Imports System.ComponentModel
<ToolboxBitmap(GetType(TextBox))> _
Public Class MyTextBox

Still in the Code Window, change the line that reads:

Inherits System.Windows.Forms.UserControl

So that it reads:

Inherits System.Windows.Forms.TextBox

Add properties:

#Region "Properties"

    Private Sec As Integer = 2
    <Description("Set time in seconds"), _
       Category("CustomPorpeties")> _
        Public Property ColorTime() As Integer
        Get
            Return Sec
        End Get
        Set(ByVal Value As Integer)
            Sec = Value
        End Set
    End Property

    Private MyCol As Color = Color.LightGoldenrodYellow
    <Description("Set BackColor for text_changed event"), _
    Category("CustomPorpeties")> _
        Public Property MyBackColor() As Color
        Get
            Return MyCol
        End Get
        Set(ByVal Value As Color)
            MyCol = Value
        End Set
    End Property


    Private PosCol As Color = Color.Green
    <Description("Set ForeColor for positive value"), _
    Category("CustomPorpeties")> _
    Public Property PositiveColor() As Color
        Get
            Return PosCol
        End Get
        Set(ByVal Value As Color)
            PosCol = Value
        End Set
    End Property

    Private NegCol As Color = Color.Red
    <Description("Set ForeColor for negative value"), _
    Category("CustomPorpeties")> _
    Public Property NegativeColor() As Color
        Get
            Return NegCol
        End Get
        Set(ByVal Value As Color)
            NegCol = Value
        End Set
    End Property

#End Region

Add Timer:

Friend WithEvents Timer1 As System.Windows.Forms.Timer

Add Code:

Dim OldValue As Integer = 0

    Private Sub MyTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
         Try
            If Text - OldValue > 0 Then
                ForeColor = PosCol
            Else
                ForeColor = NegCol
            End If
            OldValue = Text
            BackColor = MyBackColor
            Timer1.Interval = ColorTime * 1000
            Timer1.Start()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        BackColor = Color.White
        Timer1.Stop()
    End Sub

Build the project;

Since now, you can use your custom made "MyTextBox" in your projects:
Create a new project,
Right Click on the ToolBox. Select Customize Toolbox from the menu that appears. Choose the .Net Framework Tab.
Now, click on the Browse button and navigate your way to the folder where you saved your User Control. Open the bin Subfolder of the project. You will see the file MyTextBox.dll in that folder.

Click on the Open button and the control will be added to the list of .Net Framework Components in front of you. It should have automatically have been checked, but if it hasn't then you can do this now.

Close the Customize Toolbox menu by clicking OK. If you review the ToolBox again you will see that the MyTextBox has been added. You are now able to use this control in this (and any future) projects that you create or use.
0
 

Author Comment

by:Dynotoe
ID: 16626758
Wow that's fantastic.

Thank you very much Vadim.

Cheers!

Dynotoe
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16626784
Thank you, Dynotoe. Good luck.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question