Sunday, August 23, 2009

"PrintForm" Component, available in .NET 3.5 SP1

An alternative use the 'PrintForm' component, which is available now in .NET 3.5 SP1 or available as a separate download . Here is Some sample code:

Place a ToolstripButton on to the Form :

private void toolStripButton2_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild == null)
return;
printForm1.Form = this.ActiveMdiChild;
printForm1.PrintAction = System.Drawing.Printing.PrintAction.PrintToPreview;
printForm1.Print();
}

How to see Print Preview & Print a WindowsForm



using System.Drawing.Printing; //For "PrintDocument" Class

public partial class Form3 : Form
{
PrintDocument PrintDoc1 = new PrintDocument();
PrintPreviewDialog PPDlg1 = new PrintPreviewDialog();

public Form3()
{
InitializeComponent();

PPDlg1.Document = PrintDoc1;
PrintDoc1.OriginAtMargins =true; //To set or Get the Position of a Graphic Object
PrintDoc1.PrintPage += PDoc_PrintPage;
}

private void btn_PrintPreview_Click(object sender, EventArgs e)
{// When PrintPreview Button Clicks
PPDlg1.ShowDialog();
}

private void PDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bmp = new Bitmap(this.Width, this.Height);
//this.DrawToBitmap(bmp, this.ClientRectangle);
this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); //Takes the Snap of the Exact WindowForm size as Bitmap image
e.Graphics.DrawImage(bmp, 0, 0);
}

private void btn_PrintPage_Click(object sender, EventArgs e)
{//When Print Button Clicks, Image will be show & Ready to Print
PrintDoc1.Print();
}

}

Wednesday, August 19, 2009

Deployment of a Windows Forms / Console Application

On the Build menu, choose Rebuild all Projects, to make sure that the client application,

To create a deployment project

1. On the File menu, point to Add, and then click New Project.


2. In the Add New Project dialog box, in the Project Types pane, open the Other Project Types node, click Setup and Deployment Projects, then click Setup Project in the Templates pane. In the Name box, type <project_name>_Setup.


3. Click OK to close the dialog box.

The project is added to Solution Explorer, and the File System Editor opens.


4. Change ProductName property and remove "_Setup" from default ProductName name


· Select the <project_name>_Setup project in Solution Explorer. In the Properties window, select the ProductName property and type <project_name> without _Setup.






Note: · The ProductName property specifies the name that will be displayed for the application in installation folder names and in the Add/Remove Programs dialog box.

5. Change Manufacturer from ‘Default Company Name’ property to xxx . Then the MSI will install your application installed into the c:\Program Files\xxx\<project_name>\ folder


To add the WinForms / Console application to the installer


1. Right-click the \<project_name>_Setup project in Solution Explorer.

· View -> File System

· In the File System Editor, select the Application Folder node.


2. On the Action menu, point to Add, and then click Project Output.


3. In the Add Project Output Group dialog box:

  • choose \<project_name> from the Project drop-down list
  • Select the Primary Output group from the list
  • In the Configuration box, select (Active).
  • Click OK to close the dialog box.

To create Program Files folder & menu


Select the \<project_name>_Setup project in Solution Explorer.


1. Normally Install will, by default, restrict installation to a single user. If needed, in Project Explorer, click Setup Project name, view properties list, and set “InstallAllUsers” to true.

1a. In the File System Editor, right-click User’s Program Menu.


2. Add -> Folder and Rename Folder to <project_name>


3. In the File System Editor, in the Application Folder, select the Primary output from <project_name> node.


4. Right Click -> Create Shortcut to Primary Output.


5. Rename the Shortcut to <project_name>.


6. Select the Shortcut and drag it to your User’s Program Menu folder in the left pane.


To add an icon for Program Files folder & menu

  1. In the File System Editor, click User’s Program Menu. then in the right panel, right-click the shortcut (not the folder).

  2. In the Properties window, select the Icon property and choose (Browse...) from the drop-down list.
  3. The Icon dialog box will be displayed.

  4. In the DropDownListBox, choose "User's Program Menu"

  5. Click the Add File Button, then browse for the Icon file.

Rebuild the _setup project, then you should be ready !

You can repeat the steps above to add Desktop Icons, in the same way that you created Program Files folder & menu
On the Build menu, choose Rebuild all Projects.
If necessary, use Windows Explorer, to paste any other "application reference data” e.g. XML data, into the Application Folder

..

Tuesday, August 11, 2009

Load Text File Rtb

How to load text file rtb in visualBasic6


RichTextBox1.LoadFile ("C:\test.txt")

Load Crystal Report from File


This code will show how to load a crystal report .rpt file that is saved on a local drive instead of embedded. The advantage to this is the program does not need to be re-compiled each time a report is modifed. Also, the .rpt can be upload from the application and stored in a database and then written to file. Do not embed the .rpt file when using this method.


using System;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace Report
{
public partial class Report : Document
{
public void ReportLoad()
{
ReportDocument reportDocument = new ReportDocument();
string filePath = "C:\Projects\Application\Report\CrystalReport.rpt";
reportDocument.Load(filePath);
crystalReportViewer.ReportSource = reportDocument;
}
}

}
..

Export Data from Listview to Excel


private void Export2Excel()
{
try
{
//lvPDF is nothing but the listview control name
string[] st = new string[lvPDF.Columns.Count];
DirectoryInfo di = new DirectoryInfo(@"c:\PDFExtraction\");
if (di.Exists == false)
di.Create();
StreamWriter sw = new StreamWriter(@"c:\PDFExtraction\" + txtBookName.Text.Trim() + ".xls", false);
sw.AutoFlush = true;
for (int col = 0; col < lvPDF.Columns.Count; col++)
{
sw.Write("\t" + lvPDF.Columns[col].Text.ToString());
}

int rowIndex = 1;
int row = 0;
string st1 = "";
for (row = 0; row < lvPDF.Items.Count; row++)
{
if (rowIndex <= lvPDF.Items.Count)
rowIndex++;
st1 = "\n";
for (int col = 0; col < lvPDF.Columns.Count; col++)
{
st1 = st1 + "\t" + "'" + lvPDF.Items[row].SubItems[col].Text.ToString();
}
sw.WriteLine(st1);
}
sw.Close();
FileInfo fil = new FileInfo(@"c:\PDFExtraction\" + txtBookName.Text.Trim() + ".xls");
if (fil.Exists == true)
MessageBox.Show("Process Completed", "Export to Excel", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
}
}
...

Load Data from Excel File to DataGridView



To read data from excel file and load that data into DataGridView, this function will take file name as parameter and reads that excel file

Controls Required: DataGridView


using System.Data.OleDb;
using System.IO;

public void setExcelFileAsDataSourceToDataGridView(string FileName)
{
OleDbConnection ocon;
OleDbDataAdapter oda;
DataSet ds;

//Check Whether file is xls file or not
if (Path.GetExtension(FileName) == ".xls")
{
try
{
//Create a OLEDB connection for Excel file
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source=" + FileName + ";" + "Extended Properties=Excel 8.0;";
oCon = new OleDbConnection(conStr);
oda = new OleDbDataAdapter("select * from [Sheet1$]",oCon);
ds = new DataSet();

//Fill the Data Set
oda.Fill(ds);
//Set DataSource of DataGridView
dataGridView1.DataSource = ds.Tables[0];
ds.Dispose();
oda.Dispose();
oCon.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("Please select Excel File");
}
}

...

Followers