basic controls and file handling

Upload: megha-gupta

Post on 08-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Basic Controls And File Handling

    1/56

    Slide 1 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    A user interface is the means by which a user interacts with

    an application.

    User interaction in a windows-based application is

    accomplished through Windows Forms and controls.

    The purpose of GUI Applications Development Using .NETFramework is to provide the student with a hands-on

    environment in which they will learn the core skills of

    Microsoft Windows Forms application development.

    Rationale

  • 8/7/2019 Basic Controls And File Handling

    2/56

  • 8/7/2019 Basic Controls And File Handling

    3/56

    Slide 3 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    The two ways of interacting with an application are:

    By using Character User Interface (CUI)

    By using Graphical User Interface (GUI)

    Introducing the GUI Environment

    Enter your name: John

    Enter a book name: Famous Five

    Sorry, The book is not available.

    Do you want to check for another book (y/n):

    In CUI, the user interacts with a system by keying in

    commands.The following figure shows the sample output of a CUI

    application.

  • 8/7/2019 Basic Controls And File Handling

    4/56

    Slide 4 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Advantages of GUI applications are:

    They are easy to learn and use.

    Users need not remember the lines of syntax.

    Most of the commands can be executed using the mouse.

    Introducing the GUI Environment (Contd.)

  • 8/7/2019 Basic Controls And File Handling

    5/56

  • 8/7/2019 Basic Controls And File Handling

    6/56

    Slide 6 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Just a minute

    A ____________ is a control that consists of other controls.

    Answer:

    Container

  • 8/7/2019 Basic Controls And File Handling

    7/56

    Slide 7 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Windows provides the following types of dialog boxes:

    Modal

    System modal

    Modeless

    Identifying Controls of GUI Interface (Contd.)

  • 8/7/2019 Basic Controls And File Handling

    8/56

    Slide 8 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Just a minute

    Which dialog box allows users to switch to another area of

    the application?

    Answer:

    Modeless Dialog Box

  • 8/7/2019 Basic Controls And File Handling

    9/56

    Slide 9 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Just a minute

    A __________ acts as a container for a group of related

    controls.

    Answer:

    Dialog box

  • 8/7/2019 Basic Controls And File Handling

    10/56

    Slide 10 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Identifying the Features of the Windows Environment

    Two of the most important features of the Windows

    environment that enable applications to interact effectively

    with the user are:

    Event-Driven Programming:

    Based on users input, an event gets generated and thecorresponding code is executed.

    Dynamic Linking:

    Windows application is linked to the existing library functions at

    run time.

  • 8/7/2019 Basic Controls And File Handling

    11/56

    Slide 11 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    The .NET offers a complete suite for developing and

    deploying applications.

    This suite consists of the:

    .NET Products

    .NET Services

    .NET Framework

    The .NET Framework

    Microsoft has introduced Visual

    Studio .NET, which is a tool fordeveloping .NET applications by

    using programming languages

    such as Visual Basic, Visual C#,

    and Visual C++.

    These products aim at allowing

    developers to create numerous

    applications.

  • 8/7/2019 Basic Controls And File Handling

    12/56

    Slide 12 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    The .NET offers a complete suite for developing and

    deploying applications.

    This suite consists of the:

    .NET Products

    .NET Services

    .NET Framework

    The .NET Framework (Contd.)

    Microsoft has come up with its ownset of Web services, known as My

    Services. These services are

    based on the Microsoft Passport

    Authentication service, which is

    used in Web applications such

    as Hotmail.

    This service allows users to access

    data by linking calendars,

    phonebooks, address books, and

    personal references to the passport

    authentication service.

  • 8/7/2019 Basic Controls And File Handling

    13/56

    Slide 13 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    The .NET offers a complete suite for developing and

    deploying applications.

    This suite consists of the:

    .NET Products

    .NET Services

    .NET Framework

    The .NET Framework (Contd.)

    It is the foundation on which you

    design, develop, and deploy

    applications.

    It is a consistent and simplified

    programming model that helps you

    to easily uild ro ust applications.

  • 8/7/2019 Basic Controls And File Handling

    14/56

    Slide 14 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    The following figure shows the different components of the

    .NET Framework.

    Components of the .NET Framework

    CLR is the

    environment where

    all programs using.NET technologies

    are executed.

    The CLR allows the

    execution of code

    across different

    platforms bytranslating code into

    Intermediate

    Language (IL).

    IL is a low-level

    language that the

    CLR understands.

  • 8/7/2019 Basic Controls And File Handling

    15/56

    Slide 15 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Some of the advantages offered by the .NET Framework

    are:

    Consistent programming model

    Multi-platform applications

    Multi-language integrationAutomatic resource management

    Ease of deployment

    Components of the .NET Framework (Contd.)

  • 8/7/2019 Basic Controls And File Handling

    16/56

    Slide 16 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    The Visual Studio .NET IDE provides you with a common

    interface for developing various kinds of applications.

    The IDE also provides you with a centralized location for

    designing the user interface for an application, writing code,

    and compiling and debugging the application.Let us see the various components of .NET IDE.

    Using Visual Studio .NET IDE

  • 8/7/2019 Basic Controls And File Handling

    17/56

    Slide 17 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Just a minute

    Which template creates a .NET Compact Framework 2.0

    forms application for Pocket PC 2003 and later?

    1. Windows Control Library

    2. Class Library

    3. Device Application4. Web Control Library

    Answer:

    3. Device Application

  • 8/7/2019 Basic Controls And File Handling

    18/56

    Slide 18 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Just a minute

    Which window displays messages for the status of various

    features provided in the Visual Studio .NET IDE?

    Answer:

    The Output Window

  • 8/7/2019 Basic Controls And File Handling

    19/56

    Slide 19 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Just a minute

    Which window allows to view the resources of the servers,

    such as services, processes, and the event log.

    Answer:

    The Server Explorer window

  • 8/7/2019 Basic Controls And File Handling

    20/56

    Slide 20 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Navigational Features in Visual Studio .Net IDE

    In addition to various windows, the Visual Studio .NET IDE

    also provides three navigational features.

    These are:

    Docking

    Tabbed NavigationAuto Hide

    Let us see these navigational features in the Visual Studio

    .NET IDE.

  • 8/7/2019 Basic Controls And File Handling

    21/56

    Slide 21 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Creating Projects and Solutions

    In Visual Studio .NET, an application can be made up of

    several files and folders.

    To organize these files and folders efficiently, Visual Studio

    .NET has provided two types of containers:

    ProjectsSolutions

    Lets see how to create projects and solutions.

  • 8/7/2019 Basic Controls And File Handling

    22/56

    Slide 22 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    Compiling and Executing a Project

    To compile and execute the application, you need to

    perform the following steps:

    1. Select BuildBuild Solution or press F6 to compile the

    application.

    2. Select DebugStart Debugging or press F5 to execute theapplication.

  • 8/7/2019 Basic Controls And File Handling

    23/56

    Slide 23 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    In this session, you learned that:

    Windows provides a GUI, which enables a user to interact with

    the application by using graphical components.

    Windows provides three types of dialog boxes:

    Modal Dialog BoxSystem Modal Dialog Box

    Modeless Dialog Box

    The .NET Framework consists of three main components:

    Common Language Runtime (CLR)

    The .NET Framework Class Library

    User and Program Interfaces

    Summary

  • 8/7/2019 Basic Controls And File Handling

    24/56

    Slide 24 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    The Visual Studio .NET IDE provides you with a common

    interface for developing various kinds of projects for the .NET

    Framework.

    In Visual Studio .NET, an application can be made up of one or

    more items, such as files and folders.

    To organize these items efficiently, Visual Studio .NET has

    provided two types of containers, projects and solutions.

    In addition to various windows, the Visual Studio .NET IDE

    also provides three navigational features. These are:

    Docking

    Tabbed NavigationAuto Hide

    Summary (Contd.)

  • 8/7/2019 Basic Controls And File Handling

    25/56

    Slide 25 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET Framework

    MDI (Multiple Document Interface) isa Microsoft Windows programminginterface for creating an application

    that enables users to work withmultiple documents at the same time.

    Each document is in a separate spacewith its own controls for scrolling. The

    user can see and work with differentdocuments with the help of this MDI-Form.

    MDI Form Creation:

  • 8/7/2019 Basic Controls And File Handling

    26/56

    Slide 26 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkExample ForMDI Form:

  • 8/7/2019 Basic Controls And File Handling

    27/56

    Slide 27 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

  • 8/7/2019 Basic Controls And File Handling

    28/56

    Slide 28 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkCode For Activating MDI Menus:

    using System;

    using System.ComponentModel;

    using System.Drawing;

    namespace MDI

    {

    public partial class Form1 : Form{

    public Form1()

    {

    InitializeComponent();

    }

    private void menuItem2_Click(object sender, EventArgs e)

    {

    Form2 f = new Form2();

    f.Show();

    }

  • 8/7/2019 Basic Controls And File Handling

    29/56

    Slide 29 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

    private void menuItem3_Click(object sender, EventArgs e)

    {

    Form3 f1 = new Form3();

    f1.Show();

    }

    private void menuItem4_Click(object sender, EventArgs e){

    Form4 f = new Form4();

    f.Show();

    }

    private void menuItem7_Click(object sender, EventArgs e)

    {

    Close();

    }

    }

    }

  • 8/7/2019 Basic Controls And File Handling

    30/56

    Slide 30 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkFile Handling

    In this section of File Handling, we clearly

    describes about the topics about

    How to open a file?How to Create a File?

    How to Save a File?

    How to Append a File?and so on

  • 8/7/2019 Basic Controls And File Handling

    31/56

    Slide 31 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkOpenFileDialog:

    The OpenFileDialog object interacts with theComputer's API (Application Programming Interface)to present available files to the user and retrieves the

    user's file selection back to the program. This object is part of the System.Windows.Forms

    library so no additional using reference will beneeded.

    This dialog can be customized to show the file type,beginning directory, and the title to be displayed onthe dialog itself.

  • 8/7/2019 Basic Controls And File Handling

    32/56

    Slide 32 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

  • 8/7/2019 Basic Controls And File Handling

    33/56

    Slide 33 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

    *.Structure:

    openFileDialog1.ShowDialog();

    *.Code For Activation:if (openFileDialog1.ShowDialog() == DialogResult.OK){

    StreamReader dr = new _StreamReader(openFileDialog1.FileName);

    richTextBox1.Text = dr.ReadToEnd();

    dr.Close();

    }

  • 8/7/2019 Basic Controls And File Handling

    34/56

    Slide 34 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkStreamReader:

    StreamReaderis used to read data from thefiles, and display them into the correspondingcontrols.

    You want to read a text file using StreamReaderfrom the System.IO namespace in the C#language and base class libraries.

    By using the 'using' statement, which is idealfor this purpose, you can both perform actualfile IO and dispose of the system resources.

  • 8/7/2019 Basic Controls And File Handling

    35/56

    Slide 35 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkCreating A File:

    First the user has to create a file by means of

    type every required thing within the

    appropriate control, and save everything with

    the help of this SaveFileDialog control.

    SavefileDialog Prompts the user to select a

    location for saving a file.

    Structure:

    saveFileDialog1.ShowDialog();

  • 8/7/2019 Basic Controls And File Handling

    36/56

    Slide 36 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

  • 8/7/2019 Basic Controls And File Handling

    37/56

  • 8/7/2019 Basic Controls And File Handling

    38/56

    Slide 38 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkFileStream Class

    The FileStream class is derived from Stream

    class. This class can be used for reading from

    and writing to files such as bytes, characters,

    strings, and other data-type values to a file.

    Structure:

    FileStream fileStream = new_ FileStream(@"c:\file.txt",

    FileMode.Operation);

  • 8/7/2019 Basic Controls And File Handling

    39/56

    Slide 39 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkStreamWriter:

    Implements a TextWriter for writing

    characters to a stream in a particular encoding.

    N

    amespace:System.IO

  • 8/7/2019 Basic Controls And File Handling

    40/56

    Slide 40 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkAppend File:

    For appending content to a text file we use the

    Append Method of the File Class.

    The Append methods takes in the path of thefile to which the contents to be appended as an

    argument.

    It opens the file in the specified path and

    returns a StreamWriterobject which can be

    used to append contents to the file.

  • 8/7/2019 Basic Controls And File Handling

    41/56

    Slide 41 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

    *.Code For Activation:saveFileDialog1.Filter = "text files|*.txt";

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)

    {

    FileStream fs = new FileStream(saveFileDialog1.FileName,_FileMode.Append);

    StreamWriter wr = new StreamWriter(fs);

    wr.WriteLine(richTextBox1.Text);

    wr.Close();

    fs.Close();MessageBox.Show("saved");

    }

  • 8/7/2019 Basic Controls And File Handling

    42/56

    Slide 42 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkFile Information Retrieval:

    FileInfo class is used for viewing File

    Information.

    FileInfo class has nonstatic members Nameand Extension and Length are found in

    FileInfo class.

    These properties are not there in File class.

  • 8/7/2019 Basic Controls And File Handling

    43/56

    Slide 43 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

  • 8/7/2019 Basic Controls And File Handling

    44/56

    Slide 44 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

    *.Code For Activation:

    if (openFileDialog1.ShowDialog() == DialogResult.OK)

    {

    FileInfo f = new FileInfo(openFileDialog1.FileName);

    MessageBox.Show("name" + "---" + f.Name + "\n" +

    _"extension"+ "---" + f.Extension + "\n" + "length" +

    _"----" +f.Length + "\n" + "creation time" + "---" +

    _f.CreationTime);

    }

  • 8/7/2019 Basic Controls And File Handling

    45/56

    Slide 45 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkFile:

    Use ofFile class:(static members).

    It must be a valid path.

    The folder must be a valid folder. This File Class allow you to verify the

    corresponding file is available in the given

    directory or not.

  • 8/7/2019 Basic Controls And File Handling

    46/56

    Slide 46 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinuous

  • 8/7/2019 Basic Controls And File Handling

    47/56

    Slide 47 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinuous

    *.Code For Activation:string s="";

    if (openFileDialog1.ShowDialog() == DialogResult.OK)

    {

    s = openFileDialog1.FileName;

    }

    if (File.Exists(s) == false)

    {MessageBox.Show(file not there");

    FileStream ft= File.Create(s);

    MessageBox.Show("file created");

    ft.Close();

    }

    else

    {MessageBox.Show("file is there");

    FileStream fs = File.Open(s, FileMode.Open);

    StreamReader dr = new StreamReader(fs);

    richTextBox1.Text = dr.ReadToEnd();

    fs.Close();

    }

  • 8/7/2019 Basic Controls And File Handling

    48/56

    Slide 48 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkDirectory:

    The Directory Class allow you to Create, drop,

    and check for directories existence in the

    respective path

    Base Class:

    System.IO

    .Directory

  • 8/7/2019 Basic Controls And File Handling

    49/56

    Slide 49 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

  • 8/7/2019 Basic Controls And File Handling

    50/56

    Slide 50 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

    *.Code For Activation:string z = "d:\\DEOGIRI ENGG";

    if (Directory.Exists(z) == false)

    {

    Directory.CreateDirectory(z);File.Create(z + "\\" + "demo.txt");

    MessageBox.Show("created directory on"+"---"+Directory.GetCreationTime(z));

    }

    else

    {Directory.Delete(z);

    MessageBox.Show("deleted directory");

    }

  • 8/7/2019 Basic Controls And File Handling

    51/56

    Slide 51 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkDirectoryInfo:

    This DirectoryInfo

    Class is used to

    show all the

    subdirectoriesbelow a given

    directory

  • 8/7/2019 Basic Controls And File Handling

    52/56

    Slide 52 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

    *.Code For Activation:try{

    DirectoryInfo d = new DirectoryInfo("d:\\");

    DirectoryInfo[] z = d.GetDirectories();

    foreach (DirectoryInfo y in z)

    {

    treeView1.Nodes.Add(y.Name);DirectoryInfo[] p = y.GetDirectories();

    if (p.Count() > 0)

    {

    for (int i = 0; i < treeView1.Nodes.Count; i++)

    treeView1.Nodes[i].Nodes.Add(y.Name);

    }}

    }

    catch (Exception ex){

    MessageBox.Show(ex.Message);

    }

  • 8/7/2019 Basic Controls And File Handling

    53/56

    Slide 53 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkBinaryReader:

    This BinaryReaderclass is used for reading the data

    from a binary file.

    BinaryReader is using for readprimitive types as

    binary values in a specific encoding stream.

    BinaryReaderObject works with Stream Objects that

    provide access to the underlying bytes.

    For creating a BinaryReaderO

    bject , you have to firstcreate a FileStreamObject and then pass

    BinaryReader to the constructor method .

  • 8/7/2019 Basic Controls And File Handling

    54/56

    Slide 54 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkContinues

    *.Code For Activation:FileStream fs = new FileStream("d:\\hello\\abd.txt", FileMode.Open);

    BinaryReader br = new BinaryReader(fs);

    br.BaseStream.Seek(1, SeekO

    rigin.Begin);while(fs.Position < fs.Length)

    {

    MessageBox.Show(fs.Position + "---" + fs.Length);

    listBox2.Items.Add(br.ReadInt32() + "---" + br.ReadString());

    fs.Position += 1;

    }

    fs.Close();

  • 8/7/2019 Basic Controls And File Handling

    55/56

    Slide 55 of 24Session 1Ver. 1.0

    GUI Applications Development Using .NET FrameworkBinaryWriter:

    BinaryWriter Class is used for write primitivetypes as binary values in a specific encodingstream.

    C# BinaryWriterObject works with StreamObjects that provide access to the underlyingbytes.

    For creating a BinaryWriterObject , you haveto first create a FileStream Object and then

    pass BinaryWriter to the constructor method .

  • 8/7/2019 Basic Controls And File Handling

    56/56

    GUI Applications Development Using .NET FrameworkContinues

    *.Code For Activation:try {

    FileStream fs = new FileStream("d:\\hello\\abd.txt",FileMode.Append);

    BinaryWriter br = new BinaryWriter(fs);

    br.BaseStream.Seek(1, SeekOrigin.End);

    int a = Convert.ToInt32(textBox1.Text);string b = textBox2.Text;

    br.Write(a.ToString());

    br.Write(b);

    MessageBox.Show("saved data");

    textBox1.Text = "";

    textBox2.Text = "";

    br.Close();

    fs.Close();

    }

    catch (FileNotFoundException) {

    MessageBox.Show("File is not there");

    }