-->

Tuesday, August 20, 2013

ASP.NET : How to use Link Button control

The LinkButton control is another standard Web server control used to link the current Web page to some other Web page, similar to the HyperLink control. The only difference between the two is that the HyperLink control just allows the browser to navigate to a new Web Page, whereas in the LinkButton control, we can also perform some other action by handling the click and Command events of this control.

Use of the LinkButton control
  • Best use in Signin/Signout button
  • Design horizontal/vertical menu bar 
  • In bulleted list 
  • Define product category
Snapshot of the link button control
Account setting link button

SignOut Example of LinkButton

<%@ Page Language="C#" %>
<!DOCTYPE html>

<script runat="server">

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("Default.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">SignOut</asp:LinkButton>
    </div>
    </form>
</body>
</html>

Output
signout link button

Monday, August 19, 2013

Passing Values from DataGridView between Different Forms

Sometimes we need to transfer all the rows and columns of a DataGridView from one form to another. In this article I will transfer all the binded data of first form's grid view and bind them to second form's grid view.

Create two forms with dataGridView in each and a button on first form. Create a class to which your datagridview will bind like i create a student class having Name, Age and address field.
Bind the first form's gridview in C# language as
studList.Add(new Student() { Name = "Jacob", Age = 23, Address = "London" });
studList.Add(new Student() { Name = "Jaklin", Age = 25, Address = "US" });
studList.Add(new Student() { Name = "Julia", Age = 26, Address = "UK" });
dataGridView1.DataSource = studList;

In the click event of button write the following code in C# language
List<Student> tempList = dataGridView1.DataSource as List<Student>;
new Form1(tempList).ShowDialog();

And your second form's constructor have to look like the below code in C# language
public Form1(List<Student> sourceList)
{
InitializeComponent();
dataGridView1.DataSource = sourceList;
}
When we run this project and click on transfer button then both the form have same list of data e.g.

So we have passed all the rows and columns of first form's gridview to second form's gridview.

Windows Store Apps : Write simple Hello world

Getting started with windows store
Step-1: Download windows 8 sdk from Microsoft website
http://msdn.microsoft.com/en-us/library/windows/desktop/hh852363.aspx

Step-2: Select Windows store Apps from file-->New-->Project-->Visual C# -->Windows store

start screen of Windows Store Apps : Write simple Hello world

Step-3: Select "MainPage.xaml" file in solution explorer.

You can make use of the TextBlock control for displaying Text on the screen.The TextBlock control has a property called Text that you can set to "Hello World" after press button.

Step-4: Paste this code into your "MainPage.xaml" file


 

<Page
x:Class="Helloworld.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Helloworld"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button Content="Press Me" HorizontalAlignment="Left" Margin="94,76,0,0" VerticalAlignment="Top" Width="192" Height="71" Click="Button_Click_1"/>
<TextBlock x:Name="label1" HorizontalAlignment="Left" Margin="94,175,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="85" Width="211"/>

</Grid>
</Page>
Codebehind file
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;




// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
 
 

namespace Helloworld



{
 
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page


{
 
public MainPage()



{
 
this.InitializeComponent();



}
 
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)



{

}
 
private void Button_Click_1(object sender, RoutedEventArgs e)



{
 
label1.Text = "Hello world!";



}

}

}
 
 
Output
Windows Store Apps : Write simple Hello world

MessageBox Class in Windows Form

MessageBox class is used to display informative messages to users. A message box can contains text, buttons and symbols. One cannot create a new instance of this class something like other classes. To display a message you need to call show() method of this class. It is a static method and accepts text, title, buttons, icons and etc. as per desired parameters.


To show a simple message to the user:
MessageBox.Show("This is My First Message");

And it will provide a message box as in following image

Message Box with text only in windows forms
Show method have some overloads, we will use a standard overload that is mostly used in programming.

MessageBox.Show("This is My First Message","Title",
MessageBoxButtons.OKCancel,MessageBoxIcon.Information);

A message box will show like the following image.

Message Box with text, title, buttons and icons in windows forms

In the above show method the first parameter is text to be shown, second parameter is the captions or title of the box, third parameter is the messageBox buttons (here OkCancel) and fourth parameter is the icon(here information icon) to be shown. There are some more buttons and icons to be used in this method which are:

MessageBox buttons which are in enumeration MessageBoxButtons
  • AbortRetryIgnore: contains Abort, Retry and Ignore button.
  • Ok: contains ok button.
  • OkCancel: contains Ok and Cancel button.
  • RetryCancel: contains Retry and Cancel button.
  • YesNo: contain Yes and No buttons.
  • YesNoCancel: contains Yes, No and Cancel buttons.
Message box icons which are in enumeration MessageBoxIcons
  • Asterisk: symbol consisting a letter ‘l’ within a circle.
  • Error: symbol consisting a letter ‘X’ within a white circle and red background.
  • Exclamation: symbol consisting an exclamation mark within a rectangle.
  • Hand: symbol consisting a white ‘X’ within a circle.
  • Information: symbol consisting a letter ‘i’ within a circle.
  • None: contains no symbol.
  • Question: symbol consisting a question mark within a circle.
  • Stop: symbol consisting a white X within a circle.
  • Warning: symbol consisting a exclamation point within a triangle.
Here are some more overloads of Show() method.
MessageBox.Show("Text");
MessageBox.Show("Text", "Title");
MessageBox.Show("Text", "Title", MessageBoxButtons.OK);
MessageBox.Show("This is My First Message", "Title", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
We can set default button of the box, enable options of message box, enable of disable help button and etc.as per our requirements.

Status Strip Control
Error Provider Control

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

© Copyright 2013 Computer Programming | All Right Reserved