If I understand you correctly, you want the X button to do something and then not close the application? You need to use the OnFormClosing event, which you can place anywhere in your form class:
Code:
protected override void OnFormClosing(FormClosingEventArgs e)
{
// put your custom code here
e.Cancel = true; // prevent form closure
base.OnFormClosing(e);
}
Here's an example, where if you click the X button it shows a dialog asking you to confirm you wish to exit:
Code:
protected override void OnFormClosing(FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want to quit?", "Exit?", MessageBoxButtons.OKCancel);
if (result != DialogResult.OK)
e.Cancel = true;
else
base.OnFormClosing(e);
}