Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Monday 27 October 2014

Reverse a string in C#

In this Article, We will learn How to reverse a string using C#.

using System;

namespace reverseString
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "", reverse = "";
            int Length = 0;
            Console.WriteLine("Enter a Word");
            //Getting String(word) from Console
            str = Console.ReadLine();
            //Calculate length of string str
            Length = str.Length - 1;
            while(Length>=0)
            {
                reverse = reverse + str[Length];
                Length--;
            }
            //Displaying the reverse word
            Console.WriteLine("Reverse word is {0}",reverse);
            Console.ReadLine();
        }
    }
}
Preview:

Hope you like it. Thanks.
[Download Source Code via Google Drive]
Watch video:

Tuesday 21 October 2014

ToolTip in WPF

ToolTip show some information or Hint about a control in a floating box when mouse is hover on that control and it disappears when mouse is moved away from that control. In this Article, We will learn How to use ToolTip and its properties. We learn following topics in this Article:
ToolTip Topics WPF

Let's begin:

Basic ToolTip Example:
Drop Button from toolbox and add ToolTip property in Button with a Message you want to show on hovering the mouse on the button control. We can add ToolTip in two ways. First button shows the simplest way to display ToolTip. Second method is preferred for creating customized ToolTip.

<Button Content="Click Here" Margin="30" FontSize="16" ToolTip="Click Here"></Button>
<Button Content="Click Here" Margin="30" FontSize="16">
        <Button.ToolTip>
               <ToolTip>
                   Click Here
               </ToolTip>
        </Button.ToolTip>
</Button>
Preview:
ToolTip in WPF Basic Example

Customized/Complex TootTip Example:
In this Example, We have created our ToolTip Layout with the help of StackPanel and other controls in ToolTip Element

<Button Content="Print" FontSize="16">
                <Button.ToolTip>
                    <ToolTip>
                        <StackPanel Width="200">
                            <StackPanel Orientation="Horizontal" Background="Tan"       Width="200">
                                <Image Source="printer.png" Margin="10 5"></Image>
                                <Label Content="Print" Margin="10 5" FontSize="20" FontWeight="Bold"></Label>
                            </StackPanel>
                            <TextBlock Text="Please Select your printer before giving Print Command" FontSize="14" TextWrapping="WrapWithOverflow"></TextBlock>
                            <Line Stroke="Gray" StrokeThickness="2" X2="200"></Line>
                            <TextBlock Text="Press F1 for Help" FontWeight="ExtraBold"></TextBlock>
                        </StackPanel>
                    </ToolTip>
                </Button.ToolTip>
</Button>
Preview:
Custom ToolTip WPF

ToolTipService.ShowDuration Property:
ToolTipService Class provides various properties to control display and behavior of ToolTips. Using ToolTipService.ShowDuration Property, We can set the amount of time that a ToolTip remains visible when the user places mouse pointer over the control that defines the ToolTip. Its default value is 5000 milliseconds.

<Button Content="ToolTipService.ShowDuration Property"
        ToolTip="ClickMe" ToolTipService.ShowDuration="5000">
</Button>
Preview:
ToolTipService.ShowDuration Property:

ToolTipService.ShowOnDisabled Property:
When Control is disabled then ToolTip does not show on mouse hovering the control. If you want to display Tooltip on disabled control(i.e. IsEnabled property set to False) than We have to set ToolTipService.ShowOnDisabled attached property to True.

<Button Content="Disabled Button" ToolTip="Click Me" IsEnabled="False" FontSize="16"></Button>
<Button Content="Disabled Button(ToolTipService.ShowOnDisabled)"
        ToolTip="Click Me" IsEnabled="False"
        Margin="0 10" FontSize="16"
        ToolTipService.ShowOnDisabled="True">
</Button>
Preview:
ToolTipService.ShowOnDisabled Property

ToolTipService.InitialShowDelay Property:
When user hover mouse on Button, there is a delay before the ToolTio is displayed. We can set this delay using ToolTipService.InitialShowDelay Property.

<Button Content="ToolTipService.InitialShowDelay='1000'"
        ToolTip="Click Me" ToolTipService.InitialShowDelay="1000">
</Button>

HorizontalOffset and VerticalOffset Property of ToolTip:
We can adjust position of ToolTip using HorizontalOffset and VerticalOffset property of ToolTip. HorizontalOffset Property sets the horizontal distance between the Mouse Pointer and the ToolTip popup and VerticalOffset Property sets the vertical distance between the Mouse Pointer and the ToolTip popup.

<Button Content="HorizontalOffset and VerticalOffset" FontSize="16">
        <Button.ToolTip>
                <ToolTip HorizontalOffset="20" VerticalOffset="20">
                    Click Me
                </ToolTip>
        </Button.ToolTip>
</Button>
Preview:
HorizontalOffset and VerticalOffset Property of ToolTip:

HasDropShadow Property of ToolTip:
HasDropShadow Property sets the dropped shadow of Tooltip.

<Button Content="HasDropShadow='True'" FontSize="16">
        <Button.ToolTip>
                <ToolTip>
                     Click me
                </ToolTip>
        </Button.ToolTip>
</Button>
<Button Content="HasDropShadow='False'" FontSize="16" Margin="0 40">
        <Button.ToolTip>
                <ToolTip HasDropShadow="False">
                     Click me
                </ToolTip>
        </Button.ToolTip>
</Button>
Preview:
HasDropShadow Property of ToolTip

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

Sunday 19 October 2014

Reverse a Number in C#

In this Blog, We will learn How to reverse a no. using C#.

using System;

namespace ReverseNo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a No. to reverse");
            int Number = int.Parse(Console.ReadLine());
            int Reverse = 0;
            while(Number>0)
            {
                int remainder = Number % 10;
                Reverse = (Reverse * 10) + remainder;
                Number = Number / 10;
            }
            Console.WriteLine("Reverse No. is {0}",Reverse);
            Console.ReadLine();
        }
    }
}
Preview:
[Download Source Code via Google Drive]

Watch Video:

Saturday 4 October 2014

GridSplitter in WPF

A GridSplitter is a divider which divides a Grid into two Section. GridSplitter allows us to resize rows or columns in a Grid by dragging the GridSplitter Bar. One of the example of GridSplitter you have seen in Windows Explorer.

Let's Begin:

Adding a Vertical GridSplitter in a Grid:
In this Example, I have added three columns. Out of three Columns, I have added GridSplitter in 2nd Column. I set width of 2nd column as auto so that 2nd columns is sized automatically to fit the GridSplitter. I am using the entire column for placing the GridSplitter which is the best way for placing the Splitter in a Grid. We can also place the Splitter within the column containig some content. For creating a vertical Splitter, set VerticalAlignment property to stretch and HorizontalAlignment property of GridSplitter to Center (for creating Horizontal Splitter set VerticalAlignment property to Center and HorizontalAlignment property of GridSplitter to stretch). Set width and background color property of GridSplitter.

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <TextBlock Text="Some Text here" FontSize="16" Margin="10" TextWrapping="Wrap"></TextBlock>
            <TextBlock Text="Some Text here" FontSize="16" Margin="10" TextWrapping="Wrap"></TextBlock>
            <TextBlock Text="Some Text here" FontSize="16" Margin="10" TextWrapping="Wrap"></TextBlock>
            <TextBlock Text="Some Text here" FontSize="16" Margin="10" TextWrapping="Wrap"></TextBlock>
        </StackPanel>
        <TextBlock Text="Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here" Grid.Column="2" TextWrapping="Wrap" FontSize="16" Margin="10"></TextBlock>
        <GridSplitter HorizontalAlignment="Center"
                      VerticalAlignment="Stretch"
                      Grid.Column="1"
                      Width="5" Background="Silver">
        </GridSplitter>
</Grid>
Preview:

GridSplitters in Nested Grid:
For showing this, I have added 3 columns in Grid and place GridSplitter in the 2nd Column. In 1st column, I have added Grid with three Rows and place GridSplitter in the 2nd row.

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <!--Creating Sub-Grid in Column 0-->
        <Grid Grid.Column="0">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Button Content="Button1" Grid.Row="0"></Button>
            <Button Content="Button2" Grid.Row="2"></Button>
            <GridSplitter HorizontalAlignment="Stretch"
                          VerticalAlignment="Center"
                          Grid.Row="1" Height="4" Background="Green">
            </GridSplitter>
        </Grid>
        <!--Creating Sub-Grid in Column 2-->
        <Grid Grid.Column="2">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Button Content="Button3" Grid.Row="0"></Button>
            <Button Content="Button4" Grid.Row="1"></Button>
        </Grid>
        <GridSplitter HorizontalAlignment="Center"
                      VerticalAlignment="Stretch"
                      Grid.Column="1" Grid.Row="0"
                      Grid.RowSpan="3" Width="4" Background="Black">
        </GridSplitter>
</Grid>
Preview:

ShowsPreview property of GridSplitter:
When we set Show Preview property to true, a preview of change in row or column size is shown and size of row or column changes when the GridSplitter is released. On setting ShowPreview property as false, Size of row and column updates in real time on dragging the GridSplitter.

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock Text="Some Text Here.Some Text Here.Some Text Here.Some Text Here." FontSize="16" TextWrapping="Wrap" Grid.Column="0" Margin="10"></TextBlock>
        <TextBlock Text="Some Text Here.Some Text Here.Some Text Here.Some Text Here." FontSize="16" TextWrapping="Wrap" Grid.Column="2" Margin="10"></TextBlock>
        <GridSplitter HorizontalAlignment="Center" VerticalAlignment="Stretch" Width="4" Background="Gray" Grid.Column="1" ShowsPreview="True"></GridSplitter>
</Grid>
Preview:

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

Other Related Articles:

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook