Change Color of UD TextBox Based On Its Value

I have a UD textbox on the Quote Entry Form that I’d like to set the color of, based on the value of that UD textbox field.
For example, if the value is “On File”, set the color to green. If the value is “Expired” then set the color to red.

What would be the best way to go about this?

I will throw some code in here tomorrow unless someone beats me to it.

If you only have those two choices, you may consider using a shape instead. They have an “enabled” and disabled" state that are pre-programmed to do the green/red with different messages for each. Then you can set the enabled/disabled in an EpiViewNotification. See below example.

Thanks Aaron!!!

Dan:
Well, unfortunately I gave only two, but there are two others as well.

Yeah then a shape isn’t the right tool for this job.

Give this ago!

In class Script section.

private EpiDateTimeEditor dteOrderDate;

In InitCustC

this.OrderHed_Column.ColumnChanging += new DataColumnChangeEventHandler(this.OrderHed_BeforeFieldChange);

this.dteOrderDate = (EpiDateTimeEditor)csm.GetNativeControlReference("320012d6-a708-4d91-a330-57b28abb1b72");

In Destroy section

this.OrderHed_Column.ColumnChanging -= new DataColumnChangeEventHandler(this.OrderHed_BeforeFieldChange);
	private void OrderHed_BeforeFieldChange(object sender, DataColumnChangeEventArgs args)
	{
	    switch (args.Column.ColumnName)
	    {
	        case "PONum":
	            if (string.IsNullOrEmpty((string)args.ProposedValue))
	            {
					dteOrderDate.UseAppStyling = false;
	                dteOrderDate.BackColor = System.Drawing.Color.Red;
	            }
	            else
	            {
					dteOrderDate.UseAppStyling = false;
	                dteOrderDate.BackColor = System.Drawing.Color.Green;
	            }
	            break;
	        // add more cases here if necessary
	    }
	}

Let me know if it doesn’t work…

Thanks
Aaron.

1 Like

Thank you Aaron for the code. I modified it for my application. It compiles successfully, but nothing is updated on the Quote Entry screen. I added a messagebox right after the private void QuoteHed_BeforeFieldChange to see if it is being triggered, but alas, it is not.

I’ve tried other events such as AfterRowChange, BeforeRowChange, and AfterFieldChange, but to no avail, same result. This is my modified code:

public class Script
{

    private EpiTextBox txtVerStmntStatus;

	public void InitializeCustomCode()
	{
        this.QuoteHed_Column.ColumnChanging += new DataColumnChangeEventHandler(this.QuoteHed_BeforeFieldChange);
        this.txtVerStmntStatus = (EpiTextBox)csm.GetNativeControlReference("b58ae739-d838-41e8-b906-7ba0db9b4643");
	}

	public void DestroyCustomCode()
	{
        this.QuoteHed_Column.ColumnChanging -= new DataColumnChangeEventHandler(this.QuoteHed_BeforeFieldChange);
	}

	private void QuoteHed_BeforeFieldChange(object sender, DataColumnChangeEventArgs args)
	{
        MessageBox.Show("after row change QuoteHed triggered");
	    switch (args.Column.ColumnName)
	    {

	        case "VerStmntStatus_c":
	            if (string.IsNullOrEmpty((string)args.ProposedValue))
	            {
					txtVerStmntStatus.UseAppStyling = false;
	                txtVerStmntStatus.BackColor = System.Drawing.Color.Red;
	            }
	            else
	            {
					txtVerStmntStatus.UseAppStyling = false;
	                txtVerStmntStatus.BackColor = System.Drawing.Color.Yellow;
	            }
	            break;
	        // add more cases here if necessary
	    }
	}
}

Got an update…
Seems the BeforeFieldChange event is triggered when creating a new quote. The messagebox appears after I type in a customer id and hit tab. However, it is still not changing the color.

In the customization settings at the top there is a field called UseAppStyling. Turn this to false for any field you need to manually set the color (or any styling) for. I see it is set in the code, but sometimes I have to manually change it to get colors to work.

Yes, thank you Nate for that reminder. Unfortunately, that is one of the first things I did when I added the custom UD field to the form.

Plop a couple more message boxes inside the if statement to make sure the expected path is followed. This will help confirm the code is being processed at all.

He already confirmed that it only fires when a new quote is generated.

@jharrington
Modify this code and put your color change on the textbox text changed event.

:+1: Will do!

Sorry I thought you wanted it after new record.
I can throw in some more code if needed to give it a kick up the arse!

Well, yes actually, I do want the color of the textbox to change after clicking on the new Quote button, entering a customer ID (or searching for it), and hitting tab. At that point, I have a Method Directive (Erp.BO.Quote.GetCustomerInfo / Post-Processing) that populates the {QuoteHed.VerStmntStatus_c} UD field based on two UD fields in the customer record.

Based on the value that gets populated in {QuoteHed.VerStmntStatus_c}, which could be four values, I’d like the {QuoteHed.VerStmntStatus_c} field to be a certain color.

Hope that makes sense!

I will write some code to trigger after the method.

It’s 22:29 here and typing code on a phone is not something I’m mentally prepared for :rofl::clown_face:

No worries! I appreciate your help immensely!

Y’all are overcomplicating it. :slight_smile:

using System.Drawing;

private void SetTextBoxBackColor(EpiTextBox textBox, System.Drawing.Color color)
{
    textBox.UseAppStyling = false;
    textBox.BackColor = color;
}

System.Drawing.Color originalTextBoxBackColor;
bool defaultTextBoxBackColorIsSaved = false;

private void epiTextBoxC1_TextChanged(object sender, System.EventArgs args)
{
    if(defaultTextBoxBackColorIsSaved == false)
    {
        originalTextBoxBackColor = ((EpiTextBox)sender).BackColor;
        defaultTextBoxBackColorIsSaved = true;
    }         

    switch (((EpiTextBox)sender).Text.ToLower())
        {
        case "on file":
                SetTextBoxBackColor((EpiTextBox)sender, Color.Green);
            break;

        case "expired":
                SetTextBoxBackColor((EpiTextBox)sender, Color.Red);
            break;

        case "somethingelse":
                SetTextBoxBackColor((EpiTextBox)sender, Color.Orange);
            break;

        default:
                SetTextBoxBackColor((EpiTextBox)sender, originalTextBoxBackColor);
            break;
    }
}

2 Likes

Sigh GIFs - Get the best gif on GIFER

1 Like