C# Application program UNIT III

Preview:

Citation preview

UNIT III

• BUILDING WINDOWS APPLICATION• ACCESSING DATA IN ADO.NET

Building Windows Application

Program Flow• Fill Directory & Sub Directory• Create an order list of all selected file used by both

copy and delete button• GetCheckedFile and GetParentString used by the list

file• Explanation for each button• Get the full path of the chosen directory –

Target_AfterSelect• Mark each node below the current one with current

value of checked – source_Aftercheck• Recursively set to clear - SetCheck

TREE VIEW CONTROL

SET THE CHECK BOX CONTROL OF LEFT (SOURCE) TREEVIEW TO TRUE AND OTHER AS FALSE

FILE COMPARE CLASSpublic frmfilecopier( { InitializeComponent(); FillDirectoryTree(tvsource, true); FillDirectoryTree(tvtarget, false); }

public class FileComparer : IComparer<FileInfo> { public int Compare(FileInfo file1, FileInfo file2) { if (file1.Length > file2.Length) { return -1; } if (file1.Length < file2.Length) { return 1; } return 0; } public bool Equals(FileInfo x, FileInfo y) { throw new NotImplementedException(); } public int GetHashCode(FileInfo x) { throw new NotImplementedException(); } }

FILL_DIRECTORY TREEprivate void FillDirectoryTree(TreeView tv, bool isSource) { tv.Nodes.Clear(); string[] strDrives = Environment.GetLogicalDrives(); foreach ( string rootDirectoryName in strDrives ) { try { DirectoryInfo dir = new DirectoryInfo( rootDirectoryName ); dir.GetDirectories(); TreeNode ndRoot = new TreeNode( rootDirectoryName ); tv.Nodes.Add( ndRoot ); if(isSource) { GetSubDirectoryNodes( ndRoot,ndRoot.Text,true,1); } else { GetSubDirectoryNodes( ndRoot,ndRoot.Text,false,1); } } catch { } Application.DoEvents(); } }

SUBDIRECTORYprivate void GetSubDirectoryNodes(TreeNode parentNode, string fullName, bool getFileNames, int level) { DirectoryInfo dir = new DirectoryInfo(fullName); DirectoryInfo[] dirSubs = dir.GetDirectories(); foreach (DirectoryInfo dirSub in dirSubs) { if ((dirSub.Attributes & FileAttributes.Hidden) != 0) { continue; } TreeNode subnode = new TreeNode(dirSub.Name); parentNode.Nodes.Add(subnode); if (level < MaxLevel) { GetSubDirectoryNodes(subnode, dirSub.FullName, getFileNames, level + 1);  }  } if (getFileNames) { FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { TreeNode filenode = new TreeNode(file.Name); parentNode.Nodes.Add(filenode);  } } } 

UNIT IV

BUILDING WEB APPLICATION

COPY BUTTON

private void Btncopy_Click(object sender, EventArgs e) { List<FileInfo> filelist = Getfilelist(); foreach (FileInfo file in filelist) { try { lblstatus.Text = "Copying" + txtTargetdir.Text + "\\" + file.Name + "..."; Application.DoEvents(); file.CopyTo(txtTargetdir.Text + "\\" + file.Name, chkoverwrite.Checked); } catch (Exception ex) { MessageBox.Show(ex.Message); } } lblstatus.Text = "Done"; Application.DoEvents(); }

IMPLEMENTIN COPYprivate List<FileInfo> Getfilelist() { List<string> fileNames = new List<string>(); foreach (TreeNode thenode in tvsource.Nodes) { GetCheckedFiles(thenode, fileNames); } List<FileInfo> fileList = new List<FileInfo>(); foreach(string fileName in fileNames) { FileInfo file = new FileInfo(fileName); if(file.Exists) { fileList.Add(file); }

} IComparer<FileInfo> Comparer = (IComparer<FileInfo>) new FileComparer(); fileList.Sort(Comparer); return fileList; }

IMPLEMENTIN COPY 2private void GetCheckedFiles(TreeNode node, List<string> filenames) { if(node.Nodes.Count == 0) { if(node.Checked) { string fullpath = GetParentstring(node); filenames.Add(fullpath); } } else{ foreach (TreeNode n in node.Nodes) { GetCheckedFiles(n,filenames); } } } private string GetParentstring(TreeNode node) { if(node.Parent == null) { return node.Text; } else { return GetParentstring(node.Parent) + node.Text + (node.Nodes.Count == 0 ? "":"\\"); } }

DELETE BUTTONprivate void btndelete_Click(object sender, EventArgs e) { System.Windows.Forms.DialogResult result = MessageBox.Show("Are u quit sure?","Delete

files",MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button2); if(result == System.Windows.Forms.DialogResult.OK) { List<FileInfo> filenames = Getfilelist(); foreach(FileInfo file in filenames) { try{ lblstatus.Text = "Deleting" + file.Name + "..."; Application.DoEvents(); file.Delete(); } catch(Exception ex) { MessageBox.Show(ex.Message); } lblstatus.Text = "Done"; Application.DoEvents(); } } }

CLEAR BUTTONprivate void btnclear_Click(object sender, EventArgs e) {

foreach(TreeNode node in tvsource.Nodes) { SetCheck(node,false); } }

CANCEL BUTTON

private void btncancle_Click(object sender,EventArgs e)

{ Application.Exit();

}

TREEVIEW private void tvtarget_AfterSelect(object sender,System.Windows.Forms.TreeViewEventArgs e) { string theFullpath = GetParentstring(e.Node); if(theFullpath.EndsWith("\\")) { theFullpath = theFullpath.Substring(0,theFullpath.Length-1); } txtTargetdir.Text =theFullpath; } private void tvsource_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e) { if (e.Action != TreeViewAction.Unknown) { SetCheck(e.Node, e.Node.Checked); } } private void SetCheck(TreeNode node, bool check) { foreach (TreeNode n in node.Nodes) { n.Checked = check; if (n.Nodes.Count != 0) { SetCheck(n, check); } } }

Cont..private void tvExpand(object sender, TreeViewCancelEventArgs e) { TreeView tv = (TreeView)sender; bool getFiles = tv == tvsource; TreeNode currentnode = e.Node; string fullName = currentnode.FullPath; currentnode.Nodes.Clear(); GetSubDirectoryNodes(currentnode, fullName, getFiles, 1); }  private void tvsource_AfterSelect(object sender, TreeViewEventArgs e) {  }  private void chkoverwrite_CheckedChanged(object sender, EventArgs e) {  }  } }

ADO.NET APPLICATION

SELECTING THE DATA GRID VIEW

DATA GRID VIEW

SELECTING THE MENU

ADD THE DATA SOURCE

SELECT UR DATABASE

SELECT UR DATABASE

DB IS ADDED

THE CONNECTION IS SAVED

SELECT THE TABEL

THE TABLE IS ADDED

SAVE AND BUILD

THE CODE GENERATED

RUN THE APPLICATION

UNIT IV

BUILDING WEB APPLICATION

OPENING A WEB APPLICATION

SELECT ASP.NET WEB SITES

RENAME FROM DEFAULT TO HELLOWORLD

THE NAME IS CHANGED

NOW TO CHANGE THE CLASS NAME

CONFIRM THE CHANGE

IN THE ASPX CHANGE THE INHERIT ATTRIBUTE

CODE UR PROGRAM

WHILE RUN CONFIRM THIS MESSAGE

THE RESULT