-->

Thursday, September 12, 2013

How to Use Toggle Button: WPF

Toggle button is a sticky button, which have two states i.e. checked and Unchecked by default. It don’t have cancel behaviours of the button. Clicking this first time will set IsChecked property to true, and second time clicking will set IsChecked property to false.

The following XAML code will show two toggle buttons with IsChecked property true and false respectively. As the checkbox button, toggle button also have IsThreeState property means it have three states i.e. checked, unchecked and indeterminate.

<ToggleButton Margin="5" Height="20" Width="100" IsChecked="True"/>
<ToggleButton Margin="5" Height="20" Width="100" IsChecked="False"/>

When we want to use the third state, then the value of IsChecked will be {x:Null} and it will also look like false value. The above code will show the window with two toggle button like following image:


To use third property we have to set IsThreeState property to true. Now first click set the IsChecked to true, second will set null and third will set to false. It have individual event for all the three states:
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
}

private void toggleButton_Unchecked(object sender, RoutedEventArgs e)
{
}

private void toggleButton_Indeterminate(object sender, RoutedEventArgs e)
{
}

How to access subDirectories of a Directory in C#

About

A directory or a folder can contains multiple directories or files. If you want to access all those directories which is under in a parent directory Then you should use DirecotryInfo class for accessing subDirectories of a parent Directory.In previous example we had seen that how to create a subDirectory under parent Directory.Now in this example we will seen that how to access all subDirectories under parent Directory.

Logic behind the program is

Step-1 : Create a instance of DirectoryInfo Class with parent directory path.
Step-2 : Now check , If parent directory is exist or not in specified path
Step-3: If Exist then get all subdirectories under parent directory and assign to an array using GetDirectories() method
Step-4 : Items add into List.

NoteGetDirectories() method does not access compressed directories

Lets take an example How to access subDirectories 

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;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dirpath = textBox1.Text;
            DirectoryInfo dirinfo = new DirectoryInfo(dirpath);
            if (dirinfo .Exists)
            {
                DirectoryInfo[] subdir = dirinfo.GetDirectories();
                foreach (DirectoryInfo  item in subdir)
                {
                    listBox1.Items.Add(item);
                }
            }
         


        }
    }
}


Output
How to access subDirectories of a Directory in C#

How to access subDirectories of a Directory in C#

Wednesday, September 11, 2013

How to access anchor tag in code file in ASP.NET

The HtmlAnchor Class

The HtmlAnchor class is used to create an anchor, <a>, element for navigation. This class can be used for the following purpose:
Navigation- You can use the Href property and provide the address of anchor website, where you want to take the user . Alternatively , you can also take the user at a different location of the same page.
Handling events : You can handle the events of this class , such as the ServerClick event, to programmatically perform the desired actions, when user interacts with the anchor <a> element.
This HtmlAnchor class gives you access to the HTML <a> tag in server code.

Event of the HtmlAnchor Class

ServerClick : Occurs when the user clicks an Anchor control in the browser, This event is handled in the server.

Example of How to access anchor tag in code file in ASP.NET


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

<!DOCTYPE html>

<script runat="server">

    protected void anchor1_ServerClick(object sender, EventArgs e)
    {
        anci.InnerHtml = " welcome to anchor tag";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <a id="anchor1" runat ="server" onserverclick ="anchor1_ServerClick">Click here</a>
        <br />
        <span id="anci" runat ="server" ></span>
    </div>
    </form>
</body>
</html>
Output
How to access anchor tag in code file in ASP.NET

How to access html form element in code file in ASP.NET

The HtmlForm Class

The HtmlForm class can be used to access the HTML <form> element in the code file. If you add any HTML control to the form using ASP.NET IDE , the web form automatically converts that control into the object of the HtmlForm class. You can also create additional forms using the HtmlForm class, if required. All server controls that postback to the server must be placed between the opening and closing tags of an HTML form. You can use the HTML Form class to get access to the HTML <form> element on the server.
This class inherits the HtmlControl class.

Lets take an example

The Html Form class can be used to access the HTML <form> elements in the code file
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Submit1_ServerClick(Object Sender, EventArgs e)
    {
        ename.InnerHtml = Text1.Value.ToString();
        fsub.InnerHtml = Text2.Value.ToString();
     
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
Enter Your name: <input id="Text1" type="text" runat ="server" /><br />
   Enter your fav. Subject: <input id="Text2" type="text" runat ="server" /><br />
        <input id="Submit1" type="submit" value="submit" runat ="server" onserverclick ="Submit1_ServerClick" />
        <br />
        Your enter name: <span id="ename" runat="server" /> <br />
        Your Fav. subject <span id="fsub" runat ="server" />

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


Output
How to access html form element in code file in ASP.NET

Tuesday, September 10, 2013

Specify Range with ProgressBar Control: WPF

Progress bar is also a range control as the slider control in our previous post. It has a different visibility to user. In windows operating system, when we copying/moving/large-running operations, then by default a progress bar is shown. This progress bar notify the status of progress, to the user.

How to use Progress bar control in WPF

To show the above progress bar below XAML code have been used:
<ProgressBar Minimum="0" Maximum="100" Value="25" Height="30" Width="300" Margin="10">
</ProgressBar>
This control also have its common properties like minimum, maximum and value as in slider control. By default minimum is zero, maximum is ten and value is zero. It have two mostly used and new properties:
  • IsIndeterminent: it is used to show the animation in progress bar control. It doesn’t need minimum, maximum and value properties. Mostly used when we don’t let the user know about the progress.
  • Orientation: It can be set to vertical, and the progress bar show the progress in vertical form as shown in the following image.
Use Progress bar control with vertical orientation: WPF

How to Specify Range with Slider Control: WPF

Range controls are used to get or set numeric values that falls within a specified range. These controls are inherited from an abstract class RangeBase, which provides basic features. Some common properties of these controls, mostly used are:
  • Minimum: get or set the minimum value of the control. By default it is zero.
  • Maximum: get or set the maximum value of the control. By default it is Ten.
  • Value: get or set the current value of the control. By default it is zero.
By using the following lines of XAML code, a slider control will place on the window:


<Slider Name="slider" Minimum="0" Maximum="100" Value="25" Height="25" Width="300">
</Slider>

Run the code and the slider control is on your screen, with value 25- defined above.

Slider control WPF

Ticks can be used to place the slider on exact position. We can enable ticks by using the property TickPlacement. It have four values i.e. None, Both, TopLeft and BottomRight.
  • None: it will give a simple look as in above image. 
  • TopLeft: the ticks will placed on left with vertical orientation and top with horizontal. 
  • BottomRight: the ticks will placed on right with vertical orientation and bottom with horizontal.
  • Both: It is used on both left and right, when the orientation is vertical.
The following image shows the slider control with TickPlacement set to TopLeft.

Slider control with TickPlacement in WPF

How to Customize Items in ComboBox: WPF

In our previous post I have discussed about the introduction of combo box control and insert some items in it. What if I want to insert an image also with a text in combo box items? So in this article, I will add some items in combo box which will have text as well as an image also.

To place an image and a text, I am using a stack panel with orientation horizontal. This stack panel will contain an image and a textblock, providing sufficient information. Check out the below code:
<ComboBox Name="customizeComboBox" Height="30" Width="200"
 IsEditable="True">
<StackPanel TextSearch.Text="Item 1" Orientation="Horizontal">
<Image Source="image1.png"></Image>
<TextBlock Text="First item with image"></TextBlock>
</StackPanel>
<StackPanel TextSearch.Text="Item 2" Orientation="Horizontal">
<Image Source="image2.png"></Image>
<TextBlock Text="Second item with image"></TextBlock>
</StackPanel>
</ComboBox>

The XAML code have simple definition of combo box which contains a name, height and width. IsEditable property will be used to do some search when user will type something in combo box. In the combo box tags I have used two stack panel (may be more), with an image and a text.

Run the code and a combobox will be shown with two items, each containing an image and a text specified above:

Customize items in combo box wpf

© Copyright 2013 Computer Programming | All Right Reserved