-->

Wednesday, August 28, 2013

How to use RadioButton Control in ASP.NET

The RadioButton Control

Similar to the CheckBox control , a RadioButton control creates a single radio button . The RadioButton control exists within the System.Web.UI.WebControls namespace . The RadioButton controls can be grouped, where only one RadioButton control can be selected at a time. In web forms, You have to set the RadioButton's GroupName property to the same value to associate them into a group.

Use of the RadioButton

  • Where you want to select only single item in given multiple items.
  • In BioData Forms you have to select gender
How to use RadioButton Control in ASP.NET

Public Properties of the RadioButton Class

GroupName : Obtains the name of the group to which radio button belongs.

Example of RadioButton Control in ASP.NET


<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        result.Text = "You have Selected <br/>" + RadioButton1.Text;
        RadioButton2.Checked = false;

    }

    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        result.Text = "You have Selected <br/>" + RadioButton2.Text;
        RadioButton1.Checked = false;

    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>RadioButton Example

    </h3>
        <h3>Select The car you want to Buy</h3>
        <p>
            <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" OnCheckedChanged="RadioButton1_CheckedChanged" Text="Maruti Zen" />
        </p>
        <p>
            <asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" Text="Honda City" OnCheckedChanged="RadioButton2_CheckedChanged" />
        </p>
        <p>
            <asp:Label ID="result" runat="server"></asp:Label>
        </p>

    </div>
    </form>
</body>
</html>

OutPut
How to use RadioButton Control in ASP.NET

Tuesday, August 27, 2013

How to use CheckBoxList Control in ASP.NET

The CheckBoxList Control

you can use a CheckBoxList control to display a number of check boxes at once as a column of check boxes. The CheckBoxList control exists within the System.Web.UI.WebControls namespace. This control is often useful when you want to bind the data from a datasource to checkboxes . The CheckBoxList control creates multiselection checkbox groups at runtime by binding these controls to a data source.



Use of CheckBoxList Control

  • Where you find multiple options.
  •  In Shoping Site where you find sub category under category 
How to use CheckBoxList Control in ASP.NET

Example of CheckBoxList Control in ASP.NET 

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string s = "Your selected books are<br/>";
        foreach (ListItem s1 in CheckBoxList1.Items)
        {
            if (s1.Selected)
            {
                s = s + s1.Text + "<br/>";


            }
        }
        result.Text = s;

    }

    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div >
Select Your Favorite books:<br />
            <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True">
                <asp:ListItem>ASP.NET </asp:ListItem>
                <asp:ListItem>WINDOWS FORMS</asp:ListItem>
                <asp:ListItem>WPF</asp:ListItem>
                <asp:ListItem>WCF</asp:ListItem>
                <asp:ListItem>JAVA SCRIPT</asp:ListItem>
            </asp:CheckBoxList>


        </div>
        <asp:Label ID="result" runat="server"></asp:Label>
    </form>
</body>
</html>


Output
How to use CheckBoxList Control in ASP.NET

This example shows that you can choose multiple item in given list. Here foreach loop is running from starting index to ending imdex. When you select ASP.NET book item in the given list then your item will appear on the Label control under the CheckBoxList. If you again select Windows Forms Book in given list , PostBack will occurs and your result will appear on Label Control with previous result.   

Monday, August 26, 2013

Print Image using PrintDialog Box

To print images in windows forms Graphics class and its various methods are used. Graphics class provides methods for sending objects to a device such as printer. Add a printDocument,  printDialog and a button to the windows form application.

In the code behind file create an object of Bitmap class which will be used to create an image. Write a function to capture the screen to be printed just like written in the below code:
private void CaptureScreen()
{
Graphics graphics = this.CreateGraphics();
graphics.DrawRectangle(Pens.Black, 50, 50, 300, 100);
image = new Bitmap(300, 300, graphics);
Graphics g = Graphics.FromImage(image);
g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
}
In the above code a rectangle will be drawn at the given location. CopyFromScreen() function used to performs a bit-block transfer of the color data, corresponding to a rectangle of pixels, from the screen to the drawing surface.

In the click event of code write the following code:
CaptureScreen();
printDocument1.PrintPage += printDocument_PrintPage;
printDialog1.Document = printDocument1;
DialogResult res = printDialog1.ShowDialog();
if (res == DialogResult.OK)
printDocument1.Print();
The above code is somewhat same to the code used in printing text except the calling of CaptureScreen() method.

Print page event of print document should be look like:
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(image, 50, 100);
}

Run the project and click on print button. On the print Dialog box click on ok with selected printer and it will print whatever is on your screen at the given points. When I print this then it had printed as the following image:

Print image using PrintDialog Box
See also: Printing Text

PrintDialog Box Control in Windows Forms

To print any text or image, print Dialog box is used which is in-built control in windows forms. To invoke the default print dialog box, you need to either add the PrintDialog and PrintDocument control from the toolbox or create an instance of the same classes. The following image shows the default Print Dialog box.

PrintDialog box Control in windows forms

Printing Text

In this topic we will print the text from textbox to the printer attached with the computer. Just add a textbox and a button to the form.

Printing text form preview in windows forms

The click event of button should be look like the following code in C# language:
private void button1_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
DialogResult res = printDialog1.ShowDialog();
if (res == DialogResult.OK)
printDocument1.Print();
}
Now when we check out the above code, text to be print is not specified there. To specify the text to be print printPage event of print document is used. The print page event will be:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(textBox1.Text, new Font("Tahoma", 25), Brushes.Black, 150, 20);
}
Here above the drawstring method draws the specified text at the given location with given properties. If we don’t want to add print dialog and print document from the toolbox then we have to instantiate both of the classes in our code. Just write the following code (C# language) in the click event of button:
PrintDocument pd = new PrintDocument();
pd.PrintPage += pd_PrintPage;

PrintDialog printDialog = new PrintDialog();
printDialog.Document = pd;

DialogResult res = printDialog.ShowDialog();
if (res == DialogResult.OK)
pd.Print();
The second line in above code will be used to generate the printPage event of printDocument. The code of this event will be the same as written first.
Run the project and write something in the textbox e.g. “Print Example”. It will print a page written the above text on the location given above.
There are some properties of the printDocument class, some of which are DefaultPageSettings, PrinterSettings, DocumentName, PrintController and etc.

See also: Print Image using Print Dialog

FolderBrowserDialog Control in Windows Forms

Browse for folder dialog box is used to select a folder to save or open a file. Most often you may need to select a particular folder to open or save a file in windows based application. It enables user to select any folder on system and to retrieve the path of a folder selected by the user.
Following image shows the Browse for Folder dialog box:
This dialog box is similar to open file dialog box except that it enables user to work with folders. To show this type of dialog box just add Folder Browser Dialog box from toolbox or create an object of this class.

Write folderBrowserDialog1.ShowDialog(); in the click event of button.

To open the default browse for dialog box write the following code in the click event of a button:
private void folderBrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.ShowDialog();
}

See also: OpenFileDialog Box Control

Sunday, August 25, 2013

How to use CheckBox Control in ASP.NET

Introduction
The Checkbox control creates a check box that can be selected by clicking it. The control exists within the System.Web.UI.WebControls namespace. The Checkbox control displays checkmarks that allow the user to toggle between a True or False condition.

Public Properties of the Checkbox Class
AutoPostBack : Obtains a value showing , if the CheckBox state automatically post back to the server when clicked or not.
Checked : Obtains a value showing , if the CheckBox control is checked or not.
Text : Obtains the text label associated with the CheckBox control.

Example of CheckBox control in ASP.NET


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>CHECKBOX EXAMPLE</h2>
        <br/>
        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" Text="Apple" />

        <br />
        <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" Text="Mango" />

    </div>
        <asp:CheckBox ID="CheckBox3" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox3_CheckedChanged" Text="Orange" />
        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server" Height="145px" Width="112px"></asp:ListBox>
    </form>
</body>
</html>

Codebehind Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1 .Checked==true)
        {
            ListBox1.Items.Add(CheckBox1.Text);

         
        }
        else
        {
            ListBox1.Items.Remove(ListBox1.Items.FindByText(CheckBox1.Text));
        }

    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox2.Checked == true)
        {
            ListBox1.Items.Add(CheckBox2.Text);


        }
        else
        {
            ListBox1.Items.Remove(ListBox1.Items.FindByText(CheckBox2.Text));
        }

    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox3.Checked == true)
        {
            ListBox1.Items.Add(CheckBox3.Text);


        }
        else
        {
            ListBox1.Items.Remove(ListBox1.Items.FindByText(CheckBox3.Text));
        }

    }
}

Code Generate the following Output
How to use CheckBox Control in ASP.NET
This example shows when you select any one checkbox which is taken in this example then your checkbox Text add to the ListBox . If you unselect it then remove Text from the list.Because AutoPostBack property is working here.
ListBox1.Items.Remove() : If you want to remove item from list then use remove method of the list.
ListBox1.Items.FindByText() : If you want to search any item from the list then use FindbyText() method.

Saturday, August 24, 2013

XNA : How to show Image in Windows Phone Game

Step-1 : Right below the SpriteBatch spriteBatch ; line , add the following :

GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D texture;
        Rectangle current = new Rectangle(40, 40, 100, 75);
These are all the variables you will need for the show image in window Phone game , here is a quick breakdown:
texture : The Texture2D class holds a two dimensional image . We will define a small texture in memory to use when drawing the image.
current : The XNA Framework defines a structure called Rectangle that can be used to represent an area of the display by storing the x and y position of the upper left corner along with a width and height.


Step-2 : Add the following code to the LoadContent( ) method after the spriteBatch initialization:
spriteBatch = new SpriteBatch(GraphicsDevice);
            texture = Content.Load<Texture2D>(@"squ");
Here:
1. Download image from internet
2. Save the image as squ.bmp in a temporary location.
3. Back in visual c# Express , Right-click on WindowsPhoneGame1Content (Content) in solution Explorer (You may need to scroll down to see it) and select Add | Existing item.
Browse to the image you downloaded and click on ok.

Step-3 : Add the following code after the call to clear the display

GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(texture, current, Color.Red);
            spriteBatch.End();


            base.Draw(gameTime);


Output

XNA : How to show Image in Windows Phone Game

Source code :


© Copyright 2013 Computer Programming | All Right Reserved