How to do print functionality in Silverlight ?



Hi

Silverlight4.0 support Printing Api, using this api we can do the print functionality in silverlight 4.0 like this
Step1: Create the XAML page design like this

<StackPanel>
<Image x:Name="imgOne" Source="Img/Winter.jpg"
Height="300" Width="500"/>

<Button x:Name="Print" Content="Print" Width="150" Height="40"
Click="Print_Click" />
</StackPanel>

Step2:Write the code on Click Event in Code behind page like this
and make ensure to use “System.Windows.Printing” namespace.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Printing;

namespace PrintTest
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

private void Print_Click(object sender, RoutedEventArgs e)
{
PrintDocument pdoc = new PrintDocument();
pdoc.PrintPage += (p, args) =>
{
args.PageVisual = imgOne;
args.HasMorePages = false;

};

pdoc.EndPrint += (p, args) =>
{
MessageBox.Show("Printing operation completed");
};

pdoc.Print("Some Document");

}
}
}

How to use delegate in asp.net ?


Delegate is similar to “C” language function pointer, where functions can be assigned like a variable and called at the run time based on dynamic conditions
Or
Delegate is the type that defines a method signature. To invoke a delegate object, one or more methods are required with the EXACT same signature. The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.

Uses of Delegate:
1. If we have to fire or trigger some action in our project on some particular situation like click event, then we can use delegate
2. If we have to access the main page or other usercontrol from usercontrol then we can use delegate in asp.net.

General Syntax

public delegate int Calculate(int x, int y);

Simple Example of delegate Concept with asp.net

Step1:Design the default page like above Img

Step2: Create one class i.e Class1 like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>
///

public delegate int Calculate(int x, int y);
public class Class1
{
public int Add(int x, int y)
{
return x + y;
}
}

Step3:Write the code on Click Event like this

protected void BtnSubmit_Click(object sender, EventArgs e)
{

Class1 mc = new Class1();
Calculate opAdd = new Calculate(mc.Add);
int res = opAdd(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
lblResult.Text = res.ToString();

}

Delegate concept with UserControl

Step1: Create one UserControl and Keep one asp.net Button Control like this

<asp:Button ID="btnTest" runat="server" Text="Click Here" OnClick="btnTest_Click" />

Step2:Write the code in Codebehind like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebUserControl : System.Web.UI.UserControl
{

// Delegate declaration
public delegate void OnButtonClick(string strValue);

// Event declaration
public event OnButtonClick btnHandler;

// Page load
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnTest_Click(object sender, EventArgs e)
{
// Check if event is null
if (btnHandler != null)
{
btnHandler(string.Empty);
}

}
}

Step3:Keep on dropdown for countrylist and label for displaying selected country and also keep the usercontrol which contain the Button like this code

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem>Nepal</asp:ListItem>
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>Singapur</asp:ListItem>
</asp:DropDownList>

<br />
<br />
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<br />
<br />

Selected Country is: <asp:Label ID="lblText" Text=""
runat="server"/>

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

Step4:Write the code in code behind page like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Private : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

WebUserControl1.btnHandler+=new WebUserControl.OnButtonClick(WebUserControl1_btnHandler);

}

void WebUserControl1_btnHandler(string strValue)
{
lblText.Text = ddlCountry.SelectedItem.Text;
}
}

Step5: Now click on button which is in usercontrol,then the selected country will display on label like this

ShortCut Keys in Visual studio 2010



Hi
Yesterday night, i was searching ShortCut Keys in Visual studio 2010 then i got very nice URL. You can download the pdf sheet from this URL
Download URL

How to create modal popup in silverlight ?


Hi
In Silverlight 2.0, creating modal popup was some tedious work. But in Silverlight 3.0,4.0,5.0, we can create very easily like this.

Step1: Create the new silverlight project

step2:Right clik on Solution Exp of silverlight project and add one Silverlight Child Window like this Img

Step3: Keep one Img in ChildWindow.xaml page like this

<controls:ChildWindow x:Class=”ChildWindow.ChildWindow2″

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;

xmlns:controls=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls”

Width=”400″ Height=”300″

Title=”ChildWindow2″>

<Grid x:Name=”LayoutRoot” Margin=”2″>

<Grid.RowDefinitions>

<RowDefinition />

<RowDefinition Height=”Auto” />

</Grid.RowDefinitions>

<StackPanel>

<Image x:Name=”img” />

<TextBlock x:Name=”caption” TextWrapping=”Wrap” />

</StackPanel>

<Button x:Name=”CancelButton” Content=”Cancel” Click=”CancelButton_Click” Width=”75″ Height=”23″ HorizontalAlignment=”Right” Margin=”0,12,0,0″ Grid.Row=”1″ />

<Button x:Name=”OKButton” Content=”OK” Click=”OKButton_Click” Width=”75″ Height=”23″ HorizontalAlignment=”Right” Margin=”0,12,79,0″ Grid.Row=”1″ />

</Grid>

</controls:ChildWindow>

Step4:Create one button in main page like this

<Grid>

<Button x:Name=”Button1″ Content=”Click Here” Click=”Button1_Click” Height=”28″ Width=”180″></Button>

</Grid>

Step5: Create the code in code behind page mainpage.xaml.cs like this

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Navigation;

