-->

Wednesday, November 13, 2013

How to use BackgroundWorker control in windows form c#

Introduction

In this example we will learn about BackgroundWorker control with their properties and events. Background worker is normally used where you want to run some operations on separate thread. As the name indicate "BackgroundWorker" means you can run your background thread simultaneously with your Application.

Some useful Public Properties of Background worker class are:

WorkerReportsProgress :  you can retrieve or set the value in WorkerReportProgress. That value indicates whether the BackgroundWorker can report progress updates. This property is capable of indicating to the rest of the application as well as executes.
     
WorkerSupportsCancellation : You can cancel your update asynchronously using set true to WorkerSupportCancellation property.

Lets take an simple example

Step-1 : Add some controls (two button control, one progress Bar and one Background Worker control) on forms.

two button control, one progressBar and one BackgroundWorker control

Step-2 : Set both WorkerReportProgress and WorkerSupportCancellation property to true.
Step-3 : Handle some events (Button_click, backgroundWorker1_DoWork, ProgressChanged, RunWorkerCompleted) which are related to Button and BackgroundWorker.
Step-4 : Write the below code in .cs file


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;


namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

        {

            int count = (int)e.Argument;

            int sum = 0;

            for (int i = 1; i <=count; i++)

            {

                if (backgroundWorker1.CancellationPending)

                {

                    e.Cancel = true;

                    break;

                }

                sum += i;

                backgroundWorker1.ReportProgress(i * 100 / count);

                System.Threading.Thread.Sleep(500);



               

            }

            e.Result = sum;


        }


        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

        {

            progressBar1.Value = e.ProgressPercentage;

        }


        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

        {

            progressBar1.Value = 0;

            if (e.Cancelled)

            {

                MessageBox.Show("Cancel BackGroundWorker");

            }

            else

            {

                MessageBox.Show("Result:" + e.Result);

            }


        }


        private void button1_Click(object sender, EventArgs e)

        {

            backgroundWorker1.RunWorkerAsync(100);

        }


        private void button2_Click(object sender, EventArgs e)

        {

            progressBar1.Value = 0;

            backgroundWorker1.CancelAsync();

        }

    }

}

 
How to use Background control in windows form c#

How to use Background control in windows form c#

How to use Background control in windows form c#

Learn code in detail

Now RunWorkerAsync is the  method of BackgroundWorker compound directly call to DoWork event handler with 100 integer argument. In DoWork event handler count variable contains 100. Every iteration in "for" loop will check whether CancellationPending property is true or false (by-default) that means whether your cancel button is pressed or not. If your cancel button is pressed then CancelAsync() method of the BackgroundWorker will run and Your CancellationPending property is set to true.  

Introduction to HTML: Hyper Text Transfer Language

Introduction

HTML is a document - layout and hyperlink - specification language. It defines syntax and placement of special, embedded direction that are not displayed by the browser, but tell it how to display content of the document, including text, images, and other support media. The language also tells you to how to make a document interactive through special hypertext link, which connect your document with other documents.

HTML in brief

1. HTML stands for Hyper Text Markup Language.
2. HTML is not a programming language and used for data representation on web.
3. A markup language is a set of markup tags which describe how text should be displayed. 

HTML Tags

1. HTML tags are keywords surrounded by angle brackets like <html>
2. HTML tags are used in pairs like <b> and </b>
3. The first tag in a pair is the start tag (opening tag), the second tag is the end tag (closing tag). 

HTML File

1. An HTML file is a text file with HTML tags.
2. HTML file name must end with .htm or .html.
3. An HTML file is a simple text file so it  can be created using simple text editor.
4. An HTML file is often known as HTML document or a web page.

With all multimedia-enabling, new page layout features, and hot technologies that give life to HTML document over Internet. It is also important to understand the languages limitations : HTML is not a word processing tool, a desktop publishing solution, or even a programming language. Its fundamental purpose is to define the structure and appearance of document and document families so that they may be delivered quickly and easily to user. 

Structure of an HTML Document

An HTML document consists of :
1. Text: which defines the content of the document
2. Tags: which defines the structure and appearance of the document .

The structure of HTML document is simple , consisting of an outer <html> tag enclosing the document head and body :
<html>
  <head>
     <title>Title of page</title>
  </head>
  <body>
      This is an Example of simple <b>HTML document </b>
  </body>
</html>

HTML file is a plain text so it can be created using any text editor  and can be saved as filename.htm.
Each document has a head and a body, delimited by the <head> and <body> tags. The head where you give HTML document a title and where you indicate other parameters the browser may use when displaying the  document. The body is where you put the actual contents of HTML documents. This includes the text to display and document control markers (tags) that advise the browser how to display text. Tags also references special-effects files, including sound and graphics, and indicate the hyperlinks and anchors that are used to link document to other document.

Tuesday, November 12, 2013

Reusing Components of a XML Schema

One of the key features of schemas is their ability to support a high degree of reusability among other schemas. This can be done by using import elements.

The include element


The include element is used to include or refer to an external schema that is located at a definite address. The syntax for using the include element is as follows:

<include id="ID" schemaLocation="filename"/>

In the preceding syntax, the include element consists of two attributes, id and schemaLocation. The id attribute is used to specify the element ID. The ID must be unique within the XSD (XML Schema Definition) document. The id attribute is optional. The another attribute is schemaLocation attribute. The value of this attribute specifies the physical location of the schema file.

The include element can have multiple occurrences in an XSD document. The schema element is the parent element of the include element. The only restriction placed on the use of include element is, both the containing and contained schema files must belong to the same target namespace.

A target namespace has a reference to the URI to which the schema belongs. You can create different target namespaces for the different schemas. For example, you can create a schema that contains the declarations for the elements and attributes required to store the purchase order details. You can then specify www.encomatcybershoppe.com/purchase as the targetnamespace for the schema.

The include element allows reference to any external schema that is defined in the context of the same target namespace. The target namespace for a schema is declared using the target Namespace attribute of the schema element.

Consider the following:


<schema xm1ns="http://www.w3.org/2001/XMLSchema"
targetNamespace="www.ecomatcybershoppe.com/purchase">
<simpleType name="prstring">
      <restriction base="string">
            <pattern value="[p] {1} \d {3}"/ >
      </restriction>
</simpleType>
</schema>

In the preceding example, a simple data type called prstring is created in an XML schema. This data type has a restriction that specifies that an element or attribute that contains prstring as its type should match a specific pattern. You can use the prstring data type in other XML schemas, as follows:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="www.ecomtcybershoppe.com/purchase"
xmlns:prd="www.ecomatcybershoppe.com/purchase">
<include schemaLocation="potype.xad"/>
                :
<element name="PRODID" type="prd:prstring"/>
                :
</schema>

In the preceding code snippet, the default namespace is specified as
http://www.w3.org/2001/XMLSchema. When you do not include any prefix with the element name or the data type, it is assumed that the element or data type belong to the default namespace. The target namespace is specified as www.ecomatcybershoppe.com/purchase.

Note that the target namespaces are the same in both the schemas. The prd prefix is used as an alias to refer to the namespaces URI www.ecomatcybershoppe.com/purchase. Now, can refer to the data types declared in potype.xsd, which is the physical location of the schema file in the target namespace, by using the prd prefix before the name of the data type. If you do not use the prefix, prstring will be considerd to belong to the default namespace.

The import Element


The import element performs the same function as the include element. however, the import element allows you to access components from multiple schemas that may belong to different target namespaces.

The syntax for using the import element is as follows:

<import id="ID" namespace="namespace" schemaLocation="filename" />

In preceding syntax, the import element contains three attributes:
  • The id attribute takes the ID of the element as its value. This ID must be unique for the XSD document. This attribute is optional.  
  • The namespace attribute specifies a namespace URI to which the imported schema belongs. The namespace attribute also specifies the prefix used to associate an element or an attribute also with a particular namespace.
  • The schemaLocation attribute is identical to the value used by the include element. The value of this attribute is set to the physical location of the specified schema file.
While importing an XML schema, the importing  schema must contain a namespace reference to the target namespace of the imported schema. This namespace prefix can then be appended to the elements that are declared in the imported document, but used in the importing document.

Windows Store:How to Add item in GridView

  1. Add a GridView control to a parent container using XAML code editor with name attribute.
<GridView x:Name="GridView1" >           

</GridView>

To assign a name to the grid view, set the x:Name attribute to a string value. To refer to a control in code, it must have a name. Otherwise, a name is not required.

2. Add items to the grid view by populating the Items collection or by binding the ItemsSource property to a data source.
 
<GridView x:Name="GridView1" > 
<x:String> name </x:String>
<x:String> City </x:String>          

</GridView>

Start Screen

Windows Store:How to Add item in GridView
 
Windows Store:How to Add item in GridView
 

Apply II method in code file

1. Create a List of String Items and bind with GridView control in main file constructor.
 

public MainPage()

{

 

this.InitializeComponent();

List<String> itemcoll = new List<String>();

itemcoll.Add("Apple");

itemcoll.Add("Mango");
itemcoll.Add("Orange");

GridView1.ItemsSource = itemcoll;

}

 

Sunday, November 10, 2013

How to set PictureBox Image using OpenFileDialog or Bitmap Class: WindowsForms

A person’s basic properties are name, age, date of birth, contact no. and also have a picture of that person. To insert the picture we have to preview that picture at the same time we are loading the picture. That’s why we need this picture box control on the windows forms.

Picture Box control is used to display pictures that may be jpeg, jpg, png or any bitmap file. The picture can be set during run time by the user, or can be fixed by the programmer during design time. It have many properties to be used by the programmer, to make the picture adjustable.
Picture box have some common properties that are mostly used by, which are:

  • SizeMode: used to control the clipping and positioning of the image in the display area.
  • ImageLocation: specify the image to be displayed.
  • ClientSize: used to change the size of the display area at run time.
  • BorderStyle: to distinguish the control from the rest of the form, even it have no image.

Drag-n-drop a picture box which will hold the image and button to be used to browse the image. The form will look like the image below:

How to set PictureBox Image using OpenFileDialog or Bitmap Class: WindowsForms

Generate the click event of button and write the following C# code:
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
pictureBox1.ImageLocation = ofd.FileName;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

When you run this project and click on the button, it will show an openFileDialog box. You have to select an image file and click on OK button. At the last the picture has been set on the picture box as shown in the below image.

How to set PictureBox Image using OpenFileDialog or Bitmap Class: WindowsForms

It is not required to set the picture as above, we can set an object also of the Image class in the System.Drawing namespace. For that, just create an object of Bitmap class and set that object to the image property of picture box, as described in the below code:
Bitmap bt = new Bitmap(“image path”);
pictureBox1.Image = bt;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

The most thing to be noticed in the second code, is we have to know the exact path of the image file. Run the code and it will give the same output as the first code provide.

See Also: OpenFileDialog in windows forms

Saturday, November 9, 2013

Attendance Management Project in Windows Forms with C#

Download this project

Project cost : 300Rs or $10
Pay me at:


Via bank transfer

 
ICICI bank account number is :    153801503056
Account holder name is :                 Tarun kumar saini
IFSC code is :                                   ICIC0001538

SBBJ bank detail :                             61134658849
Account holder name:                        Tarun kumar saini
IFSC code :                                        SBBJ0010398

CONTACT ME

Introduction

All the institutions are following almost the same procedure of storing the attendance of student i.e. registers or may be an excel file. When counting the attendances at the last they have to manually count for each student, or by insertion of some formula in excel file.

Attendance management project will simply provide all the common features, it should have. Through this project one can add new student, assign them attendances monthly and at the last a report can also be generated as a hard proof. The person, know the username and password, can only be permissible to use this software.

The solution have another class library project that is used to create the database using entity framework. Database has only two tables i.e. Attendance and Student, each have its own field.

Features of Project: 


  • Administrator can only login into the software.
  • User can add/modify student’s details as per the requirements.
  • Can search attendance according to either student name or roll no.
  • A report can be generated at the last as a proof for guardian/parents.

System Requirements:



  • Visual Studio 2010 or higher
  • SqlServer 2008 or higher
  • DotNet Framework 4.0 or higher (pre-loaded with Visual Studio)

Run the project:

It is a windows form application, so either press F5 or click on Start button. It will load the login window, which requires username and password. Username is “admin” and password is “password”, click on login button and the manage options window will be shown to you.

Screenshots:
New Attendance: The form through which the user can add attendance according to the roll no.

Attendance Management Project in Windows Forms with C#

Attendance List: Let the user search the attendance according to the student name.
Attendance Management Project in Windows Forms with C#

Attendance via RollNo: User can count attendance via roll no of the student.

Attendance Management Project in Windows Forms with C#

Report Form: The form will show you how the report will look like, you can change this design using the report window.

Attendance Management Project in Windows Forms with C#

If you want to purchase this please contact me on :  narenkumar851@gmail.com

Read Also:
  Lab Digitization Project

Binding Data with BindingNavigator in Windows Forms

There are some steps to bind the data with DataSet object. Theses steps are
Step-1: Create a Windows Form.
Step-2 : Add Binding Navigator and control (Three Label control and Three TextBox control) on the form
Controls and binding navigator on the windows form
Step-3: Right Click on First TextBox and select properties .
Step-4: Select "Add project data source " link  in Text property of (DataBindings) tab.
"Add project data source " link  in Text property of (DataBindings) tab

Step-5: Now open Data Source configuration wizard.
Step-6: Select Database in configuration wizard and click next button.
Select Database in configuration wizard

Step-7 : Select DataSet in Database model and click next.
Select DataSet in Database model

Step-8:  Choose your Data Connection and create connection string.
Data Connection and create connection string.
Step-9: Choose Database object which you want like Table,View etc.
 Database object which you want like Table,View
Step-10: Click to Finish Button.
Step-11: In connected data source select single field or column which you want to connect with first TextBox.
DataBinding in windows form

Step-12: Similarly bind city and country with remaining TextBox control.
Step-13: Set BindingSource property of the BindingNavigator is given DataSet.
Step-14: Run your application.
Binding Data with BindingNavigator in Windows Forms

Binding Data with BindingNavigator in Windows Forms

© Copyright 2013 Computer Programming | All Right Reserved