Monday, December 19, 2005

The CodeBank has moved!

I'm now using the DataBrowser at http://www.atakala.com/Browser/Item.aspx?user_id=amos

I've found that the blog mode is no good for what I'm doing here, I needed a site that supported "encyclopedia mode" instead.

Come check it out!

Thursday, October 06, 2005

Color the Background of a C# TabControl

To set the color of a tabControl to something other than 'Control', set the DrawMode property on the tabControl.
DrawMode = OwnerDrawFixed.

Then, handle the DrawItem event; you will need to draw the tab text yourself, as well as optionally setting the background color.

private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
string tabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
Rectangle centerRect = new Rectangle(e.Bounds.X+3,e.Bounds.Y+3,e.Bounds.Width-6,e.Bounds.Height-3);
e.Graphics.FillRectangle(Brushes.WhiteSmoke, centerRect); // colors the tab caption
e.Graphics.DrawString("Filter", new Font("Arial", 9, FontStyle.Regular),System.Drawing.Brushes.Black, centerRect, sf);
} // load


Since this only colors the actual tab captions, you need to color each tab page the same color as here. Do this by setting the BackColor of each tab page to the color you used here.

Note that we only colored an area excluding a 3px border. This is because the tabPages have, by default, a 3px border around them, so our tab captions need to match that.



Sunday, September 18, 2005

Add Prompt on C# Webpage

When a user clicks a button, you probably want to confirm that they meant to click it. Especially if the button's Text is "Delete Database". Here's about the simplest check you could do:

myDeleteButton.Attributes.Add("onClick", "return confirm('Are you sure you wish to delete the database?');");

The user will see "Ok" and "Cancel". If the user doesn't choose 'Ok', the operation is canceled.

Friday, September 16, 2005

Alternating Color on Crosstab Rows

In Crystal Reports 8.5 it "isn't possible" to alternate row color on a CrossTab. However, as usual with Crystal, there are plenty of workarounds.
For me, most of my crosstabs always have the same number of rows, with variable numbers of fixed width columns.
Workaround?
Create a Picture which is a light grey box, and move it behind every other row. (Send to back.) Then, in the code that calls the report (C#/.NET), you can dynamically set the width of the box to match the number of columns you have:

// set row shading width.
foreach(CrystalDecisions.CrystalReports.Engine.ReportObject reportObj in report.reportDocument1.ReportDefinition.ReportObjects)
{
if(reportObj.Kind == CrystalDecisions.Shared.ReportObjectKind.PictureObject)
{
CrystalDecisions.CrystalReports.Engine.PictureObject box = (CrystalDecisions.CrystalReports.Engine.PictureObject)reportObj;
box.Width = 1000+cols*500; //(label width + x for each column)
}
} // FORMAT lines


Note that you may be tempted to use a BoxObject, but setting the Width of a BoxObject dynamically is not allowed. Also, make SURE you are decreasing the width of the picture box (put it on the report at maximum width). Stretching the box doesn't seem to work.

So theoretically, you could enhance this by dynamically creating (or moving) a series of pictures to handle the case of multiple rows as WELL as multiple columns. Good luck with that! I'll post it if I ever need to come up with the algorithm.

Thursday, September 15, 2005

Changing Mailing Label Size in Crystal Reports

It's easy to create mailing labels using the Mailing Label Wizard in Crystal Reports. (8.5+, at least).
But when you realize that you needed 1 inches instead of 2, how do you fix it without starting all over?
It's simple enough, but certainly not intuitive. Open the Page Setup and enter the margins for your label sheet. Then, go to the Section Expert for the Detail section and choose the Layout Tab. Here you can change x, y, x-margin, y-margin, and the Across/Down order. Verify that the number of rows and columns are correct. You may need to delete extra whitespace around the detail section to get everything to add up.
And that's it! : )

Friday, August 26, 2005

Error detected in export .dll - Crystal Reports

When exporting Crystal Reports to a .pdf format, the great error message "Error detected in export .dll" started popping up. It took a while to track down, but the cause was this:
An image field was on the report, but the data was null. Worked fine in the viewer, but just not on the export.
Solution: set "Suppress" to isnull(image_field_here).

Tuesday, August 23, 2005

Testing SQL Code without cache - SQL Server

To find out how long a SQL query takes to run, you can't have the results already in the SQL Server cache.
To clear the SQL cache, enter:
DBCC DROPCLEANBUFFERS

If you are testing a stored procedure, clear the stored procedue cache by entering:
DBCC FREEPROCCACHE