Mark Bonafe

Common Sense is Here

<May 2008>
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567


Navigation

bloggers

C#

Subscriptions



Opening IE Using C# Windows

So now you have a nice report written to an HTML file.  How do you open it for the user to view it using IE?  You could try this:

System.Diagnostics.Process.Start("iexplore.exe","MyReport.htm");

But this hasn't always worked for me.  Here is a sure fire way to put it all together.

Add a project reference to the COM library, Microsoft Internet Controls.  Add these two using statements:

using SHDocVw;
using System.Runtime.InteropServices;

Where you need to open the browser, add the following code:

explorer = new InternetExplorer(); 
if (explorer != null

     explorer.Visible = true
     object x = null;
     explorer.Navigate(@"MyReport.htm
", ref x, ref x, ref x, ref x); 
}

Thanks to a good friend of mine, Micheal Beall of Overdrive Technologies, for helping me with this.

posted Wednesday, February 16, 2005 6:34 AM by MarkBonafe

Reporting with HTML. Who Needs Expensive Reporting Tools?

Printing a web page that represents a report has been difficult for me in the past.  I want to print a header and/or a footer that repeats on every printed page.  The screen version certainly isn't paginated, so how can you print a good looking report?  Style (or CSS) to the rescue. 

Put the following Style tag, or something similar, in the header of the page.

<STYLE TYPE=”text/cssMEDIA=”screen, print>
<!--
TABLE {
  table-layout: fixed;
  border: 0;
  cellspacing: 1;
  cellpadding: 1;
  font-family: Arial;
  font-size: 8pt;
  }
TH {
  font-family: Arial;
  color: black;
  background-color: lightgrey;
  text-decoration: underline;
  }
THEAD {
  display: table-header-group;
  }
TFOOT {
  display: table-footer-group;
  }
-->
</STYLE>

Since nearly every report uses an HTML Table, this works very well.  The THEAD and TFOOT styles is what makes the table a report.  If you don't want to setup a style tag, you can enter the style right into the table.

<table style="table-layout:fixed">
    <colgroup>
        <col width="150"/>
        <col width="100"/>
        <col width="150"/>
    </colgroup>
    <thead style="display:table-header-group">
        <tr>
            <td>Header column 1</td>
            <td>Header column 2</td>
            <td>Header column 3</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Body column 1</td>
            <td>Body column 2</td>
            <td>Body column 3</td>
        </tr>
    </tbody>
    <tfoot style="display:table-footer-group">
        <tr>
            <td>Footer column 1</td>
            <td>Footer column 2</td>
            <td>Footer column 3</td>
        </tr>
    </tfoot>
</table>
 
Sounds too easy to be true but it works very nicely.  Of course, adding more style (bolding, underlining, background and foreground colors, etc.) makes this a very nice reporting option.

posted Wednesday, February 16, 2005 5:47 AM by MarkBonafe

Printing a RichTextBox

After examining numerous articles on printing with C#, I was about ready to toss in the towel and just copy & paste to Word (yuck).  Then I found How To Print the Content of a RichTextBox Control By Using Visual C# .NET on Microsoft's support site.

I was sure that printing a RichTextbox should be pretty easy.  All the articles made it very difficult, though.  This article shows the “correct” way to print.  The entire contents are printed; pictures, other graphics, colors, etc.

posted Tuesday, January 11, 2005 10:55 AM by MarkBonafe

You've Got to be Kidding

I swear sometimes it's just not worth comming into work.  Everyday, I get so called updates to the code base.  The changes are made by people in Maintenance (I'm in Development).  And everyday I find something completely rediculous.  Normally, it's something mundane like changing this:

deptName = value;

to this:

this.deptName = value;

Then today I found this fine little nugget.  This perfectly fine property:

public string ApprovedByUserName
{
    get
    {
        if (this.ApprovedBy > 0)
        {
            Users user = new Users();
            user.UserId = this.ApprovedBy;
            user = (Users)StoreFront.Instance.Get(user);
            return user.DisplayName;
        }
        else
        {
            return "";
        }
    }
}

Was rewritten to this:

public string ApprovedByUserName
{
    get
    {
        string displayName = String.Empty;

        bool result = (this.ApprovedBy > 0);

        switch (result)
        {
            case true:
                Users user  = new Users();
       &nb