Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Saturday, December 27, 2008

How to: Parse dates (ASP.NET 2.0)

In ASP.NET 1.x, you would need to warp a try/catch around the DateTime.Parse method to try and parse a string into a DateTime object.
In ASP.NET 2.0, you can use the DateTime.TryParse method to parse your date strings.

DateTime dt;

bool isSuccessful = DateTime.TryParse("05/28/2007",out dt);

If the TryParse was not successful, then it would return false. Also the DateTime returning from the output parameterwil be set to DateTime.MinValue.
Make sure you have the "out" keyword at the beginning of the second parameter, since this parmeter is an output parameter.

Saturday, November 29, 2008

Using enum in .net

Using a Enum in C#.net
Code Sample
// declaring a enum
enum Operations
{
add,sub,mul,div
};


public void mymethod()
{
// creating object of enum
Operations op ;

// assinging value to enum
op = Operations.add ;

// using it to compare
if(op== Operations.add)
{
// do somethng.. addition
}

}

Wednesday, November 26, 2008

Calling a JavaScript function from ASP.NET

Calling a JavaScript funtion from ASP.NET
Requirement :
Calling a javascript function which return strings and binding it to textbox of asp.net page
For e.g . You have a javascript function say
MyJavaScript() whichs return string, on click of button you need to call javascript function and
bind the returned value to textbox.

Code Snippet
function MyJavaScript()
{
return 'Text from JavaScript';
}

asp:TextBox ID="mytxtBox" runat="server"

asp:btn ID="myButton" runat="server"
Text="Click to Get Value"
OnClientClick ="document।getElementById('mytxtBox').value= MyJavaScript()"
/asp:btn
Output

Monday, November 24, 2008

Animating a Windows Form

Requirment

To animate a windows form while displaying it to user , just like a powerpoint slides effects.

Solution

To get such powerpoint slides effect can be achieved using Win32 API . the only problem here is that its unmanged code.

Note : sample code below is in C#

1) Add a reference to you class file
using System.Runtime.InteropServices;

2) import user32 dll with following syntax
[DllImport("User32.dll")]
static extern bool AnimateWindow(System.IntPtr handle, int time, int value);

3) call AnimateWindow function at windows form load event


Arguments
1) Handler(this. handle) of form(to be animated)
2) time(in milliseconds) to specify how much time animation will take (faster or slower)
3) values(in hexadecimal ) which specify how the form will be displayed (left to right, right to left, top to bottom, bottom to top or center etc..)


The last values are or'd together and can be any of the following:

&O1 = Animate the window from left to right. This flag can be used with roll or slid eanimation It is ignored when used with the &H10 flag.

&H2 = Animate the window from right to left. This flag can be used with roll or slide animation. It is ignored when used with the &H10 flag.

&H4 = Animate the window from top to bottom. This flag can be used with roll or slide animation. It is ignored when used with the &H10flag.

&H8 = Animate the window from bottom to top. This flag can be used with roll or slide animation. It is ignored when used with the &H10flag.

&H10 = Makes the window appear to collapse inward if the &H10000 flagis used or expand outward if the &H10000 flag is not used.

&H10000 = Hides the window. By default, the window is shown.&H20000 = Activates the window. Do not use this flag with &H10000.

&H40000 = Uses slide animation. By default, roll animation is used. This flag is ignored when used with the &H10 flag.

&H80000 = Uses a fade effect. This flag can be used only if hwnd is a top-level window.