Tuesday, 25 February 2014

How to make TEXTBOX as autocompelete in C# (WINFORMS)


Use this code of programming to make your textbox control autocompelete in C#




namespace AutoCompleteTextBox{
public partial class frmAuto : Form{public string strConnection = ConfigurationManager.AppSettings["ConnString"];AutoCompleteStringCollection namesCollection =new AutoCompleteStringCollection();public frmAuto(){InitializeComponent();}
private void frmAuto_Load(object sender, EventArgs e){SqlDataReader dReader;SqlConnection conn = new SqlConnection();conn.ConnectionString = strConnection;SqlCommand cmd = new SqlCommand();cmd.Connection = conn;cmd.CommandType = CommandType.Text;cmd.CommandText = "Select distinct [Name] from [Names]" + " order by [Name] asc";conn.Open();dReader = cmd.ExecuteReader();if (dReader.HasRows == true){while (dReader.Read())namesCollection.Add(dReader["Name"].ToString());
}else{MessageBox.Show("Data not found");}dReader.Close();
txtName.AutoCompleteMode = AutoCompleteMode.Suggest;txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;txtName.AutoCompleteCustomSource = namesCollection;
}
private void btnOk_Click(object sender, EventArgs e){MessageBox.Show("Hope you like this example");}
}}
}}

Monday, 24 February 2014

Here you'll learn how to create a PDF File using C#

 learn how to create a PDF File using C#




steps are as below:



void CreatePDF()
    {
        try
        {


            string path = Server.MapPath("Attatchment PDF");
        /*Logo Path*/
            string Invoiceid = ;
        string strLogoPath = Server.MapPath("Image") + "\\jayendra.jpg";
        Document doc = new Document(PageSize.LETTER, 25F, 25F, 50F, 25F);
        PdfWriter.GetInstance(doc, new FileStream(path + "/Font.pdf", FileMode.Create));
        doc.Open();
      iTextSharp.text.Rectangle page = doc.PageSize;
        iTextSharp.text.Font Verdana = FontFactory.GetFont("Verdana", 10F, iTextSharp.text.Font.NORMAL,iTextSharp.text.Color.BLACK);
        PdfPTable table = new PdfPTable(2);
        table.TotalWidth = page.Width;
        PdfPCell cell = new PdfPCell();
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(strLogoPath);
        AddImageInCell(cell, image, 200f, 200f, 1);
        cell.Border = 0;
        AddtextCell(table, cell, 0, 0, 0);
        PdfPCell cell2 = new PdfPCell();
        PdfPTable table2 = new PdfPTable(1);
        PdfPCell line_2 = new PdfPCell(new Phrase("Name: Gohil Jayendrasinh F", Verdana));
        AddtextCell(table2, line_2);
        PdfPCell line_3 = new PdfPCell(new Phrase("Email: jayendrasinhgohil@gmail.com", Verdana));
        AddtextCell(table2, line_3);
        PdfPCell line_4 = new PdfPCell(new Phrase("Compny Name: SIGMASOLVE LLC", Verdana));
        AddtextCell(table2, line_4);
        cell2.AddElement(table2);
        cell2.Border = 0;
        AddtextCell(table, cell2, 0, 0, 0);
        doc.Add(table);
        doc.Close();
        }
        catch (Exception)
        {

            throw;
        }
    }


     private static void AddImageInCell(PdfPCell cell, iTextSharp.text.Image image,float fitWidth,float fitHight,int Alignment)
    {
        image.ScaleToFit(fitWidth, fitHight);
        image.Alignment = Alignment;
        cell.AddElement(image);
    }
    private void AddtextCell(PdfPTable table, PdfPCell cell)
    {
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        cell.Border = 0;
        table.AddCell(cell);
    }
    private void AddtextCell(PdfPTable table, PdfPCell cell, float paddingLeft,float paddingRight)
    {
        cell.Colspan = 3;
        cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        cell.PaddingLeft = paddingLeft;
        cell.PaddingRight = paddingRight;
        cell.Border = 0;
        table.AddCell(cell);
    }
    private void AddtextCell(PdfPTable table, PdfPCell cell, float paddingLeft, float paddingRight,int hAlign)
    {
        cell.Colspan = 3;
        cell.HorizontalAlignment = hAlign; //0=Left, 1=Centre, 2=Right
        cell.PaddingLeft = paddingLeft;
        cell.PaddingRight = paddingRight;
        cell.Border = 0;
        table.AddCell(cell);
    }
    private static void AddtextCell(PdfPTable table, PdfPCell cell, int Colspan, int HorizontalAlignment, int Border)
    {
        cell.Colspan = Colspan;
        cell.HorizontalAlignment = HorizontalAlignment; //0=Left, 1=Centre, 2=Right
        cell.Border = Border;
        table.AddCell(cell);
    }


