CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2002
    Posts
    1,417

    button event handler

    I'm using Visual Studio 2005 Pro, and trying my hand at learning C#. I've been writing C and C++ code for quite a few years.

    Problem #1: I put a button on a form that is supposed to exit the application. In C++ MFC I would just call CDialog::OnOK() event. Is there an equivalent in C#? If yes, what is it?

    Problem #2: what books do you recommend for beginners moving from c++ to C#, or for C# windows programming in general? Any good links other than amazon.com?

    Thanks

  2. #2
    Join Date
    Apr 2005
    Posts
    576

    Re: button event handler

    #1: Application.Exit() will gracefully exit all message pumps, or Close() on "main" form will work too if that is the only form receiving events.

  3. #3
    Join Date
    Nov 2006
    Location
    North Bend, WA
    Posts
    487

    Re: button event handler

    The parts of "Windows Forms 2.0 Programming " by Sells and Weinhardt that I've read have been very useful. I haven't bought the book yet, but I probably will.

  4. #4
    Join Date
    Jun 2002
    Posts
    1,417

    Re: button event handler

    thank you both for timely responses.

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: button event handler

    the button you place on your form has a Click event. handle that event and add the click logic there. Each form has a DialogResult, and even when you assign the form's OK button to the one you've placed on the form, you must explicitly assign the forms' DialogResult property to OK inside your OK button's click event. Cancel will be set automatically when you click the cancel button (once you've set the forms cancel button on the form) but ok does not have the same behavior.

    no recomendations on books.

    also Application.Exit kills the app. it has the same effect as killing the application from process explorer. calling the Close method on the main form will cause the application to exit in a normal fashion.

  6. #6
    Join Date
    Apr 2005
    Posts
    576

    Re: button event handler

    Yep, MadHatter is right, when Close() is called on a Form events Form.Closed and Form.Closing will be fired, but not on Application.Exit().

    Application.Exit() will stop all message loops and close all windows, but not stop the application. Instead it will cause the Application.Run() method to terminate, so after that (typically in your main), your program may do other things - but in most cases it will not.

  7. #7
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: button event handler

    actually I think in 2.0 they added a Form.FormClosing or Form.FormClosed or something like that that event that is supposed to fire on Application.Exit().

  8. #8
    Join Date
    Oct 2006
    Location
    Timisoara, Romania
    Posts
    123

    Re: button event handler

    I recommend Professional C# 2005 which I think is a great book.

    http://www.amazon.com/Professional-2.../dp/0764575341

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured