-->

Sunday, September 15, 2013

Introduction to C# : Console Application Tutorial

In this tutorial we will learn basic structure of the c# program . A structure can hold multiple things such as namespaces , main method ,classes etc.
The basic structure of the c# program is

using System;
//import namespaces here

namespace ConsoleApplication4
{
    //create new namespace her also a namespace can contain sub namespace,classes,delegates etc
    class Program
    {
        //class start here . A class can hold data member and member function.  
        static void Main(string[] args)
        {
        }
    }
}


If you want to create a new c# application in visual studio then follow some steps for creating C# console application.
Step-1: Open visual studio
Step-2: Select file->new->project
Step-3: New Window will open , In Left pane you can select your language and right pane you can select application(console application)
Step-4: After selection (left pane-->VB and right pane -->Console application)
Step-5: now at time you can design your first c# application

If you want to write and read something like "hello world" on/from console window  then you should use Console class. This class provide static method for reading and writing . lets take an example if you want to write something on console window then use

Console.WriteLine("hello world");

Similarly if you want to read something from keyboard then use
  string n = Console.ReadLine();

  here is the complete code.
using System;
//import nameapces here

namespace ConsoleApplication4
{
    //create new namespace her also a namespace can contain sub namespace,classes,delegates etc
    class Program
    {
        //class start here . A class can hold data member and member function.
        static void Main(string[] args)
        {
        Console.WriteLine("hello world");
         string n = Console.ReadLine();
         Console.ReadKey();
        }
    }
}

 here is the first line define the declaration of the namespace.
 what is a namespace : The namespace is used to organize your code and is a collection of classes, interface, structs, enums and delegates.
 Main method: main method is the entry point into your application.
Output
Introduction to C# : Console Application Tutorial


Saturday, September 14, 2013

How to use ListBox Control in ASP.NET

The ListBox Control

The List control is a standard Web server control used to select one or more items from a list of items on a web page at runtime.
you can use the Rows property of the ListBox control to specify the height of the control. To enable muliple item selection you can set selection Mode property to ListSelectionMode. Multiple. you can find the
selection item in a single-selection ListBox control with the help of SelectedItem and SelectedIndex
properties. The SelectedItem property returns the  selection item as a List item object, which supports Text,
value and ListBox control and returns each as a List Box controls, the loop runs over the selected item
in the List Box controls and returns each selected item as a ListItem object by using the selectedIndex property.
This ListBox class inherits the :ListControl class and does not have any non-inherited events. The ListControl class does not have any non-inherited methods either. Note that the Items property of the ListControl class returns a collection of ListItem Objects which you can use to access an item in a ListBox.

Lets take a simple example of How to use ListBox Control.

In this example we will show that how to bind ListBox Control using Items collection property
Step-1 : Drop ListBox Control to Design window
Step-2 : Select Edit item using Show smart tag or you can use Items collection property for adding item to the ListBox
How to use ListBox Control in ASP.NET
Step-3 : Add item using ListItem Collection Editor
How to use ListBox Control in ASP.NET

Complete Code

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

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True">
            <asp:ListItem>Apple</asp:ListItem>
            <asp:ListItem>Mango</asp:ListItem>
            <asp:ListItem>Orange</asp:ListItem>
        </asp:ListBox>
    </div>
    </form>
</body>
</html>


Output
How to use ListBox Control in ASP.NET

How to access Text property , Value ,Index from DropDownList in ASP.NET

Top article

Introduction


Text Property : you can access or set the item text which is in DropDownList. If you want to access the text of the DropDownList then simply you can use
String var=DropDownList1.items[i].Text; (this is correct)
Here items[i] is directly denote to collection so you can not use 
String var=DropDownList1.items.Text;  ( This is incorrect)
If you want to add items to the DropDownList , simply you can use add method 
DropDownList1.Items.Add("hello");

Lets take an example to access index,Text and value of the selected item in DropDownList

In above mentioned top article we learn how to bind DropDownList with XmlDataSource in ASP.NET. Also this example show that how to access the text of DropDownList.
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text += "Your Selected value is <br/>" + DropDownList1.SelectedValue;
        Label1.Text += "<br/> Your Selected Text is <br/>" + DropDownList1.SelectedItem.Text;
        Label1.Text += "<br/> Your Selected Item Index is <br/>" + DropDownList1.SelectedIndex.ToString();
     
     
    }

    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>
 
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="XmlDataSource1" DataTextField="Text" DataValueField="url" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml"></asp:XmlDataSource>
 
    </div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

Output
How to access Text property , Value ,Index from DropDownList in ASP.NET

Friday, September 13, 2013

How to bind RadioButtonList control with XmlDataSource in ASP.NET

Introduction

If you want to bind RadioButtonList control with the XmlDataSource then you first create a xml file with some child nodes. So take a Xml file into your projcet.

  • Right click on your project name--> Add-->Add new item
  • Select ".xml" file. Specify name of the file which you want.
<?xml version="1.0" encoding="utf-8" ?>
<Links>
  <link Text="Best Programming Blog" url="http://dotprogramming.blogspot.com" />
  <link Text="Best Gadget Blog" url="http://pc-gadgetworld.blogspot.com/" />
<link Text="Best ASP.NET Site" url="http://www.asp.net" />
<link Text="Best Search Engine" url="http://www.google.com" />
</Links>

Here is the simple structure of the file . <Links> tag is the root tag of other child tag and <Link> tag take some attribute such as Text and url . Now if you bind RadioButtonList control with XmlDataSource then you should specify DataText filed to text and DataVlaue Field is Url

Binding steps

Step-1: Drop RadioButtonList control to design window from toolbox.
Step-2: Choose DataSource using showsmart tag.
Step-3: Select <New Data Source> in open dialog box.
Step-4: Select Xml file in given DataSource.
Select Xml file in given DataSource
















Step-5: Browse xml file from project solution.


Browse xml file from project solution












Step-6: Select Text in DataText field and url in value field.


Select Text in DataText field and url in value field.

















Complete Source Code
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "You are selected " + RadioButtonList1.SelectedItem.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" DataSourceID="XmlDataSource1" DataTextField="Text" DataValueField="url">

        </asp:RadioButtonList>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml"></asp:XmlDataSource>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Output
How to bind RadioButtonList control with XmlDataSource in ASP.NET

How to Customize ToolTip Control: WPF

As in previous post, we have discussed about the tooltip control or tooltip property of the WPF controls. We can customize this tooltip according to our requirements. As in windows operating system, some controls use a rectangle in its tooltip service.

The following code will use a stack panel control in the tooltip of button. The stack panel have two textboxes and an image displayed in the screenshot shown below.
<Button Margin="5" Content="Submit" Width="200" Height="30">
<Button.ToolTip>
<StackPanel Width="180" Height="150">
<TextBlock Margin="2" Text="Introduction of Button" Background="Aqua" Foreground="Red"/>
<Image Margin="2" Source="wall.jpg" Height="100"></Image>
<TextBlock Margin="2" Text="For more help click F1" Background="BlueViolet" Foreground="Azure"/>
</StackPanel>
</Button.ToolTip>
</Button>
Run the project and a button will show, when we hover the mouse on it then the tooltip will display. Look out the following image:

How to Customize ToolTip control in WPF, XAML


Tooltip service have its open and close events to act on appearing and disappearing of the tooltip. It have some special properties to be used mostly by the programmer.

  • ShowDuration: to specify the duration till the tooltip will be shown. It is in millisecond.
  • ShowOnDisable: it enable or disable the tooltip when the control is disabled.
  • Placement: to place the tooltip on desired direction. It may be left, right, bottom, absolute and some more.
  • HasDropShadow: enable or disable the drop shadow effect of tooltip.

Some more properties are there, but can be used as per need.

How to Use ToolTip Control: WPF

In windows, when we hover our mouse on anything (may be an icon, button and etc.) that we are using, then a message displays. That message contains some information about that thing. That message is called tooltip of that thing, which can be enable using the tooltip service of programming.

Just use the below XAML code and a button is placed on the window.
<Button Margin="5" Content="Submit" Width="200" Height="30">
<Button.ToolTip>
Submit your data by Click
</Button.ToolTip>
</Button>

When we hover our mouse on the button, it will show a tooltip (shown in image) containing about the button.

Enable tooltip for the controls in WPF and XAML

Every control automatically has its tooltip property just like the above button. Whatever the tooltip contains becomes the content of that tooltip.

Thursday, September 12, 2013

How to Use Radio Button in: WPF

This is another control derives from the Toggle Button. It has a different in-built feature that the toggle button doesn’t have. That feature is mutual exclusion, it means if 2 or more radio buttons are grouped together on a form/user control, then only one can be checked at a time. User cant checked multiple radio buttons.

When we check a radio button, all others got unchecked in a single group. Following code will show three radio buttons having individual state:
<RadioButton Margin="5" Name="radioButton1" IsChecked="True" Content="Checked"></RadioButton>
<RadioButton Margin="5" Name="radioButton2" IsChecked="False" Content="UnChecked"></RadioButton>
<RadioButton Margin="5"  Name="radioButton3" IsChecked="{x:Null}" Content="InDeterminate"></RadioButton>

Run the above code and it will display three radio buttons displayed in the following image:

How to use Radio Button in WPF

As the toggle button, it has also IsThreeState property for enable its all three states to be used. To group more than radio buttons we have to specify the same GroupName for all the radio buttons.

To show some text with a radio button, we have to use content property as used in above code.

© Copyright 2013 Computer Programming | All Right Reserved