Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Force a Form to Close

Force a Form to Close

Scheduled Pinned Locked Moved C#
testingbeta-testingxmlquestion
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MatrixCoder

    You obviously don't know C#, do you? The This keyword refers to the current instance of the class, kind of like the Me keyword in VB.


    Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

    L Offline
    L Offline
    Latheesan
    wrote on last edited by
    #6

    You obviously don't know C#, do you?

    Yea you are correct, im still learning :-O

    L M 2 Replies Last reply
    0
    • M MatrixCoder

      Did you try Application.Exit();?


      Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

      L Offline
      L Offline
      Latheesan
      wrote on last edited by
      #7

      Oh yea, i tried that too and it didn't work either. Normally Close(); alone just works fine, but in this case, it's acting quite wierd...

      1 Reply Last reply
      0
      • L Latheesan

        You obviously don't know C#, do you?

        Yea you are correct, im still learning :-O

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #8

        May I suggest you buy a book on the C# language; and read, study and understand it completely, so you know the language and some OO concepts, before you engage in creating fancy programs. That too is "separation of concerns", one of the modern software concepts. :)

        Luc Pattyn [My Articles]

        1 Reply Last reply
        0
        • L Latheesan

          Hello, I have this search method which is called when the user enter some value into textbox and presses Search button. If a record is found, it's supposed to open a form called Account Management Form. What i wanted to achieve was, Close the search form right after the record is found and Acc Management Form is being opened. this is the code i have so far: private void doEditAccount(IAccount account) { this.close(); AccountManageForm form = new AccountManageForm(account); form.ShowDialog(); } private void searchBtn1_Click(object sender, EventArgs e) { string fileName = "Account_Data.xml"; XPathDocument doc = new XPathDocument(fileName); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr; expr = nav.Compile("//Account[@ID='" + accountIDInput.Text + "']"); XPathNodeIterator iterator = nav.Select(expr); iterator = nav.Select(expr); if (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); string tmpAccID = (nav2.GetAttribute("ID", "")); nav2.MoveToFirstChild(); string tmpName = (nav2.Value); IAccount account = new Account(tmpName, ""); doEditAccount(account); } } In theory, shouldn't the search form get closed because of the line this.close(); inside the doEditAccount method and then open the Acc Management Form... but in my testing, the Adv Search Form won't close. Im i doing something wrong? or is there another way to force a form window to close before opening another one?

          L Offline
          L Offline
          Leslie Sanford
          wrote on last edited by
          #9

          Latheesan wrote:

          In theory, shouldn't the search form get closed because of the line this.close(); inside the doEditAccount method and then open the Acc Management Form...

          No. The form.ShowDialog() code will block until the AccountManageForm is closed. Once that form is closed, the doEditAccount method will run to completion, control will be returned to the original form which will then process the close message.

          L 1 Reply Last reply
          0
          • L Leslie Sanford

            Latheesan wrote:

            In theory, shouldn't the search form get closed because of the line this.close(); inside the doEditAccount method and then open the Acc Management Form...

            No. The form.ShowDialog() code will block until the AccountManageForm is closed. Once that form is closed, the doEditAccount method will run to completion, control will be returned to the original form which will then process the close message.

            L Offline
            L Offline
            Latheesan
            wrote on last edited by
            #10

            Oh i get it. Is there any way to get around this?

            L 1 Reply Last reply
            0
            • L Latheesan

              You obviously don't know C#, do you?

              Yea you are correct, im still learning :-O

              M Offline
              M Offline
              MatrixCoder
              wrote on last edited by
              #11

              Latheesan wrote:

              Yea you are correct, im still learning

              I was talking to Luc Pattyn. But if you need a good book on C#, I would suggest: Learning C# 2005.


              Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

              1 Reply Last reply
              0
              • L Latheesan

                Oh i get it. Is there any way to get around this?

                L Offline
                L Offline
                Leslie Sanford
                wrote on last edited by
                #12

                Latheesan wrote:

                Is there any way to get around this?

                Well, let's consider what you're trying to do. You have a main form in which the user can enter something they wish to search for into a text box. They hit a search button. If the item is found, a new form is opened for them to interact with. What you want to do is close the original form with the text box and search button, correct? The problem is that you want to close what is apparently the main form while keeping open a child form. I don't know of a way to do this. Why not instead use one main form and two child forms. One child form is for entering searches. Once the user has entered a search string and hit the search button, it's closed. The main form then opens the second child form, which uses the results of the search. So you have a main form representing the basic functionality of the application and two child forms, one for searching, and the other for using the results of the search. Make sense?

                L 1 Reply Last reply
                0
                • M MatrixCoder

                  You obviously don't know C#, do you? The This keyword refers to the current instance of the class, kind of like the Me keyword in VB.


                  Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

                  L Offline
                  L Offline
                  Leslie Sanford
                  wrote on last edited by
                  #13

                  MatrixCoder wrote:

                  You obviously don't know C#, do you?

                  I'm guessing that Luc knows C# and was just requesting more info on the context of the code.

                  1 Reply Last reply
                  0
                  • L Leslie Sanford

                    Latheesan wrote:

                    Is there any way to get around this?

                    Well, let's consider what you're trying to do. You have a main form in which the user can enter something they wish to search for into a text box. They hit a search button. If the item is found, a new form is opened for them to interact with. What you want to do is close the original form with the text box and search button, correct? The problem is that you want to close what is apparently the main form while keeping open a child form. I don't know of a way to do this. Why not instead use one main form and two child forms. One child form is for entering searches. Once the user has entered a search string and hit the search button, it's closed. The main form then opens the second child form, which uses the results of the search. So you have a main form representing the basic functionality of the application and two child forms, one for searching, and the other for using the results of the search. Make sense?

                    L Offline
                    L Offline
                    Latheesan
                    wrote on last edited by
                    #14

                    Yup, thanks allot for the info. The need to close the Search Form before opening the Editing Form isn't exactly in the project requirement, it's a little feature i wanted to add, because 1) im a uber perfectionist and 2) it would have looked more professional and more user friendly (I felt as if i didnt have the search form close, it'll look messy with 3 form windows open from 1 software). Thanks anyway =)

                    1 Reply Last reply
                    0
                    • L Latheesan

                      Hello, I have this search method which is called when the user enter some value into textbox and presses Search button. If a record is found, it's supposed to open a form called Account Management Form. What i wanted to achieve was, Close the search form right after the record is found and Acc Management Form is being opened. this is the code i have so far: private void doEditAccount(IAccount account) { this.close(); AccountManageForm form = new AccountManageForm(account); form.ShowDialog(); } private void searchBtn1_Click(object sender, EventArgs e) { string fileName = "Account_Data.xml"; XPathDocument doc = new XPathDocument(fileName); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr; expr = nav.Compile("//Account[@ID='" + accountIDInput.Text + "']"); XPathNodeIterator iterator = nav.Select(expr); iterator = nav.Select(expr); if (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); string tmpAccID = (nav2.GetAttribute("ID", "")); nav2.MoveToFirstChild(); string tmpName = (nav2.Value); IAccount account = new Account(tmpName, ""); doEditAccount(account); } } In theory, shouldn't the search form get closed because of the line this.close(); inside the doEditAccount method and then open the Acc Management Form... but in my testing, the Adv Search Form won't close. Im i doing something wrong? or is there another way to force a form window to close before opening another one?

                      M Offline
                      M Offline
                      Michael Sadlon
                      wrote on last edited by
                      #15

                      private void doEditAccount(IAccount account) { this.isVisible = false; AccountManageForm form = new AccountManageForm(account); form.ShowDialog(); } Later, when the AccountManageForm is done running, you can make it visible again, or just not make it visble :P It's a bit of a hack, I guess, but you won't waste resources painting the form and get your desired functionality. Hope it helps. I didn't test the code, but I'm 99% sure you can just hide the form and get what you want.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups