Tuesday, August 19, 2014

How to display PDF documents in Windows applications(C# and VB.Net)



To display PDF in Windows Application:
  1. Start a new Windows Project
  2. Right Click on the tool box and Click on “Choose Items”

  1. You will get the following “Choose Toolbox Items” window. Go to “COM Components” Tab  and check “Adobe PDF Reader” and click on “OK”

  1. You will now find the adobe control in the tool boxes.

  1. In the Design View Drag and drop Adobe PDF reader Control and a Button Control

On the button click event add the following code:

VB.Net code

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim dlg As OpenFileDialog
        dlg = New OpenFileDialog()
        dlg.Filter = "pdf files (*.pdf) |*.pdf;"
        dlg.ShowDialog()
        If (Not String.IsNullOrEmpty(dlg.FileName)) Then
            AxAcroPDF1.LoadFile(dlg.FileName)
        End If
     End Sub


C# .Net Code

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "pdf files (*.pdf) |*.pdf;";
            dlg.ShowDialog();
            if(dlg.FileName != null)
            {
                axAcroPDF1.LoadFile(dlg.FileName);
            }

        }

Output:





Monday, August 18, 2014

LINQ - An intro to LINQ

LINQ stands for Language Integrated Query. LINQ is a microsoft technology which provides language level support to query any type of datastore (SQL server, XML documents, objects in memory like list of customers, list of orders, arrays, entities, dataset etc)

Suppose we are developing a .Net application which is fetching data from different datasources like SQL XML document, Memory objects etc,then we need to understand the technology and the syntax that is specific for the data source.(Suppose to retrieve data from SQL database we need to understand ADO.net and Transact SQL. To retrieve data from XML document, we need to understand how XPath and XSLT works. Similarly with Memory objects we need to understand how array and generics works.)


The different Types of LINQ:
LINQ to Objects
LINQ to XML
LINQ to DataSet
LINQ to SQL
LINQ to Entities

Friday, August 15, 2014

Working with SQLite in .Net

SQLite is a database engine which is used widely in the software development.
SQLite is also used in Microsoft .net project as it is standalone ,does not require any configuration and is serverless.

To use SQL Lite as your database . you should download SQLite dll from the SQlite website
System.data.SQLite.dll
Once you have downloaded,Add the SQlite dll in the bin directory of your application folder
In Visual studio Right click your project and add reference, Browse and add System.data.SQLite.dll .
The namespace is used is - System.Data.Sqlite.


Thursday, August 14, 2014

How to center a WPF application on center of screen



To center the WPF screen at center add the code at the XAML add attribute WindowStartupLocation and set its property to CenterScreen

WindowStartupLocation="CenterScreen"


<Window x:Class="DemoApplicationMainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="800" Width="850" WindowStartupLocation="CenterScreen"  MaxHeight="9000">

Code to create a single instance of WPF application



Code to create a Single instance of Windows Application in WPF
To create a single instance first we need to check if there is any instance is already running and kills the current process to stop creating multiple instances. This can be achieved by the below code.  

using System;
using System.Diagnostics;
using System.IO;
namespace SingleInstance
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            var exists = Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count();

            if(exists > 1)
            {
                MessageBox.Show("An Instance of the application is already running");               
                System.Diagnostics.Process.GetCurrentProcess().Kill();
                return;
            }

            InitializeComponent();
        }
    }
}


Tuesday, August 12, 2014

VB.Net Code to get the installed printers in the network

VB.Net code for Installing printers in the network.


Add namespace: System.Drawing.Printing. 
Add Combo box Control: cbPrinterList
During the Page load: Copy the below code
  For Each Printer As String In PrinterSettings.InstalledPrinters
            cbPrinterList.Items.Add(Printer.ToString())
        Next

Summary:
Imports System.Drawing.Printing
Imports System
Public Class PrinterList
Private Sub PrinterList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each Printer As String In PrinterSettings.InstalledPrinters
            cbPrinterList.Items.Add(Printer.ToString())
        Next
        cbPrinterList.Text = My.Settings.cbPrinter

    End Sub