Force a Form to Close
-
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? -
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? -
Correct me if im wrong, but isnt "this." used to select the current opened/active form? Even if i don't use this.Close(); i tried the following options to form the form to close: Close(); AdvSearchForm.ActiveForm.Close(); Still no luck, this is what i could think of top of my head and from my past exprience with form closing...
-
You obviously don't know C#, do you? The
This
keyword refers to the current instance of the class, kind of like theMe
keyword in VB.
Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.
-
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?Did you try
Application.Exit();
?
Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.
-
You obviously don't know C#, do you? The
This
keyword refers to the current instance of the class, kind of like theMe
keyword in VB.
Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.
-
Did you try
Application.Exit();
?
Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.
-
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]
-
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?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 theAccountManageForm
is closed. Once that form is closed, thedoEditAccount
method will run to completion, control will be returned to the original form which will then process the close message. -
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 theAccountManageForm
is closed. Once that form is closed, thedoEditAccount
method will run to completion, control will be returned to the original form which will then process the close message. -
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.
-
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?
-
You obviously don't know C#, do you? The
This
keyword refers to the current instance of the class, kind of like theMe
keyword in VB.
Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.
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.
-
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?
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 =)
-
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?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.