Tuesday, 22 July 2014

StackPanel in WPF

StackPanel places child elements in vertical or horizontal stack. It is one of the popular panel because of its simplicity and usefulness. By Default, StackPanel's child element grows from top of the panel to the bottom i.e. Vertical orientation. We can control the position of elements using HorizontalAlignment or VerticalAlignment and control the spacing using margin and padding properties. Let's create various layout using Stackpanel.

Example: Creating Simple Layout using StackPanel.

In this Example, I have added one TextBlock and 5 Buttons in StackPanel. By default, StackPanel arranges child elements from top to bottom. All Elements inside the StackPanel Streched to the full width of the StackPanel.

<Window x:Class="StackPanelExample1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="250" Width="525">
    <StackPanel>
        <TextBlock Text="This is TextBlock"></TextBlock>
        <Button Content="Button 1"></Button>
        <Button Content="Button 2"></Button>
        <Button Content="Button 3"></Button>
        <Button Content="Button 4"></Button>
        <Button Content="Button 5"></Button>
    </StackPanel>
</Window>
Preview:

Example: Changing the Orientation of StackPanel to horizontal

In this Example, We arrange Child Elements of StackPanel horizontally by setting Orienatation property value as Horizontal. Now Elements are streched to the full height of StackPanel.

<StackPanel Orientation="Horizontal">
        <TextBlock Text="This is a TextBlock"></TextBlock>
        <Button Content="Button 1"></Button>
        <Button Content="Button 2"></Button>
        <Button Content="Button 3"></Button>
        <Button Content="Button 4"></Button>
        <Button Content="Button 5"></Button>
</StackPanel>
Preview:

Example: Changing the FlowDirection property of StackPanel

On setting FlowDirection property RightToLeft of a StackPanel(with Horizontal orientation), stacking of child elements starts from Right to Left . Default behaviour is from LeftToRight.

<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft">
        <TextBlock Text="This is a TextBlock"></TextBlock>
        <Button Content="Button 1"></Button>
        <Button Content="Button 2"></Button>
        <Button Content="Button 3"></Button>
        <Button Content="Button 4"></Button>
        <Button Content="Button 5"></Button>
</StackPanel>
Preview:

Example: Changing HorizontalAlignment property of Child Elements in StackPanel

By Default, HorizonatalAlignment propert is Stretch for all the Child Elements that's why Child element takes the full width of the Stackpanel. We can change HorizontalAlignment to Right,Left,Center,Stretch.

<StackPanel>
        <TextBlock Text="This is a TextBlock"></TextBlock>
        <Button Content="Button 1" HorizontalAlignment="Right"></Button>
        <Button Content="Button 2" HorizontalAlignment="Left"></Button>
        <Button Content="Button 3" HorizontalAlignment="Center"></Button>
        <Button Content="Button 4" HorizontalAlignment="Stretch"></Button>
        <Button Content="Button 5"></Button>
</StackPanel>
Preview:

Example: Changing VerticalAlignment property of Child Elements in StackPanel with Horizontal Orientation

<StackPanel Orientation="Horizontal">
        <TextBlock Text="This is a TextBlock"></TextBlock>
        <Button Content="Button 1" VerticalAlignment="Bottom"></Button>
        <Button Content="Button 2" VerticalAlignment="Top"></Button>
        <Button Content="Button 3" VerticalAlignment="Center"></Button>
        <Button Content="Button 4" VerticalAlignment="Stretch"></Button>
        <Button Content="Button 5"></Button>
</StackPanel>
Preview:

Important Point: Setting HorizontalAlignment property of a Child Element in a StackPanel with Horizontal Orientation is meaningless because Each Element get the exact amount of space they actually need and there is no space to move element Hozrizontally. Setting VerticalAlignment property of Child Element in a StackPanel with Vertical Orientation is also meaningless.

Example: Setting Margin Property in StackPanel
Using the margin property, we can set the space between the StackPanel's edge and Window/Container. Here, We set Margin="16" for all the four sides. We can also set different margin for each side (Margin="left,top,right,bottom").

<StackPanel Margin="16">
        <TextBlock Text="This is a TextBlock" Background="BurlyWood"></TextBlock>
        <Button Content="Button 1"></Button>
        <Button Content="Button 2"></Button>
        <Button Content="Button 3"></Button>
        <Button Content="Button 4"></Button>
        <Button Content="Button 5"></Button>
</StackPanel>
Preview:

Example: Nested StackPanel
We can use another StackPanel in Stackpanel. For showing this example, I have created a mini Application in which Orientation of Outer Stackpanel is Vertical(default) and Inner Stackpanel is with Horizontal Orientation and  HorizontalAlignment to Center.

<StackPanel Margin="30">
        <TextBlock Text="Select your Favorite Programming language" FontSize="14"></TextBlock>
        <RadioButton Margin="10,10,0,0">C#</RadioButton>
        <RadioButton Margin="10,10,0,0">Java</RadioButton>
        <RadioButton Margin="10,10,0,0">VB.Net</RadioButton>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Content="Vote" Margin="5" MinWidth="60"></Button>
            <Button Content="Exit" Margin="5" MinWidth="60"></Button>
        </StackPanel>
</StackPanel>
Preview:
That's All.Hope you like it.
Thanks.
[Download Source Code from Google Drive]

Sunday, 20 July 2014

Getting IP Address and Host Name using DNS Class in C#

In general, When we have to find IP Address of a machine, we run ipconfig command in command prompt.


We can find the same with C# using DNS Class. DNS (Domain Name Service) class is present in System.Net namespace that has methods to retrieve IP Address, Host Name etc.

Example: Program for getting Host Name
Firstly, Add System.Net namespace in namespace declaration. GetHostName() method of Dns class returns the host name of the local computer.

string HostName = Dns.GetHostName();
Console.WriteLine("Name of machine ="+HostName);

Preview:

Example: Program For getting IP Address of Host(Local)
Dns class provides GetHostAddresses() method which takes HostName as a parameter and returns an array of IPAddress. We get both IPv4 and IPv6 of the machine.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace GetHostNameAndIPAddress
{
    class Program
    {
        static void Main(string[] args)
        {
            string HostName = Dns.GetHostName();
            Console.WriteLine("Host Name of machine ="+HostName);
            IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);
            Console.WriteLine("IP Address of Machine is");
            foreach(IPAddress ip in ipaddress)
            {
                Console.WriteLine(ip.ToString());
            }
        }
    }
}
Preview:

Example: Program for getting IPAddress of Domain name

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace GetIPusingHostname
{
    class Program
    {
        static void Main(string[] args)
        {
            string HostName = "www.bing.com";
            IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);
            Console.WriteLine("IPAddress of " + HostName + " is");
            foreach (IPAddress ipaddr in ipaddress)
            {
                Console.WriteLine(ipaddr);
            }
        }
    }
}
Preview:

Example: Program for Getting IPv4 and IPv6 Separately by checking the Address Family

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace GetIPv4andIPv6Seperate
{
    class Program
    {
        static void Main(string[] args)
        {
            string HostName = Dns.GetHostName();
            Console.WriteLine("Host Name of machine =" + HostName);
            IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);
            Console.Write("IPv4 of Machine is ");
            foreach (IPAddress ip4 in ipaddress.Where(ip => ip.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork))
            {
                Console.WriteLine(ip4.ToString());
            }
            Console.Write("IPv6 of Machine is ");
            foreach (IPAddress ip6 in ipaddress.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))
            {
                Console.WriteLine(ip6.ToString());
            }
        }
    }
}
Preview:

Example: Program for getting Host Name based on IPAddress
getHostEntry() method of DNS class takes IPAddress as parameter and returns IPHostEntry that contains address information about the host specified in address.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace GetHostNameUsingIP
{
    class Program
    {
        static void Main(string[] args)
        {
            string IPAdd = "204.79.197.200";
            IPHostEntry hostEntry = Dns.GetHostEntry(IPAdd);
            Console.WriteLine(hostEntry.HostName);
        }
    }
}
Preview:

Hope you like it. Thanks
[Download Source Code from Google Drive]

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Subscribe us on YouTube

Like us on Facebook

Follow us on Google+