[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4197
  • Last Modified:

Accessing Colors Other Than VB's Eight (i.e. vbBlack, vbWhite....)

I'm currently developing reports using VB and a report tool (Crystal Reports) which is an Add-In to VB.  Crystal Reports offers background colors such as Navy and Silver.

I decided it would be best to change the background color to white (from
silver) during the print job to save toner.  However, getting the colors
back to their original state has been a trick for one reason.  That  is, not
all colors offered by Crystal Reports are available in VB while programming.

I need the report header background to be Navy and the header text to be
white.  All other bacground should be silver.  I have the code that turns
the colors to white and black within the PrintClicked command, and I'm almost
done with the code which turns it all back within the RefreshClicked
command:


For the Print Job:

Private Sub CRViewer1_PrintButtonClicked(UseDefault As Boolean)
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object

Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections

     For Each crSection In crSections
         crSection.BackColor = vbWhite
         Report.Text8.TextColor = vbBlack

     Next crSection
End Sub


Turning The Colors Back:

Private Sub CRViewer1_RefreshButtonClicked(UseDefault As Boolean)

Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Font.Color

Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections

     For Each crSection In crSections
         crSection.BackColor = vbsilver
         Report.Section1.BackColor = vbNavy
         Report.Text8.TextColor = vbWhite

     Next crSection

End Sub

The problem here is that there is no such thing as 'vbSilver' or 'vbNavy'.
How can I get those colors?  I was thinking I could create 'TextColor'
variables within the PrintClicked command and set them to the Navy and Silver objects of the header background and report background.  This would be done previous to turning the backgrounds white.  If that sounds good, can you tell me how the syntax is done and whether I can call these variables within the RefreshButtonClicked command when resetting the colors?

I'll take any suggestions for a different approach but I think this might be simplest.  Can you edit the code above if my idea sounds good, thanks.

Here are the only VB colors available:

vbBlack
vbRed
vbGreen
vbYellow
vbBlue
vbMagenta
vbCyan
vbWhite

0
John500
Asked:
John500
  • 10
  • 10
1 Solution
 
Erick37Commented:
Make your own color:

Const VBSILVER = &H808080

Colors are in the form BBGGRR (hex)
0
 
jkpcsCommented:
Erick37 has listed the correct concept.  Now, how to know the hex code for the color you wish?  

Here is what I do.  I use MS Access to get the color required.  Since there are more colors listed by default there.  In Access, create a form, and add a plain box (rectangle).  In the BackColor properties, find the color you are looking for.  Using the Build button, you can choose from as many colors as your video driver can display (ie 16,000+).  Once you have selected the proper color, you are given the Long Integer format for that color.  You can use that number in your VB code or you can convert it to hex and use that.

Good luck
0
 
Erick37Commented:
Alternatively, just save the default colors before you change them.  That way you do not need to know the actual value.

Ex.

Private PrevTextColor As OLE_COLOR

'Before you change the color:
PrevTextColor =  Report.Text8.TextColor

'...

'Restore the color:
 Report.Text8.TextColor = PrevTextColor
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
John500Author Commented:
Erick37,

I tried what you said but I'm doing something wrong.  What needs to be corrected below so that 'SilverTextColor' & 'NavyTextColor' can be used in the CRViewer1_RefreshButtonClicked?  

I tried declaring the variables as 'Private' but I was getting the following error for the word 'Private':

Compile error:  Invalid attribute in Sub or Function


Private Sub CRViewer1_PrintButtonClicked(UseDefault As Boolean)
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Dim SilverTextColor As OLE_COLOR
Dim NavyTextColor As OLE_COLOR

Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
   
    For Each crSection In crSections
        SilverTextColor = crSection.BackColor
        NavyTextColor = Report.Section1.BackColor
        crSection.BackColor = vbWhite
        Report.Text8.TextColor = vbBlack
                     
    Next crSection
End Sub

Private Sub CRViewer1_RefreshButtonClicked(UseDefault As Boolean)

Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object


Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections

     For Each crSection In crSections
         crSection.BackColor = SilverTextColor
         Report.Section1.BackColor = NavyTextColor
         Report.Text8.TextColor = vbWhite

     Next crSection

End Sub
0
 
Erick37Commented:
You need to declare SilverTextColor and NavyTextColor outside of the functions so that both functions can use the same variable.

eg

'~~~~Form Code~~~~

Option Explicit

Private SilverTextColor As OLE_COLOR
Private NavyTextColor As OLE_COLOR

Private Sub CRViewer1_PrintButtonClicked(UseDefault As Boolean)
    '... your sub
End Sub

Private Sub CRViewer1_RefreshButtonClicked(UseDefault As Boolean)
    '...the other sub
End Sub
0
 
John500Author Commented:
Erick37,

I did as you said and moved the two lines outside the functions and it compiled.  However, the background colors of silver and navy aren't getting changed.  Note that the text letters are getting changed back to white.

Do you have any suggestions on how I could debug this or why the colors aren't getting changed?  Thanks.
 
0
 
John500Author Commented:
Erick37,

I forgot to add in the last comment, is it possible these variables should be declared as report objects like:

Private SilverBackColor As crSection.BackColor
Private NavyBackColor As crSection.BackColor

I don't know if this makes sense, just a guess.





0
 
Erick37Commented:
Look at the values that you have assigned in each sub or add a breakpoint in the For loop and examine the variables.

For Each crSection In crSections
    SilverTextColor = crSection.BackColor
    NavyTextColor = Report.Section1.BackColor

    'Confirm that a color is being assigned
    Debug.Print Hex(SilverTextColor); Hex(NavyTextColor)
   
    crSection.BackColor = vbWhite
    Report.Text8.TextColor = vbBlack
Next crSection

'...

For Each crSection In crSections

    Debug.Print Hex(SilverTextColor); Hex(NavyTextColor)

    crSection.BackColor = SilverTextColor
    Report.Section1.BackColor = NavyTextColor
    Report.Text8.TextColor = vbWhite
Next crSection
0
 
Erick37Commented:
One bug:  You change the Backcolor in the Loop, so on the second loop, SilverTextColor is set to vbWhite.

Assign SilverTextColor to one of the Sections color BEFORE you enter the loop.

For Each crSection In crSections
    'On second entry, backcolor has been set to white
    'So silver is white
    SilverTextColor = crSection.BackColor
    NavyTextColor = Report.Section1.BackColor
    'Change to white
    crSection.BackColor = vbWhite
    Report.Text8.TextColor = vbBlack
Next crSection
0
 
John500Author Commented:
Erick37,

Still having some trouble.  I'm new to VB so I'm still trying to understand the different 'types' and so on...  I grabed much of this code from an example program. You get the idea...

I understand exactly when you say "On second entry, backcolor has been set to white, so silver is white"  However, I'm not sure where to make the assignment when you say "Assign SilverTextColor to one of the Sections color BEFORE you enter the loop"

I assigned the variables as:

Private Sub CRViewer1_PrintButtonClicked(UseDefault As Boolean)
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object

Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
SilverBackColor = crSection.BackColor
NavyBackColor = Report.Section1.BackColor
   
    For Each crSection In crSections
                crSection.BackColor = vbWhite
        Report.Text8.TextColor = vbBlack
                     
    Next crSection
End Sub

But I got the following error:
Run-time error '91':  Object variable or With block variable not set

Can you edit the code the way you think it should work?  Thanks


0
 
Erick37Commented:
My mistake.
Your first code should work, since each time through the loop, crSection refers to a different section.

What values did you get from the Debug.print statements?
0
 
John500Author Commented:
Erick37,

I put the lines back where they were and put in the debug code that you mentioned.  Tell me where the output appears for the debug statements.  I am very familar with debugging in VC++ but I can't tell how to view the variables using VB while the program is running.

I used the 'Run To Cursor' method and placed the cursor one line past the Debug statement.  I also let the entire program run, but there was still nothing like a MessageBox to reveal the values.  What am I misssing?  Thanks.


0
 
Erick37Commented:
Debug.Print statements appear in the Immediate window.
On the menu go to View->Immediate Window
I like to keep it docked on the bottom.

0
 
John500Author Commented:
Erick37,

Got any ideas why the 'Debug.Print statements don't return any info in the 'Immediate Window'?  I ran the program without any breaks but nothing shows up.

I tried 'Running to cursor' and then typing 'Print crSection.BackColor' in the Immediate window, but it's as if the window is locked up and doesn't respond

I also tried setting a couple 'Watches'.  I'm able to set the watches but when the program runs, the watches disappear from the window??

0
 
John500Author Commented:
Erick37,

I see now that the Watch window displays info after the program terminates.  Here is what I got:

Watch :   : SilverBackColor : <Out of context> : Empty : Form1.CRViewer1_PrintButtonClicked

Watch :   : crSection.BackColor : <Out of context> : Empty : Form1.CRViewer1_PrintButtonClicked

0
 
Erick37Commented:
Start a new test project with a Command Button on the form.

Code:

Private Sub Command1_Click()
Static i As Long
i = i + 1
Debug.Print "Click "; i
End Sub

Also, in your original code, add a breakpoint to the line where you assign the colors.  By placing the mouse over a variable name in the code, the value will be displayed.  Press F8 to step through the code and watch the variables.

What varsion of VB are you using?
0
 
John500Author Commented:
Erick,

I'm using the MS Visual Studio, Enterprise Edition which has VB 6.0.

I added the Command button as you said, and it works.  I figured the next step was to add modify the command as follows:

Private Sub Command1_Click()

Static i As Long
i = i + 1
Debug.Print "Click "; i
Debug.Print Hex(SilverBackColor); Hex(NavyBackColor)

End Sub

When I clicked this button I would get:

Click  1
00
Click  2
00
Click  3
00

Does this mean the values for SilverBackColor & NavyBackColor are 0 and 0

After adding the breakpoint I still wasn't able to view the value of variables.  I know what you are talking about when you say "By placing the mouse over a variable name in the code, the value will be displayed".  I do this in VC++ daily.  At this point, I don't know why VB isn't working this way.  I'm wondering if there is some option I need to select so that the values display?
0
 
Erick37Commented:
Tools->Options->Auto Data Tips
to turn on the data tips.

If debug.Print says 0, then it is 0.

I don't quite know why it is zero.
0
 
John500Author Commented:
Eric,

Maybe some progress here.  I moved the code you gave me into the PrintButtonClicked event rather than putting it in separate 'Command' button.  Therefore, I'm guessing that with each iteration of FOR the loop I got the values of something.  I'd like to say the values pertain to the Silver and Navy variables but it looks like there is only one value per loop, yes/no?

Click  1
800000800000
Click  2
C0C0C0FFFFFF
Click  3
C0C0C0FFFFFF
Click  4
C0C0C0FFFFFF
Click  5
C0C0C0FFFFFF
Click  6
C0C0C0FFFFFF
Click  7
FFFFFFFFFFFFFF

Here is the code I used again:

For Each crSection In crSections
   
        SilverBackColor = crSection.BackColor
        NavyBackColor = Report.Section1.BackColor
       
        Static i As Long
        i = i + 1
        Debug.Print "Click "; i
        Debug.Print Hex(SilverBackColor); Hex(NavyBackColor)
   
        crSection.BackColor = vbWhite
        Report.Text8.TextColor = vbBlack
                     
    Next crSection

0
 
John500Author Commented:
Erick,

I added an additional 'Print' rather than using just one.  Here was the ouput:


Click  1
800000
800000
Click  2
C0C0C0
FFFFFF
Click  3
C0C0C0
FFFFFF
Click  4
C0C0C0
FFFFFF
Click  5
C0C0C0
FFFFFF
Click  6
C0C0C0
FFFFFF
Click  7
FFFFFFFF
FFFFFF

How do you interpret this?  It seems both the Navy and Silver have the same value of 800000.  I decided to use these values in the RefreshButtonClicked event and the color did change from an all white background to all dark green?!  Here is the code:

For Each crSection In crSections
     
         Debug.Print Hex(SilverBackColor); Hex(NavyBackColor)
     
         crSection.BackColor = 800000
         Report.Section1.BackColor = 800000
         Report.Text8.TextColor = vbWhite

     Next crSection

What would you suggest I do to obtain the correct colors?
0
 
Erick37Commented:
Colors are in the form BBGGRR (hex).  So the color &H800000 is:
80 Blue
00 Green
00 Red

Again, these are HEX values (range from 00 to FF)

The HEX value 800000 is actually a dark blue (Navy) color, and should be entered in code as &H800000.  The reason it is green is because VB interpreted 800000 as a base 10 number, which is actually &H0C3500 in HEX.

&HFFFFFF is white
&HC0C0C0 is grey (silver)
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 10
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now