19 June 2009

php code inside HTML tag

The following code creates list with the elements of an array. The blue colored code segments deserves attention.

<html>
<body>
<?php
$arr = array("Sim","Sum","Shis");
$count = 0;
?>

<ul>
<?php while($count < count($arr)) :?>
<li> <?php echo $arr[$count]; $count++; ?> </li>
<?php endwhile;?>
</ul>

</body>
<html>

15 June 2009

How to disable close button of form

In a Form, you can enable/disable miximize/minimize button control easily by FormObject.MaximizeButton or FormObject.MinimizeButton property. But unfortunately, there is no such property for close button. You can disable the close button by overriding a property in your form class like below.

private const int CP_NOCLOSE_BUTTON = 0x200;

protected override CreateParams CreateParams

{

get

{

CreateParams myCp = base.CreateParams;

myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;

return myCp;

}

}

Note: This procedure works for disabling the close button at the time of form loading, not after loading the form.

14 June 2009

How to create virtual directory in Apache using Linux [Ubuntu]

Assuming that you have already installed apache2, you have to follow the steps below.

1. Open the /etc/apache2/conf.d/security file for edit.

sudo gedit /etc/apache2/conf.d/security

2. Edit the security file. Say we want to create virtual directory named php with the folder /media/Documents/Works/PHP. Then we have to add the following lines in the security file

Alias /php /media/Documents/Works/PHP

<Directory /media/Documents/Works/PHP/>

DirectoryIndex index.php

AllowOverride None

Order Allow,Deny

allow from all

</Directory>

3. Restart apache

sudo /etc/init.d/apache2 reload


Note: In Linux Apache, web directory is case sensitive (which is not the case in windows IIS)

10 June 2009

Customized Event and Event Handler

Every .NET developer is familiar with Event and EventHandler. ‘Click’ event of a button is the most common event. Let us take a look at their usage.

Say, we have a form containing a button named ‘button1’. Then we can add a click handler with following code inside the form.


private void button1_Click(object sender, EventArgs e)

{

Console.WriteLine("button1 has been Clicked");

}

Having a careful look at the code, we find that we have to add an event handler to button object’s Click event. The event handler takes two argument sender and event argument. The event argument usually contains information related with the event.

Now, let us come to our customized event and event handler. Say, there is a person who can change his/her phone no. We want to add an event to be fired when the the person changes his/her phone no. For this, we have to do the following tasks-

1. Define our customized event arguments PhoneNoChangedEventArgs (2 attributes- OldPhoneNo & NewPhoneNo)

2. Declare prototype of event handler (2 parameters- sender and arguments)

3. Define a class Person (2 attributes- Name & PhoneNo)

4. Declare the exact name of the event- PhoneNoChanged

5. Fire the event (if any handler is added with the event) when the event actually occurs


The code is given below following exactly the upper steps. Comments is there for better understanding.


using System;

namespace CustomizedEventHandler

{

// step 1. Define our customized event arguments PhoneNoChangedEventArgs (2 attributes- OldPhoneNo & NewPhoneNo)

public class PhoneNoChangedEventArgs : EventArgs

{

public readonly string OldPhoneNo;

public readonly string NewPhoneNo;

public PhoneNoChangedEventArgs(string oldPhoneNo, string newPhoneNo)

{

this.OldPhoneNo = oldPhoneNo;

this.NewPhoneNo = newPhoneNo;

}

}

// step 2. Declare prototype of event handler (2 parameters- sender and arguments)

public delegate void PhoneNoChangedEventHandler(object sender, PhoneNoChangedEventArgs e);

// step 3. Define a class Person (2 attributes- Name & PhoneNo)

public class Person

{

private string _name;

private string _phoneNo;

// step 4. Declare the exact name of the event- PhoneNoChanged

public event PhoneNoChangedEventHandler PhoneNoChanged;

public Person(string name, string phoneNo)

{

_name = name;

_phoneNo = phoneNo;

}

public string Name

{

get { return _name; }

}

public string PhoneNo

{

get { return _phoneNo; }

set

{

//step 5. Fire the event (if any handler is added with the event) when the event actually occurs

if (_name != value) // if phone no changed

{

string previousPhoneNo = _phoneNo; ;

_phoneNo = value;

PhoneNoChangedEventArgs evArgs = new PhoneNoChangedEventArgs(previousPhoneNo, _phoneNo);

firePhoneNoChangedEvent((object)this, evArgs);

}

}

}

private void firePhoneNoChangedEvent(object sender, PhoneNoChangedEventArgs e)

{

if (PhoneNoChanged != null) // if some hadler is added with the event

PhoneNoChanged(sender, e);

}

}

}


The sample code to test the above procedure:


static void Main()

{

// create a person object with PhoneNoChangedEventHandler

Person p = new Person("Alor Chhota", "01911515020");

p.PhoneNoChanged += new PhoneNoChangedEventHandler(p_PhoneNoChanged);

// change phone no

p.PhoneNo = "01730012311";

}

static void p_PhoneNoChanged(object o, PhoneNoChangedEventArgs e)

{

Console.WriteLine("Phone no. changed from " + e.OldPhoneNo + " to " + e.NewPhoneNo);

}

 

© 2007 t!ps n tr!cks: June 2009



Template unik dari rohman


---[[ Skip to top ]]---