Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, December 17, 2016

How to make PhoneBook in C#

Today,We will discuss about Phone Book in C#.
User have a text document in which he has written name of person and their phone number.
User wants to find phone number by person name.
I have created a sample text document of phone book for your easiness. 

For making this,I will explain step by step.

1)Open visual studio.File-->New-->Project-->Windows Form Application.

2)Take one TextBox named txt_Name,one button named btn_GetPhoneNum and one label named

3)One text document in which person name and his phone number is written. like this(Ram=9958008909,Shyam=7890908765,Mukesh=9876234567,).
 For this create a text document named phonebook.txt and save it to C drive.

4)User will enter Person Name in TextBox and click on Get Phone Number button.

5)On button click , I have created the object of StreamReader that will open text document of phone number and name.
       StreamReader r = File.OpenText("C:\\phoneBook.txt");
       StreamReader and File class is in System.IO namespace;

       For declaring namespace in C#,we have to use using statement.(Using System.IO)

6) By using ReadLine() method of StreamReader,read all lines of text document.

7)Find index of name that user enter in TextBox.
                int pos = line.IndexOf(name);
8)After that find index of "," and by substring method of string, user will get phone number of person.
                      int i = new1.IndexOf(",");
                    long phone = Convert.ToInt64(new1.Substring(0, i));

9) Code on .Cs page

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace PhoneBook
    public partial class Form1 : Form
        public Form1()

        private void btn_GetPhoneNum_Click(object sender, EventArgs e)

            string name = "";
            name = txt_Name.Text;
            StreamReader r = File.OpenText("C:\\phoneBook.txt");
            string line = r.ReadLine();
            while (line != null)
                int pos = line.IndexOf(name);
                if (pos != -1)
                    string new1 = line.Substring(name.Length + pos + 1, line.Length - (name.Length + pos + 1));
                    int i = new1.IndexOf(",");
                    long phone = Convert.ToInt64(new1.Substring(0, i));

                    lbl_Phonenum.Text = phone.ToString();

                    line = r.ReadLine();
                    MessageBox.Show("Name is not Found in Phone Book");
                    lbl_Phonenum.Text = "";

10)Code on designer.Cs

namespace PhoneBook
    partial class Form1
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
            if (disposing && (components != null))

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
            this.btn_GetPhoneNum = new System.Windows.Forms.Button();
            this.txt_Name = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.lbl_Phonenum = new System.Windows.Forms.Label();
            // btn_GetPhoneNum
            this.btn_GetPhoneNum.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.btn_GetPhoneNum.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_GetPhoneNum.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.btn_GetPhoneNum.Location = new System.Drawing.Point(70, 86);
            this.btn_GetPhoneNum.Name = "btn_GetPhoneNum";
            this.btn_GetPhoneNum.Size = new System.Drawing.Size(109, 23);
            this.btn_GetPhoneNum.TabIndex = 0;
            this.btn_GetPhoneNum.Text = "Get Phone Number";
            this.btn_GetPhoneNum.UseVisualStyleBackColor = false;
            this.btn_GetPhoneNum.Click += new System.EventHandler(this.btn_GetPhoneNum_Click);
            // txt_Name
            this.txt_Name.Location = new System.Drawing.Point(70, 50);
            this.txt_Name.Name = "txt_Name";
            this.txt_Name.Size = new System.Drawing.Size(109, 20);
            this.txt_Name.TabIndex = 1;
            // label1
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
            this.label1.Location = new System.Drawing.Point(12, 57);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(39, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Name";
            // lbl_Phonenum
            this.lbl_Phonenum.AutoSize = true;
            this.lbl_Phonenum.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbl_Phonenum.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
            this.lbl_Phonenum.Location = new System.Drawing.Point(228, 53);
            this.lbl_Phonenum.Name = "lbl_Phonenum";
            this.lbl_Phonenum.Size = new System.Drawing.Size(90, 13);
            this.lbl_Phonenum.TabIndex = 3;
            this.lbl_Phonenum.Text = "Phone Number";
            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(460, 262);
            this.Name = "Form1";
            this.Text = "Form1";



        private System.Windows.Forms.Button btn_GetPhoneNum;
        private System.Windows.Forms.TextBox txt_Name;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label lbl_Phonenum;

 11) Output Wondow


12) Screenshot of Text Docement

If you like this blog,enter your email id on Subscribe for New Post on right side of this blog.

Please read collection of C# Interview question
you also like this blog.
Abstraction and Encapsulation in OOPS

Inheritance in OOPS

1) C# Interview Questions and Answers Part3

2)C# Interview Questions and Answers Part 2

No comments :

Post a Comment