-->

Sunday, August 18, 2013

Restrict users from Entering in TextBox

Masked textbox supports a declarative syntax for accepting or rejecting user input. Masked textbox may be used to specify input characters, input position in the mask, mask literals and some more operations can also be done using masked textbox without any custom validation logic.

Textbox is used to allow the user to input text information to be used by the programmer. To use our custom logic we can restrict numbers/characters to be inputted by the user. We can restrict any key to be inputted by the user.

Textbox’s Key Press event occurs when the control has focus and user press and releases a key. It has following format that is in C# language:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
}

The parameter e provides the data for this event which is of type KeyPressEventArgs. Char class has some in-built functions for checking the pressed and released key by user.

char.IsDigit(): whether the character is decimal digit or not.
char.IsLetter(): whether the character is a letter.
Char.IsLetterOrDigit(): whether the character is letter or digit.

There are many more method to check each type of data, we can read more here.
To restrict the user from entering the digit in textbox
if (char.IsDigit(e.KeyChar))
e.Handled = true;

Here e.Handled will tell the debugger that I have handled this event. We can use desired method to prevent specified type of key by the user.

To restrict both the letter and digit to be inputted by the user in c# language:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsLetterOrDigit(e.KeyChar) || char.IsDigit(e.KeyChar))
e.Handled = true;
}

Now when we run this form and try to enter anything between a-z, A-Z and 0-9, we can’t enter anything among these values.

ASP.NET : how to Add control dynamically

The PlaceHolder Control
The PlaceHolder control is used as a container to store server controls that are added to the Web page at runtime. The PlaceHolder control does not produce any visible output and is used only as a container for other controls on the Web page.

Example of PlaceHolder control

 
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox text = new TextBox();
        text.Text = "This is the runtime textbox";
        text.Style["width"] = "300px";
        PlaceHolder1.Controls.Add(text);       
           
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Click to add TextBox" OnClick="Button1_Click" />
        <br />
        <br />
        <br/>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>
Output
Add Textbox on runtime

Saturday, August 17, 2013

WPF: Alignments of Elements

In earlier post we have learnt about size-related properties i.e. Height & Width and Margin & Padding. In this article we will focus on alignments of elements. How a children should be positioned within a parent’s allocated space, is described by Alignment properties of an element.

There are two alignments i.e. Horizontal and Vertical and each one has its individual four values to be assigned.

Horizontal Alignment—Left, Center, Right and Stretch
Vertical Alignment—Top, Center, Bottom and Stretch

Each of these alignments have its own layout which is described below:

Left—Children are aligned to the left of parent’s allocated space.
Right—Children are aligned to the right of parent’s allocated space.
Top—Children are aligned to the top of parent’s allocated space.
Bottom—Children are aligned to the bottom of parent’s allocated space.
Center—Children are aligned to the center of parent’s allocated space.
Stretch—Children are stretched to fill the parent’s allocated space. Default value for alignments.

Place some buttons on a window and use above properties one by one and look out the effects of these alignments.
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Button HorizontalAlignment="Left">Button 1</Button>
<Button HorizontalAlignment="Center">Button 2</Button>
<Button HorizontalAlignment="Right">Button 3</Button>
<Button HorizontalAlignment="Stretch">Button 3</Button>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Button VerticalAlignment="Top" >Button 1</Button>
<Button VerticalAlignment="Center" >Button 2</Button>
<Button VerticalAlignment="Bottom" >Button 3</Button>
<Button VerticalAlignment="Stretch">Button 3</Button>
</StackPanel>
</Grid>
The preceding code yields a layout similar to the following screenshot. Positioning effects of both the alignments are visible.
Element's Horizontal and Vertical Alignment in WPF

Margin and Padding of Elements: WPF

In our previous post we have set Height and Width properties of an element, now we will set the next size related properties i.e. Margin and Padding. Margin controls how much extra space gets placed around the outside edges of the element, whereas padding controls around the inside edges of the element.

Margin and Padding can be assign in following simple ways:
  • Margin = "10", same margin of all four sides.
  • Margin = "10,5", 10 for left & right, 5 for top & bottom.
  • Margin = "10,5,10,5", for left, top, right and bottom respectively.
Same can be used for padding. Here is xaml code with three buttons having some margins.
<StackPanel Orientation="Horizontal">
<Button Margin="10">Button 1</Button>
<Button Margin="20,5">Button 2</Button>
<Button Margin="20,15,20,15">Button 3</Button>
</StackPanel>
As padding controls around the inside edges of the element, I have used a border across each button:
<StackPanel Orientation="Horizontal" Height="60">
<Border Background="Aqua" BorderThickness="2">
<Button Padding="10">Button 1</Button>
</Border>
<Border Background="Aqua">
<Button Padding="10,15">Button 2</Button>
</Border>
<Border Background="Aqua">
<Button Padding="20,15,20,15">Button 3</Button>
</Border>
</StackPanel>
The effect of padding will look like in the following screenshot:

Margin and Padding of Elements in WPF

Varying the values of these margins and paddings, we can measure the changes. If one want to use all these margins through code behind then we have to use Thickness class like the below code:
Button button1 = new Button();
button1.Margin = new Thickness(10);
button1.Margin = new Thickness(20, 15, 20, 15);

As we can see that either we can use a single value or all the four values to use margin property of an element. There is no option to use the second type i.e. the one for left & right and one for top & bottom.

Alignments and Height & Width

ASP.NET: How to use Generic Handlers

Example of Generic Handlers
Step-1 : Add new "GenericHandlers.ashx" file in your project
 

<%@ WebHandler Language="C#" Class="Handler" %>

using System;

using System.Web;

public class Handler : IHttpHandler {



public void ProcessRequest (HttpContext context) {

context.Response.ContentType = "html";



context.Response.Write("Hello World");


manaeform(context);





}
public void manaeform(HttpContext context)


{
context.Response.Write("<html><body><form>");

context.Response.Write("<h2>Select your feature</h2>");

if (context .Request .Params ["Feature"]==null)


{
context.Response.Write("<select name='Feature'>");

context.Response.Write("<option>asp.net </option>");

context.Response.Write("<option>java </option>");
context.Response.Write("</select></form></body></html>");


}

}


public bool IsReusable {

get {

return false;


}

}

Output
ASP.NET: How to use Generic Handlers

Friday, August 16, 2013

How to set Height and Width of Elements: WPF

Panels, also called parent elements, supports multiple child elements to be arranged in it. Parents decide about where to render and how much space the children get. WPF elements tend to size to their content that is to be done through its SIzeToContent property.

Height & Width:

There are several properties which are used for layouts of an element, some are size related and some are position related. Size related properties includes height, width, margin and padding. Position related properties includes horizontal & vertical alignment.

All the elements have height and width properties and also they have MinHeight, MinWidth, MaxHeight, MaxWidth to specify the values. Height/Width property is used to get or set the suggested height/width of the respective element.

We can create a textblock with height(100) and width(200) using the below xaml code.
<TextBlock Text="Hello World" Height="100" Width="200" Background="Black"/>
The above code may be written like the below code as we have discussed in our “earlier post”.
<TextBlock>
<TextBlock.Text>Hello World</TextBlock.Text>
<TextBlock.Height>100</TextBlock.Height>
<TextBlock.Width>200</TextBlock.Width>
<TextBlock.Background>Black</TextBlock.Background>
</TextBlock>
The textblock looks like as the following screenshot:

Height and Width property of an element in WPF

Element also contains some more size-related properties like DesiredSize, RenderSize, ActualHeight and ActualWidth. RenderSize represents the final size of an element, and ActualWidth and ActualHeight are exactly same as the final width and final height of an element. DesiredSize is used when IsMeasureValid property is true for an element.

Alignments and Margin & Padding

Thursday, August 15, 2013

ASP.NET : Change label color dynamically

Introduction 
The Label control is used to display the text that the user cannot edit. The label control exists within the System.Web.UI.WebControls namespace.

Public properties of the Label class
AssociatedControlID : Obtains or sets the identifier for a server control the Label control is associated with.

Text : Obtains or sets the text content of the Label control.
BackColor : Obtains or sets the back color of the Label.

Related Top Article
 How to bind label control in asp.net

Example of Change label color dynamically




<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = " Welcome to dotprogramming.blogspot.com";
        Label1.BackColor = System.Drawing.Color.Green;
        Label1.ForeColor = System.Drawing.Color.Orange;
     
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:Label ID="Label1" runat="server"></asp:Label>
 
    </div>
    </form>
</body>
</html>

Output


ASP.NET : Change label color dynamically

© Copyright 2013 Computer Programming | All Right Reserved