Implementing Print using C#
The first assignment I got from my lead in my previous company was to create Print option to the accounting package we were working. The application was developed in VB 6.0. And we were used batch files and command prompt for printing accounting related documents from the application. Few days back one of colleague asked how we can print from C#, he was developing an accounting package for his uncle. I said something like we need to use PrintDocument class for that, but I couldn’t give him a sample code. Today I got some free time in Office so I thought about implementing printing from C#. Here is a simple implementation, which displays a Print Dialog, and based on the settings, it will print the contents of the textbox.
I tried to print the document based on the ForeColor of the TextBox, but it was throwing some error, I couldn’t resolve itIts fixed.
Source code is pretty self-explanatory. Happy Printing.
private int startIndex = 0;
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDocument printDocument = new PrintDocument();
//Print Options dialog.
using (PrintDialog printDialog = new PrintDialog())
{
printDialog.ShowHelp = false;
printDialog.ShowNetwork = false;
printDialog.Document = printDocument;
printDialog.AllowPrintToFile = true;
//As its a simple text file, disabling the advanced options.
printDialog.AllowCurrentPage = false;
printDialog.AllowSelection = false;
printDialog.AllowSomePages = false;
//Display XP Style Print Dialog
printDialog.UseEXDialog = true;
if (printDialog.ShowDialog(this) == DialogResult.OK)
{
//Assigning the Printer settings to the Document.
printDocument.PrinterSettings = printDialog.PrinterSettings;
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
//Number of copies
for (int i = 0; i < printDialog.PrinterSettings.Copies; i++)
{
startIndex = 0;
//Print method call.
printDocument.Print();
}
}
}
}
And here is the Print Page event.
protected void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
//Setting the color as Black.
Brush brush = Brushes.Black;
//Setting color dynamically
//Brush brush = new SolidBrush(this.txtEditor.ForeColor);
int lineCounter = 0;
float lineTop = 0;
//Looping throught each line in the textbox.
for (int lineIndex = startIndex; lineIndex < this.txtEditor.Lines.Length; lineIndex++)
{
string line = this.txtEditor.Lines[lineIndex];
lineCounter++;
//Writing the line to the document.
lineTop = e.MarginBounds.Top + lineCounter * this.txtEditor.Font.Size;
e.Graphics.DrawString(line,
this.txtEditor.Font,
brush,
e.MarginBounds.Left,
lineTop);
//Checking for more pages.
if (lineTop > e.MarginBounds.Bottom)
{
startIndex = lineIndex;
e.HasMorePages = true;
return;
}
}
}
How to use Stored Procedures in Entity Framework
In the current project we are using Entity Framework for database operations. Entity Framework comes with Visual Studio SP1, which helps you to map tables / views / procedures as entities in C# / VB Code. You can find more details about EF from here : http://msdn.microsoft.com/en-us/library/bb399572.aspx. In this post I am explaining how to use Stored Procedures in Entity Framework.
- Add the Stored Procedure to the Entity Model Designer using Update Model From Database Option.
- If you are added successfully, you can get the procedure in Model Browser.
- Right click on the Procedure name and select Create Function Import.
- It will popups a Windows with Stored Procedure Name, Function Import Name and Return Type. If the procedure returns nothing, you can choose none. If the procedure is returns single value, like UserId, Number Of Rows etc, then you can choose scalar option, where you need to specify the return type. And if the procedure is returns Table or Number of Rows, you need to choose the last option Entities, which will allow to select entities created in the Model as the Output. Sometimes we need to create a View in the DB and need to import it in the Model, so that we can use the View as the return type entity. Select the appropriate return type and click Ok. You can use this in code. In this code I am using a View to return the selected users.
using (SampleEntities context = new SampleEntities())
{
/*
* Thanks to Barry Soetoro.
* I was not calling the GetAllUsers function.
List<Users> Users = null;
Users = (from user in context.Users
select user).ToList();
this.dataGridView1.DataSource = Users;
*/
//Updated Version.
IEnumerable<UsersView> userview = context.GetAllUsers();
this.dataGridView1.DataSource = userview;
}
This will display list of Users in the DataGridView. Happy Coding
Microsoft Office 2010 – Community Launch – Kerala
Last weekend K-MUG had conducted the Microsoft Office 2010 community launch at Technopark Trivandrum.The launch was a great success, very well received we got the crowd more than expected. Microsoft Office 2010 is a complete makeover from the earlier successful versions. We got mixed audience and mainly developers, students and technology enthusiasts as our audience. Vijay Raj, Microsoft MVP, had demonstrated some of the cool features of the Office 2010 as part of the launch. Thanks to Abhishek Kant and Mera windows team for supporting the community launch.
Vijay made it excellent with the following demonstrations.
- Take a screenshot right inside the word or power Point, instead of using a print screen and then editing it in paint.
- Image editing capabilities to remove the background of an image
- Artistic features to design an image – Without using a third party software.
- Pinning a document to the recent list, or jump list for easy access.
- How to add Visual text effects in Word to highlight a section of text or letters.
- The recovering capabilities of an office document, when closed prematurely.
- How to create data trends chart for each row / column using Sparkline in Excel 2010.
- Improved Animation and Narration features in PowerPoint 2010.
- How to add a Video from File System as well as from Online to a PowerPoint presentation, edit and format them in ease.
- Using sections within a presentation.
- Save presentation as a video
- Broadcasting a PowerPoint presentation to external users through internet.
- Free online versions of Word, Excel, PowerPoint, and OneNote, which work in popular web browsers.
At the end, We had given the trail version of Office 2010 DVD to all attendees.
You can download trail version of Office 2010 from here. And explore the online free versions from here.