After write these methods and events call these where you want to create PDF file.


Check out my other Blogs Here:click on this link:


http://integratedskillsofdotnet.blogspot.in/
http://computerknowledgehacking.blogspot.in/

Make Read-only Single Node of Tree View in C# (WINFORMS)

Introduction

In this blog, I am going to explain that how to make any single node of tree view Read-only in c#. Steps are below:

Steps:
  1. first time, when you click on Tree View in (windows application) then and do something whatever your requirements, then change the color of tree View node
  2. And whenever you’ll click on that particular node (changed node color in first step), then a message will appear “do you want change node selection”
If user clicks ‘Yes button’ then color of node changes to its original means default color, if ‘no’ then selected node and his root node will be in Read only state. Here below I showing an example with code in c#:

Make two methods like as:
1. Call this method in any click event when you want make the node readonly TRUE.
       void ReadOnlyNodeTrue ()
        {
            bool Isexist = false;
            foreach (TreeNode t in MMCtv.Nodes)
            {
                foreach (DataGridViewRow dgvrr in dgvTreeView.Rows)
                {
                    if (dgvrr.Cells["grBatchNo"].Value.ToString() == t.Text.Replace("BatchNo:- ", ""))
                    {
                        if (t.BackColor != System.Drawing.Color.Gray)
                        {
                            t.BackColor = System.Drawing.Color.Honeydew;
                            Isexist = true;
                        }
                    }
                }
                if (Isexist)
                {
                    Isexist = false;
                    foreach (TreeNode t1 in t.Nodes)
                    {
                        foreach (DataGridViewRow dgvrr in dgvTreeView.Rows)
                        {
                            if (dgvrr.Cells["grCompMaterial"].Value.ToString() == t1.Text.Replace("MatCode:- ", ""))
                            {
                                if (dgvrr.Cells["grBatchNo"].Value.ToString() == t.Text.Replace("BatchNo:- ", ""))
                                {
                                    if (t1.BackColor != System.Drawing.Color.Gray)
                                    {
                                        t1.BackColor = System.Drawing.Color.Honeydew;
                                        Isexist = true;
                                    }
                                }
                            }
                        }
                        if (Isexist)
                        {
                            foreach (TreeNode t2 in t1.Nodes)
                            {
                                if (t2.BackColor != System.Drawing.Color.Gray)
                                {
                                    t2.BackColor = System.Drawing.Color.Honeydew;
                                    Isexist = true;
                                }
                            }
                        }
                        Isexist = false;
                    }
                } 
            }
            TreeNode TN1 = MMCtv.SelectedNode;
            TN1.BackColor = System.Drawing.Color.Honeydew;
        }

2. Call this method in TreeView_NodeMouseClick  when you want make the node readonly FALSE.
 
        void ReadOnlyNodeFalse ()
        {
            bool Isexist = false;
            foreach (TreeNode t in MMCtv.Nodes)
            {
                foreach (DataGridViewRow dgvrr in dgvTreeView.Rows)
                {
                    if (dgvrr.Cells["grBatchNo"].Value.ToString() == t.Text.Replace("BatchNo:- ", ""))
                    {
                        if (t.BackColor != System.Drawing.Color.Gray)
                        {
                            t.BackColor = System.Drawing.Color.Empty;
                            Isexist = true;
                        }
                    }
                }
                if (Isexist)
                {
                    Isexist = false;
                    foreach (TreeNode t1 in t.Nodes)
                    {
                        foreach (DataGridViewRow dgvrr in dgvTreeView.Rows)
                        {
                            if (dgvrr.Cells["grCompMaterial"].Value.ToString() == t1.Text.Replace("MatCode:- ", ""))
                            {
                                if (dgvrr.Cells["grBatchNo"].Value.ToString() == t.Text.Replace("BatchNo:- ", ""))
                                {
                                    if (t1.BackColor != System.Drawing.Color.Gray)
                                    {
                                        t1.BackColor = System.Drawing.Color.Empty;
                                        Isexist = true;
                                    }
                                }
                            }
                        }
                        if (Isexist)
                        {
                            foreach (TreeNode t2 in t1.Nodes)
                            {
                                if (t2.BackColor != System.Drawing.Color.Gray)
                                {
                                    t2.BackColor = System.Drawing.Color.Empty;
                                    Isexist = true;
                                }
                            }
                        }
                        Isexist = false;
                    }
                }
            }
            TreeNode TN1 = MMCtv.SelectedNode;
            TN1.BackColor = System.Drawing.Color.Empty;
        }