-->

Thursday, November 14, 2013

Exploring the visual Studio 2012 IDE : Part 3

The Solution Explorer window

A solution is defined as a set of projects that are part of the same application in Visual Studio. The Solution Explorer window displays every project with its references and components. If you wish to view Solution Explorer in the IDE's panes, select View->Solution Explorer or press the CTRL+ALT+L keys together.

When you create your application, components may be made up of forms, classes, modules, and any other file types. You can edit these items within the IDE by double-clicking these items through solution explorer.

The Solution Explorer window gives you a graphical representation of all the files your website have.

The Solution Explorer window IN VS2013

From the solution Explorer window, you can open a file by double-clicking the file's icon or name. To rename, copy or delete a file you need to right -click the and select the desired action from the appeared context menu.

The Properties Window

As the name suggest, the properties window in Visual Studio is used to view all the properties for various objects at design time. When you are working with classes, such as TextBox and Web form, you need to change certain attribute of those classes. When you select a component or object in the Solution explorer window or designer, all the properties associated with the selected component are displayed in the properties window. Programmer can view and edit the properties of a file, folder, project or solution using the properties window.

To view the properties window, Select View->Properties window in the menu bar or press the F4 key. Once the properties window is displayed, you can either view the list in alphabetical or categorized form of attributes. You can change the font, font size, back color, fore color, name, text and any other property control (such as button, TextBox) have.
Properties window in vs2013


If you want to view and modify the properties of controls, such as buttons or labels, then drag these controls on the form and select the control whose properties you want to modify. After selecting the appropriate control, the properties associated with that control are displayed in the Properties window.

Every individual control have its own properties, some are common among all like name, text, font. In other words the properties associated with a Button control are somewhat different from the properties associated with a Label control or from those associated with a Web Form. Note that some properties in the properties window appear in gray text. The properties appearing as gray are the read-only properties.

The Properties window also displays a toolbar containing various buttons. These buttons are explained, starting from left to right, as follows:

  • Categorized: Enables you to group the properties for a control into categories. For example, when you select a Button control and click the Categorized button on the toolbar, the properties for the button control are grouped into categories, such as Accessibility, Appearance, Behavior, Data, Layout, and Misc.
  • Alphabetical: Arranges the properties of a control alphabetically. By-default properties are sorted alphabetically.
  • Properties: Displays the properties of a control.
  • Events: Displays various events of the control.
  • Property pages: Displays the Properly Pages dialog box for the selected component. You can use the Property pages dialog box to view and edit properties related to the configuration of the project.

The Properties window is a simple tool that provides you with several benefits. You can save time while programming by using new components because information about each components is easily available and graphically configurable. You can change the properties of the components at design time as well as properties for the project and project solution. 

Windows Store : How to set Image Source in code file

Introduction

For setting Image Source in code file, you must use instance of the BitmapImage class. If your image source is a file referenced by URI, use the BitmapImage constructor that takes a URI parameter. If you want to set image using xaml then use Source attribute in <Image /> tag like.

<Image Source="Image/youtube.png" x:Name="Image1" HorizontalAlignment="Left" Height="221" VerticalAlignment="Top" Width="229"/>

Pre-requisite

  • Windows Phone 8 SDK (included in VS 2013)
  • Developer license
Windows Store : How to set Image Source in code file

Here you will see that your solution contains lots of file.  
  • MainPage.xaml - used to run our app
  • Package.appxmanifest - to describe your app and lists all the files your app contains
  • Image folder contains youtube.png file

Complete code


<Page
    x:Class="App4.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App4"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Image x:Name="Image1" HorizontalAlignment="Left" Height="221" VerticalAlignment="Top" Width="229"/>

    </Grid>
</Page>

Code file

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace App4
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            BitmapImage img = new BitmapImage();
            img.UriSource = new Uri(this.BaseUri,"Image/youtube.png");
            Image1.Source = img;          
        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
     
    }
}

Output


Windows Store : How to set Image Source in code file

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
© Copyright 2013 Computer Programming | All Right Reserved