Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey everyone. I have a crazy problem that occurs after I close a form. Here's are the steps.

1. Open MainWindow.xaml
2. Open NewWindow1.xaml from MainWindow.xaml
3. Connect NewWindow1.xaml to SQL as a new customer or edit customer.
4. Close NewWindow1.xaml when done.
5. Open NewWindow1.xaml again
6. If the user presses "Retrieve" without providing a customer ID, the form returns the value from the last search...

How do I close a form and destroy all prior used values so that this doesn't occur any more?

Additional hints:
I use a double click event inside a List View to show the forms. Inside the event, I use a switch (case form 1 open form 1, case form2 open form2, etc.)


C#
<pre lang="c#">
        private void ListViewItem_PharmDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var obj = (DependencyObject) e.OriginalSource;

            while (obj != null && !Equals(obj, PharmacyLv))
            {
                if (obj.GetType() == typeof (ListViewItem))
                {
                    var Item = (ListViewItem) sender;
                    var pf = (PharmForms) Item.Content;
                    var msgTitle = "Form Request " + pf.FormNamePharm;
                    var msgCaption = "You requested to open " + pf.FormCodePharm + " - " + pf.FormNamePharm +
                                     ". \nPress OK to continue or CANCEL to select again.";
                    var result = MessageBox.Show(msgCaption, msgTitle, MessageBoxButton.OKCancel,
                        MessageBoxImage.Question);


                    switch (result)
                    {
                        case MessageBoxResult.OK:
                            switch (pf.FormCodePharm)
                            {
                                case "Form1":
                                    OpenForm1();
                                    break;
                                case "Form2":
                                    OpenForm2();
                                    break;
                                case "Form3":
                                    OpenForm3();
                                    break;
                                case "Form4":
                                    OpenForm4();
                                    break;
                               // etc...
                                case "":
                                    break;
                            }
                            break;
                        case MessageBoxResult.No:
                            break;
                    }
                    break;
                }
                obj = VisualTreeHelper.GetParent(obj);
            }
        }
</pre>
Posted
Updated 24-May-15 15:56pm
v2
Comments
RossMW 24-May-15 22:28pm    
When you close the form do you also dispose() ?
Derek Kennard 25-May-15 0:21am    
I am now... :)
I edited the XAML to use the steps in "Way 2" in this article. It helped me avoid the the user pressing the button when I dont want them too... (http://www.codeproject.com/Tips/215457/How-to-make-Button-Enable-Disable-depends-on-a-Tex)

I also implemented this articles concept as well:
https://msdn.microsoft.com/en-us/library/system.idisposable.dispose%28v=vs.110%29.aspx

If you any more information on the above IDisposable method or hints to apply to the examples, I would appreciate it. For the most part, implementing the Dispose() in this code seems to do it...
DamithSL 24-May-15 22:55pm    
can you update the question with the code of Retrieve method?
Derek Kennard 25-May-15 0:51am    
That's a ton of code, so maybe a sampling will help. I use a DataLogic project for all contact to the database.

namespace DataLogic
{
public class RetrieveFormData
{
public string a ton of variables...

public static void ReturnFormData()
{
using (
var conn =
new SqlConnection(ConfigurationManager.ConnectionStrings["Dbconn"].ConnectionString))
{
try
{
var preSql = Settings.Default.SqlRetrieveData;
var sql = preSql + "@p1 = " + PatId;
conn.Open();
using (var cmd = new SqlCommand(sql, conn))
{
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var1 = reader[0].ToString();
var2= reader[1].ToString();
etc...
}
}
}
}
catch (SqlException ex)
{
// Build custom message box for exception upload
var msg = "RetrieveFormData.ReturnFormData() Error: " + Environment.NewLine + ex;
var title = Resources.CustomMsgBxTitle;
MessageBoxManager.Ok = "Copy";
MessageBoxManager.Cancel = "Ignore";
MessageBoxManager.Register();
var result = MessageBox.Show(msg, title, MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);
switch (result)
{
case DialogResult.OK:
Clipboard.SetText(msg);
break;
case DialogResult.Cancel:
break;
}
MessageBoxManager.Unregister();
Log.Error("Exception Thrown: " + msg + " - for Patient ID: " + PatId);
}
finally
{
conn.Close();
Log.Info("Closing SQL Connection for RetrieveFormData.ReturnFormData()");
}
}
}
}
}
Sergey Alexandrovich Kryukov 25-May-15 0:28am    
Step #2 it totally unclear. What does it mean? And it makes the whole question unclear.
—SA

1 solution

Thank you RossMW, you got me in the right location.


I edited the XAML to use the steps in "Way 2" in this article. It helped me avoid the the user pressing the button when I dont want them too... (http://www.codeproject.com/Tips/215457/How-to-make-Button-Enable-Disable-depends-on-a-Tex)

I also implemented this articles concept as well:
https://msdn.microsoft.com/en-us/library/system.idisposable.dispose%28v=vs.110%29.aspx

If you any more information on the above IDisposable method or hints to apply to the examples, I would appreciate it. For the most part, implementing the Dispose() in this code seems to do it...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900