6

How can we provide a hyperlink or button to close a web page ?

Community
  • 1
  • 1
ranbir
  • 81
  • 1
  • 1
  • 2
  • 8
    tag with 'crystal ball required' – Mitch Wheat May 23 '10 at 07:55
  • 5
    this.Close() should work, if there are no problems in other places. – n535 May 23 '10 at 07:56
  • Clearly your function that used to work has changed or it's not being called anymore. Figure out which and fix it. – Noon Silk May 23 '10 at 07:58
  • What is this `close` function that you are using? There's no such function in the base class library. In C# function names are PascalCase so I guess you are referring to `Form.Close`. Am I correct? – Darin Dimitrov May 23 '10 at 08:01
  • You might want to tell us whether you're using WinForms, WebForms, WPF, SilverLight, etc. Also, your question has nothing at all to do with Visual Studio. – John Saunders Jun 24 '10 at 21:37

4 Answers4

18

Assuming you're using WinForms, as it was the first thing I did when I was starting C# you need to create an event to close this form.

Lets say you've got a button called myNewButton. If you double click it on WinForms designer you will create an event. After that you just have to use this.Close

    private void myNewButton_Click(object sender, EventArgs e) {
        this.Close();
    }

And that should be it.

The only reason for this not working is that your Event is detached from button. But it should create new event if old one is no longer attached when you double click on the button in WinForms designer.

MadBoy
  • 10,824
  • 24
  • 95
  • 156
3

To close a windows form (System.Windows.Forms.Form) when one of its button is clicked: in Visual Studio, open the form in the designer, right click on the button and open its property page, then select the field DialogResult an set it to OK or the appropriate value.

Frederic Heem
  • 574
  • 4
  • 7
2

double click the button and add write // this.close();

  private void buttonClick(object sender, EventArgs e)
{
    this.Close();
}
HMcompfreak
  • 161
  • 2
  • 3
  • 20
1
public class Form1 : Form
{
public Form1()
{
    InitializeComponents(); // or whatever that method is called :)
    this.button.Click += new RoutedEventHandler(buttonClick);
}

private void buttonClick(object sender, EventArgs e)
{
    this.Close();
}
}
Jason Miesionczek
  • 14,268
  • 17
  • 76
  • 108