Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change forecolor in program event 2

Status
Not open for further replies.

Phil Thoms

Programmer
Oct 31, 2005
245
GB
Hello,
I have a field in a column which can have either 'STD' or 'EXP' so I wish STD to appear in black and EXP to show in red.
What is the easiest or correct way to achieve this

Many thanks
 
Hi philthoms,

I assume Your values 'STD' and 'EXP' have the type STRING.


1) You need a function:

Code:
FUNCTION ColorGet( fieldVal )
    LOCAL colorVal

    DO CASE
        CASE fieldVal == "STD"
            colorVal = RGB( 0, 0, 0 )          && color = black

        CASE fieldVal == "EXP"
            colorVal = RGB( 255, 0, 0 )        && color = red

        OTHERWISE
            colorVal = RGB( 128, 128, 128 )    && color = grey

    ENDCASE

    RETURN colorVal
ENDFUNC


2) Put this code into the Grid Init Event:

Code:
THIS.Columns( colNo ).DynamicForeColor = "ColorGet( tableName.fieldName )"


colNo: number of Your grid column
tableName: name of Your table
fieldName: name of the field in Your table

Best regards, Stefan
 
You mention a "column". Can we assume from that that the field in question is displayed in a grid?

If so, then Stefan has put you on the right track. But I would argue that you don't need a separate function for this. You can simply set the colour as follows:

Do this in the Init of the grid:

Code:
THIS.Column1.DynamicForeColor = "IIF(UPPER(ALLTRIM(MyTable.MyField)) = 'STD', 0, 255)"

This assumes that the column in question is the first one in the grid (Column1). If you prefer to make the entire row red or black, you can do this:

Code:
THIS.SetAll("DynamicForeColor", "IIF(UPPER(ALLTRIM(MyTable.MyField)) = 'STD', 0, 255)", "Column")

That said, using a separate function, as per Stefan's post, has the advantage that you can easily expand it, to cater for more colours or more values.

For more information about the "dynamic" properties, see "Conditional formatting in a Visual FoxPro grid".

Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike and Stefan, field in question is in a grid and is a char string. If I was using Stefans suggestion where would I place the function?

Many thanks.




 
If I was using Stefans suggestion where would I place the function?

You could place it in its own PRG file, in which case the file would need the same name as the function. The PRG would have to be in the VFP search path (as defined by SET PATH).

Alternatively, you could maintain a general-purpose procedure file: one which holds all the little functions that you want to be available to all parts of the application. In that case, you would need to point a SET PROCEDURE command to it at an early point in your application - probably as one of the first things you do in your main program.

Which of the above options you choose is a largely a matter or personal preferences. They each have their pros and cons.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
It's a pitty you need to ask. First you're free to put this where it fits best for you.

You have one fixed requirement, the DynamicForeColor of the grid column has to be able to make the call and that call has to find the function.
What code is easiest to call from anywhere?
1. Code publicly available
2. Code in the current context/scope

Public is quite bad, it gives no cherence to the code, things are just anywhere and while it's easy to be able to call something from anywhere, it has only a real meaning in the context of the forcolor of that column displaing that field.
So think about 2. what is the context? It is 2a) the grid 2b) the textbox in the grid 2c) the class maintaining this table and/or this concrete table field and all the behaviour around it including it's representation and coloring.

You look at it and say "I can't add a method to the grid, the textbox and I have no class / centralized code about the table". Well, then you have planned badly. You should have an own grid class extensible for such situations or a textbox class you can fit to the wanted representation and a business logic.

There is no single place this HAS to be put and the only way to work, so the question can only be about a practical solution. The answer to that is where YOU know you'll find it in the future and are able to maintain and extend or change it. One nice thing would be, if the code in the DynamicForecolor would state where to find the function, wouldn't it? An easy way for that would be, if the code in that property would say THIS.ColorGet(fieldname), then THIS would point out where to find the ColorGet method, namely at THIS place, at the grid or its column, you'll easily find out by trying, and if you need to start VFP again, create a new pjx, new form and grid to be able to experiment.

And if you now don't know how to add a method to a grid, I have to say you don't only need to go back to the drawing board but learn to draw, and that's not a thing to teach in one thread.

Bye, Olaf.
 
Hello Olaf,
Thanks for your comments which I partially agree with. I use VFP 6 and have done for many years but we are mainly using older versions of FoxPro (2.6) both Windows and DOS. I am always trying to migrate to VFP hence my occasional threads and I am trying to bring others into the 21st century. The older programs work exceedingly well so we don't fix what isn't broken. As I have said I am making headway with VFP but while I use VFP often I don't program on a daily basis unfortunately. I am busy writing books on the subject of road/rail transport, so we are getting there and all the programs written thus far work very well and look good also. I/we do not have any manuals apart from FP 2.6 and I keep an index of all the useful tips I receive for VFP work, so I try hard not to ask the same question twice. Your help and assistance is very much appreciated so I ask you kindly to be patient.

Many thanks.
 
Hi everyone,

@Olaf: we all started off small in the past :)

@philthoms: if you have any questions, no matter what questions, please feel free to post them here in this forum :)

Best regards, Stefan
 
It's worth pointing out that the answer to the question about where to place the function (in its own file or in a file opened by SET PROCEDURE) applies equally in 2.6 as it does in VFP. (But of course the DynamicForeColor property is specific to VFP.)

On that point, there is one other option you could consider: Instead of a function, make it a method of the form. In that case, you would have to preceed the name with [tt]THISFORM.[/tt] when calling it ([tt] ... .DynamicForeColor = "THISFORM.ColorGet( ... etc. [/tt] ). This is a bit more cumbersome, but has the advantage of keeping the logic in the same place as the grid that uses it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks Mike and Stefan,
I have formatted a few functions in FP 2.6 so I asked the function placement question in case VFP treated them differently but thanks for the alternative suggestions these are very interesting. The conditional formatting website is also most useful.
I decided to use Stefan's second suggestion (dynamic/immediate IF) which I applied to a couple of fields and all is working well and looking good.
Although FP 2.6 is working well for us, it would be helpful to find second hand manuals for VFP 6 to help myself and colleagues to push forward.
I have tried ebay and amazon but to no avail.
Thanks once again.
 
You don't find manuals, because the helpfiles are a library of books and a full reference on their own, AFAIR the chm was a separate install in VS 98/6.
Just googled a bit, yes: The MSDN LIbrary CD that came with Visual Studio 6 included VFP help on top of the MSDN Library of all VS languages help and more.
If you only installed VS you'd have no F1 help, though.

The help of VFP9 has not grown very much in comparison to VFP6 and so it's size should be a good measure of what you have at hand in VS98, too. It has several thousand pages and a reference of each class/object, method/property, command and function plus a few best practices and walkhroughs. Hentzenwerke Books are covering more topics and extend that reference in things like Office automation and framework designing.

I would suggest upgrading to version 9, if you want to stay VFP, though, as even with SP5 VFP6 is a very buggy version of VFP. I liked VFP7 and 9 much better, our company skipped 8, though. Anyway all newer versions are better than 6 without looking at new features, simply in the aspect of stability.

Another obvious way is to go Web/Cloud and don't stay Fox at all. In my eys PHP/MySQL is a combination better fitting VFP as migration path than dotNET+MSSQL. You have to adapt to client/server programming in both cases and PHP has the disadvantage of not offering Windows Forms as frontend part, but you have the same strong coupling of programming language and database plus OOP mixed with procedural/scripting programming. I don't think of it as bad in any case, I'm not the purist you may think I am, but OOP helps organising things for easier and centralised maintainance while procedural style programming helps providing some very general purpose functions without fitting them into complicated OOP models, plugins or extension methods. DotNet has a steep learning curve and no dirty ways, the only advantage is you can stay Winforms. It differs from VFP forms anyway. But @get/@say works different than VFP controls, too, there's a mismatch anyway and that is a strong reason to go to a place being actively developed as either Java, DotNet or the Web world are.

Bye, Olaf.
 
Hi philthoms,

You can download the Visual Foxpro 6.0 help files from here:

Foxhelp.zip

Just download the Foxhelp.zip file and extract it into an own folder. You will get 2 files:

- Foxhelp.chm
- Foxhelp.chi

Then double click on Foxhelp.chm to open and read.

Or make it much more comfortable: if You have Visual Foxpro 6.0 installed, goto => Extras => Options and insert the path for this help file.

Options_cqv2xt.jpg


- Note: I have a german Visual Foxpro installed

Now You can use the Visual Foxpro help menus or You can press the Visual Foxpro F1 button :)

Best regards, Stefan
 
Nice of you. I think Phil has a helpfile, you would notice if F1 doesn't show help and then fix that quite early. Unless that's really a reason you think VFP comes without any help, Phil.
Last time I bought a programming language with printed help manuals and reference was C for Atari ST (don't know the exact make anymore) and later Borland Pascal for PC in 1990. As I bought Borland C++ Builder for PC around mid 90ies that already only had a box with CD Rom and no dead wood.

Bye, Olaf.
 
Many, many thanks for all your help and advice. I will try to update to a later VFP asap, in the meantime I may ask for more help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top