using System.Windows.Media.Imaging;

namespace ChildWindow

{

public partial class Test1 : Page

{

public Test1()

{

InitializeComponent();

}

private void Button1_Click(object sender, RoutedEventArgs e)

{

ChildWindow2 win = new ChildWindow2();

win.img.Source = new BitmapImage(new Uri(“Img/Winter.jpg”, UriKind.Relative));

win.Title = “Silverlight popup”;

win.Show();

}

}

}

How to do bookmark functionality in asp.net ?



Hi
While working on asp.net project, we will get requirement to bookmark functionality, we can do like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">

function addToBookMarks() {
bookmarkurl = "http://www.chandradev819.wordpress.com&quot;;
bookmarktitle = "Chandradev Blog";
if (document.all)

window.external.AddFavorite(bookmarkurl, bookmarktitle);
else if (window.sidebar) window.sidebar.addPanel(bookmarktitle, bookmarkurl, ”);

}

</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Click here" OnClientClick="addToBookMarks()" />
</div>
</form>
</body>
</html>

How to create Marquee functionality in Silverlight ?


Hi
We can create the very nice rotating text or image functionality with the help of animation in silverlight.

Storyboard is used to manage the timeline of animation.Storyboard can be used to group multiple animation.It has the ability to control the playback of animation.

The complete syntax for Marquee in silverlight is

<Canvas>
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded" >
<BeginStoryboard>
<Storyboard x:Name="animation" Storyboard.TargetProperty="(Canvas.Left)" RepeatBehavior="Forever" >

<DoubleAnimation Storyboard.TargetName="txtBlock1" From="0" To="520" Duration="0:0:10" />

</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>

<TextBlock x:Name="txtBlock1" FontFamily="Verdana" FontSize="36" Foreground="Blue" Text="This is the sample text" />

</Canvas>

Type of Panel in Silverlight ?


Hi
If you are planning to work on silverlight, then you have to be perfect in panel of silverlight. Using panel we can design any page in very easy ways.

In silverlight,generally there are the following panel

1.Stack Panel
2.Grid Panel
3.Canavas Panel
4.ScrollViewer
5.Border

Stack Panel
If our requirement is to display the control in horizontal or vertical in stack format, then we should have to go for stack panel.This is one of the easiest method to do alignment in silverlight

<StackPanel Orientation="Vertical" Background="CornflowerBlue">
<TextBlock Text="This is the sample of stack panel"></TextBlock>

<Button Content="Button1" Width="200"></Button>
<Button Content="Button2" Width="200"></Button>
<Button Content="Button3" Width="200"></Button>
<Button Content="Button4" Width="200"></Button>

</StackPanel>

Grid Panel

This is one of the most famous and most powerful layout container in silverlight. This is similar to table in asp.net

If our requirement to arrange the element on tabular or Grid format on basis of row and column style, then we should have to use this panel

<Grid x:Name="LayoutRoot" ShowGridLines="True" Background="Azure">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>

</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>

<Button Grid.Row="0" Grid.Column="0" Content="Top Left(0,0)"></Button>
<Button Grid.Row="0" Grid.Column="1" Content="Middle left(0,1)"></Button>
<Button Grid.Row="1" Grid.Column="2" Content="Button Right(1,2)"></Button>
<Button Grid.Row="1" Grid.Column="1" Content="Botton Middle(1,1)"></Button>

</Grid>

Canavas Panel

This is also one of the easiest way to arrange the layout on basis of left and top coordinate.It is also very flexible to use in silverlight design.

<Canvas Background="LightBlue" Height="600" Width="500">

<Button Canvas.Left="10" Canvas.Top="10" Content="Button1 (10,10)" ></Button>
<Button Canvas.Left="120" Canvas.Top="30" Content="Button2 (120,30)"></Button>
<Button Canvas.Left="60" Canvas.Top="80" Content="Button3 (60,80)"></Button>
<Button Canvas.Left="70" Canvas.Top="120" Content="Button4 (70,120)"></Button>

</Canvas>

Scrollviewer

This panel is generally used to group the collection of controls in scrolling format like this image

<ScrollViewer Height="200" Width="150">

<StackPanel Orientation="Vertical" Background="SkyBlue" Width="100">

<Button Content="Button1" Height="40" Width="100"></Button>
<Button Content="Button2" Height="40" Width="100"></Button>
<Button Content="Button3" Height="40" Width="100"></Button>
<Button Content="Button4" Height="40" Width="100"></Button>
<Button Content="Button4" Height="40" Width="100"></Button>
<Button Content="Button6" Height="40" Width="100"></Button>
<Button Content="Button7" Height="40" Width="100"></Button>
<Button Content="Button8" Height="40" Width="100"></Button>
<Button Content="Button9" Height="40" Width="100"></Button>

</StackPanel>

</ScrollViewer>

Border Panel
This is also one panel in silverlight. But it support only one child as content. It doest not support multiple content on itself like other panel.It is generally used for giving border layout on some content.

<Border Margin="25" Padding="8" Background="LightYellow" BorderBrush="SteelBlue" BorderThickness="8" CornerRadius="3">
<Button Margin="3" Content="Click Here" ></Button>

</Border>