Turbo pascal help - parameter used for textcolor

Hello, just wanting to know if there is a way of passing a string variable to a procedure, and uses that string to change the textcolor.

For example when the string variable is 'black', then textcolor( black ) but textcolor will not accept string obviously.So is there a short way to do this?

Hope I made it clear on what i wish
Thank you in advance
Andrew

Comments

  • : Hello, just wanting to know if there is a way of passing a string variable to a procedure, and uses that string to change the textcolor.
    :
    : For example when the string variable is 'black', then textcolor( black ) but textcolor will not accept string obviously.So is there a short way to do this?
    :
    : Hope I made it clear on what i wish
    : Thank you in advance
    : Andrew
    :
    The simplest way to do this is to create a constant array holding all the names of the colors. If you place the names in the order in which they appear as numbers, you can loop through the ames and check if the name was supplied:
    [code]
    procedure TextColorByName(Color: string);
    const
    ColorNames: array[0..15] of string = ('BLACK', 'BLUE', 'GREEN', {etc});
    var
    i: integer;
    begin
    for i := 0 to 15 do
    if Uppercase(Color) = ColorNames[i] then
    TextColor(i);
    end;
    [/code]
    This code is not speed optimalized, and you might need to define Uppercase(), but it should work. If the color is unknown then nothing will happen.
  • : Hello, just wanting to know if there is a way of passing a string variable to a procedure, and uses that string to change the textcolor.
    :
    : For example when the string variable is 'black', then textcolor( black ) but textcolor will not accept string obviously.So is there a short way to do this?
    :
    : Hope I made it clear on what i wish
    : Thank you in advance
    : Andrew
    :
    The simplest way to do this is to create a constant array holding all the names of the colors. If you place the names in the order in which they appear as numbers, you can loop through the ames and check if the name was supplied:
    [code]
    procedure TextColorByName(Color: string);
    const
    ColorNames: array[0..15] of string = ('BLACK', 'BLUE', 'GREEN', {etc});
    var
    i: integer;
    begin
    for i := 0 to 15 do
    if Uppercase(Color) = ColorNames[i] then
    TextColor(i);
    end;
    [/code]
    This code is not speed optimalized, and you might need to define Uppercase(), but it should work. If the color is unknown then nothing will happen.
  • Hmm didnt think about that.About 2 minutes ago I just found a way but not the shortest one hehe.

    Type
    colorslist = ( clblack , clblue etc... );
    colors = colorslist;

    Procedure gettextcolor( txtclr : colors );
    Begin
    Textcolor( ord(txtclr) );
    End;

    I think that it works fine :)
    and still thanks for your help
  • : Hmm didnt think about that.About 2 minutes ago I just found a way but not the shortest one hehe.
    :
    : Type
    : colorslist = ( clblack , clblue etc... );
    : colors = colorslist;
    :
    : Procedure gettextcolor( txtclr : colors );
    : Begin
    : Textcolor( ord(txtclr) );
    : End;
    :
    : I think that it works fine :)
    : and still thanks for your help
    :
    I thought you wanted to use a string variable to store the color. If you wanted words in your code, then
    [code]
    TextColor(Black)
    [/code]
    is perfectly acceptable, because Pascal has predefined all the names of the known color as constants. Search the help files for black and you should find them.
  • hi,
    My name is kenny, I am from nigeria, please i will like u to give me some tutorials on the pascal programming language and how to be a good programmer.I will like tp hear from u sonnest. thanks.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories