Total members 11893 |It is currently Fri Nov 15, 2024 2:06 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






 Project Name:   Writing a Windows Form Application For .NET Framework Using C#
 Programmer:   Softomatix
 Type:   Application
 Technology:  C#
 IDE:   NONE
 Description:   The sample application demonstrates how to create and layout controls on a simple form and the handling of mouse click events. The application displays a form showing attributes of a file. This form is similar to the properties dialog box of a file (Right click on a file and Click on Properties menu item). Since attributes of a file will be shown, the sample will show how to use File IO operations in .NET framework.

csharp code
// Disclaimer and Copyright Information
// WinFileInfo.cs : Implementation of Windows Form Application
//
// All rights reserved.
//
// Written by Naveen K Kohli ([email protected])
// Version 1.0
//
// Distribute freely, except: don't remove my name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc. to
// [email protected]
///////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Summary:
// This tutuorial is designed to show how to write a Windows Form application
// in Microsoft.NET framework. Although Microsoft has provided the SDK with some
// /preliminary documentation but it is not enough. There are a lot of features
// that surface with trial and error. This tutorial is not a very comprehensive
// Windows Form application but it will show you some basic steps to get you
// started.
// The application shows how to write a dialog based application that can be used
// to check attributes of a file. This dialog box is similar to one that can be
// brought up by right clicking on a file and clicking on Properties option in
// Menu.
///////////////////////////////////////////////////////////////////////////////


#define NAVEEN_DEBUG

using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Diagnostics;

public class WinFileInfo : Form
{

/*****************************************************************************
Constructor : WinFileInfo

Abstract: Constructs an instance of WinFileInfo, calls Window's default
contructor.

Input Parameters:

******************************************************************************/
public WinFileInfo ()
{
InitForm ();
}

// Define some window control variables...
Bitmap m_Bkground = new Bitmap ("bk_bitma.bmp");
TextBox wndFileName = new TextBox ();
Panel wndPanel = new Panel ();
GroupBox wndAttribBox = new GroupBox ();
Label wndLabelFileName = new Label ();
Button wndFindButton = new Button ();
Button wndCloseButton = new Button ();
CheckBox wndFileExistCheck = new CheckBox ();
CheckBox wndArchiveCheck = new CheckBox ();
CheckBox wndReadOnlyCheck = new CheckBox ();
CheckBox wndHiddenCheck = new CheckBox ();
Label wndCreateTimeLabel = new Label ();
Label wndLastAccessLabel = new Label ();
Label wndLastWriteLabel = new Label ();
Label wndCreateTime = new Label ();
Label wndLastAccessTime = new Label ();
Label wndLastWriteTime = new Label ();
Label wndLocationLabel = new Label ();
Label wndLocation = new Label ();

/*****************************************************************************
Function: InitForm

Abstract: Creates UI elements (buttons, edit controls, ...) on the
WinFileInfo Form.

Input Parameters: None

Returns: Void
******************************************************************************/
public void InitForm ()
{
// Since this class has been inherited from Form, so we will make use of
// this (pointer) to call methods on Form.

// Set the text to be shown as the title of application.
this.Text = "File Information Application";

// Set the client area of the form.
// Make sure that we have imported System.Drawing namespce to use Size.
this.ClientSize = new Size(400, 280);

// Indicate that we need Help button to be shown on the form.
this.HelpButton = true;

// We don't need a maximize box for this form.
this.MaximizeBox = false;

// Set the Find button to ACCEPT button for this form.
this.AcceptButton = wndFindButton;

// Set the close button to Cancel operation button.
this.CancelButton = wndCloseButton;

// Set the start position of the form to be center of screen.
this.StartPosition = FormStartPosition.CenterScreen;

// Set the backgound image for the form.
//this.BackgroundImage = m_Bkground;

// Activate the Form.
this.Activated += new EventHandler (this.WinFileInfo_activate);

// Set some properties for static text control..
wndLabelFileName.Anchor = AnchorStyles.Left;
wndLabelFileName.Text = "File Name:";
wndLabelFileName.Location = new Point (0, 5);
wndLabelFileName.Size = new Size (60, 25);
wndLabelFileName.TabStop = false;

// Set some properties for the edit control..
wndFileName.TabIndex = 0;
wndFileName.BackColor = Color.White;
wndFileName.Dock = DockStyle.Right;
wndFileName.Location = new Point (65, 5);
wndFileName.Size = new Size (140, 30);

// Set some properties for Checkboxes now.
wndFileExistCheck.Text = "File Exist";
wndFileExistCheck.Enabled = false;
wndFileExistCheck.Location = new Point (5, 45);

wndArchiveCheck.Text = "Archive";
wndArchiveCheck.Enabled = false;
wndArchiveCheck.Size = new Size (72, 24);
wndArchiveCheck.Location = new Point (5, 15);

wndReadOnlyCheck.Text = "Read Only";
wndReadOnlyCheck.Enabled = false;
wndReadOnlyCheck.Size = new Size (115, 24);
wndReadOnlyCheck.Location = new Point (135, 15);

wndHiddenCheck.Text = "Hidden";
wndHiddenCheck.Enabled = false;
wndHiddenCheck.Size = new Size (72, 24);
wndHiddenCheck.Location = new Point (300, 15);

// Some properties for controls displaying various times.
wndCreateTimeLabel.Text = "Created on:";
wndCreateTimeLabel.Location = new Point (5, 160);
wndCreateTimeLabel.Size = new Size (75, 24);

wndLastAccessLabel.Text = "Last accessed on:";
wndLastAccessLabel.Location = new Point (5, 185);
wndLastAccessLabel.Size = new Size (100, 24);

wndLastWriteLabel.Text = "Last write on:";
wndLastWriteLabel.Location = new Point (5, 210);
wndLastWriteLabel.Size = new Size (75, 24);

wndCreateTime.Location = new Point (110, 160);
wndLastAccessTime.Location = new Point (110, 185);
wndLastWriteTime.Location = new Point (110, 210);

wndCreateTime.Size = new Size (220, 24);
wndLastAccessTime.Size = new Size (220, 24);
wndLastWriteTime.Size = new Size (220, 24);

// Some properties for location controls.
wndLocationLabel.Text = "Location:";
wndLocationLabel.Location = new Point (5, 130);
wndLocationLabel.Size = new Size (72, 24);

wndLocation.Location = new Point (75, 130);
wndLocation.Size = new Size (325, 24);

// Set some properties for Find Button.
wndFindButton.Text = "Find";
wndFindButton.TabIndex = 0;
wndFindButton.Anchor = AnchorStyles.Right;
wndFindButton.Size = new Size (72, 24);
wndFindButton.Location = new Point (110, 250);
wndFindButton.Click += new EventHandler (this.buttonFind_click);

// Set some properties for Close button.
wndCloseButton.Text = "Close";
wndCloseButton.TabIndex = 1;
wndCloseButton.Anchor = AnchorStyles.Right;
wndCloseButton.Size = new Size (72, 24);
wndCloseButton.Location = new Point (190, 250);
wndCloseButton.Click += new EventHandler (this.ButtonClose_Click);

// Set what edges of panel are going to be anchored to container's edges.
// In this case, Each edge of the control anchors to the corresponding
// edge of its container.
//wndPanel.Anchor = AnchorStyles.All;
wndPanel.Location = new Point (5, 5);
wndPanel.Size = new Size (200, 30);
wndPanel.Text = "File Name";
// wndPanel.BackColor = Color.Blue;

// Set the properties for the Attribue GroupBox.
wndAttribBox.Location = new Point (5, 65);
wndAttribBox.Text = "Attributes";
wndAttribBox.Size = new Size (385, 50);

// Add the CheckBox controls to the Attribute GroupBox.
wndAttribBox.Controls.Add (wndArchiveCheck);
wndAttribBox.Controls.Add (wndReadOnlyCheck);
wndAttribBox.Controls.Add (wndHiddenCheck);

// Add controls to the Form.
this.Controls.AddRange(new Control [] {
wndPanel,
wndAttribBox,
wndFileExistCheck,
wndLocationLabel,
wndLocation,
wndCreateTimeLabel,
wndLastAccessLabel,
wndLastWriteLabel,
wndCreateTime,
wndLastAccessTime,
wndLastWriteTime,
wndFindButton,
wndCloseButton
});
// Add the controls to the panel.
wndPanel.Controls.AddRange(new Control [] {
wndLabelFileName,
wndFileName
});
}

/*****************************************************************************
Function: WinFileInfo_activate

Abstract: Invoked when the WinFileInfo application window becomes active.

Input Parameters: source(Object), e(EventArgs)

Returns: Void
******************************************************************************/
private void WinFileInfo_activate (Object source, EventArgs e)
{
}

/*****************************************************************************
Function: buttonFind_click

Abstract: Invoked when the "Find" button is clicked.

Input Parameters: source(Object), e(EventArgs)

Returns: Void
******************************************************************************/
private void buttonFind_click(Object source, EventArgs e)
{
Console.WriteLine("Find Button clicked!!!");
Console.WriteLine (wndFileName.Text);

bool bFileExists = File.Exists(wndFileName.Text);

if (bFileExists)
{
wndFileExistCheck.Checked = true;
Console.WriteLine ("File found");
FileInfo myFile = new FileInfo(wndFileName.Text);
String strDir = myFile.DirectoryName;
DateTime dt = myFile.CreationTime;
FileAttributes attribs = myFile.Attributes;
wndLocation.Text = strDir;

DumpFileAttributes ();

if ((attribs & FileAttributes.Archive) == FileAttributes.Archive)
{
wndArchiveCheck.Checked = true;
}

if ((attribs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
wndReadOnlyCheck.Checked = true;
}

if ((attribs & FileAttributes.Hidden) == FileAttributes.Hidden)
{
wndHiddenCheck.Checked = true;
}

// First get the time when file was created.
DateTime timeFile = myFile.CreationTime;
wndCreateTime.Text = (timeFile.GetDateTimeFormats('F'))[0];

// Get the last access time.
timeFile = myFile.LastAccessTime;
wndLastAccessTime.Text = (timeFile.GetDateTimeFormats('F'))[0];

// Get the last write time.
timeFile = myFile.LastWriteTime;
wndLastWriteTime.Text = (timeFile.GetDateTimeFormats('F'))[0];
}
}

///<summary>
///ButtonClose_Click Method:
///<para>
///This method is invoked when Close button on the dialog box is clicked.
///This method closes the application.
///</para>
///</summary>
/*****************************************************************************
Function: ButtonClose_Click

Abstract: Invoked when the "Close" button is clicked.

Input Parameters: source(Object), e(EventArgs)

Returns: Void
******************************************************************************/
private void ButtonClose_Click(Object source, EventArgs e)
{
Application.Exit ();
}

///<summary>
///Description for DumpFileAttributes method.
///Dumps the underlying values associated with various file
///attributes. The execution of this function is conditional.
///For this function to execute symbol NAVEEN_DEBUG needs to
///defined at the top of the file.
///</summary>
/*****************************************************************************
Function: DumpFileAttributes

Abstract: Dumps the underlying values associated with various file
attributes. The execution of this function is conditional.
For this function to execute symbol NAVEEN_DEBUG needs to
defined at the top of the file.

Input Parameters:

Returns: Void
******************************************************************************/
[Conditional("NAVEEN_DEBUG")] private void DumpFileAttributes ()
{
Console.WriteLine ("Dumping all File Attribute Values");
Console.WriteLine (FileAttributes.Archive);
Console.WriteLine (FileAttributes.Compressed);
Console.WriteLine (FileAttributes.Directory);
Console.WriteLine (FileAttributes.Encrypted);
Console.WriteLine (FileAttributes.Hidden);
Console.WriteLine (FileAttributes.Normal);
Console.WriteLine (FileAttributes.NotContentIndexed);
Console.WriteLine (FileAttributes.Offline);
Console.WriteLine (FileAttributes.ReadOnly);
Console.WriteLine (FileAttributes.ReparsePoint);
Console.WriteLine (FileAttributes.SparseFile);
Console.WriteLine (FileAttributes.System);
Console.WriteLine (FileAttributes.Temporary);
}

/*****************************************************************************
Function: Main

Abstract: Entry point into application.

Input Parameters:

Returns: void

******************************************************************************/
public static void Main ()
{
Application.Run (new WinFileInfo ());
}

//-------------------------------------------------
// We need to import the Win32 API calls used to deal with
// image loading.
//-------------------------------------------------
[DllImport("user32.dll")]
public static extern int LoadIcon(int hinst, String name);
[DllImport("user32.dll")]
public static extern int DestroyIcon(int hIcon);
[DllImport("user32.dll")]
public static extern int LoadImage(int hinst, String lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);
[DllImport("gdi32.dll")]
public static extern int DeleteObject(int hObject);
}

Attachment:
Image002.jpg
Image002.jpg [ 20.6 KiB | Viewed 5695 times ]





Attachments:
FileInfo.zip [16.51 KiB]
Downloaded 812 times

_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .
Author:
Expert
User avatar Posts: 838
Have thanks: 2 time

updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Writing a Windows Form Application For .NET Framework Using
 Web enabling of windows application     -  
 Connecting Java Application to C++ Application from Code     -  
 C++ help with writing a program.     -  
 Which framework is best for beginners in PHP and Why?     -  
 java(collection framework)     -  
 Which is the best PHP framework On CodeIgniter Sharing?     -  
 Writing a Counter to any PORT     -  
 Reading and Writing To text file     -  
 Read XML file content using SAX and writing its as SQL     -  
 Credit Card Fraud Prevention Using .NET Framework in C#     -  



cron






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com