Skip to main content

WPF Main and Child Window Management


In our WPF application we call any child window in our event , example as button click event or other click event. When we can a child window from our main window normally it load our main window.  But if we click on that event again it will load again in main window. 
It’s really a big problem for our WPF application. Now we will manage our child window in this scenario that if the child window load once it will not load newly again on it’s event. If we minimize it then it will appear in main window bottom as Task bar. If we click on it’s event it will load from minimize. And only the main window will load on our computer Taskbar . Their will no child window loaded on Taskbar.
I have create a method name showWindow. Let see how the method working:
This foreach loop find our all current UI from namespace using split if it find out the desire window it just load it on main window.
 
 foreach (Window objWindow in Application.Current.Windows)  
         {  
           string[] splitedNamespace = (objWindow.ToString()).Split('.');  
           string aClassNameFromCollection = splitedNamespace[splitedNamespace.Length - 1];  
           if (aClassNameFromCollection == className)  
           {  
             isOpen = true;  
             objWindowName = objWindow;  
             break;  
           }  
         }  



If the window is already open it will set the window state normal.
 
 if (isOpen)  
         {  
           foreach (Window objWindow in Application.Current.Windows)  
           {  
             string[] splitedNamespace = (objWindow.ToString()).Split('.');  
             string aClassNameFromCollection = splitedNamespace[splitedNamespace.Length - 1];  
             if (aClassNameFromCollection == className)  
             {  
               objWindowName.WindowState = WindowState.Normal;  
               objWindowName.Activate();  
               break;  
             }  
           }  
         }  



If the window is not open it will load your desire window. When the case will match with your window name it will load and break the statement.

 if (isOpen == false)  
         {  
           #region SHOW DESIRED WINDOW  
           switch (className)  
           {  
             case "EmployeeSetupUI":  
               EmployeeSetupUI employeeInfo = new EmployeeSetupUI();  
               employeeInfo.Owner = this;  
               employeeInfo.Show();  
               break;  
             case "CalenderSetupUI":  
               CalenderSetupUI calendarSetup = new CalenderSetupUI();  
               calendarSetup.Owner = this;  
               calendarSetup.Show();  
               break;  
           }  
           #endregion SHOW DESIRED WINDOW  
         }  


Now consider the window state: Normally if we minimize the main window the child window can’t minimize and similarly maximize. We will manage this scenario on main window state change event.
For minimize the main window find out all open child window state using for loop and for maximize it set all child window state with foreach.

 if (WindowState.Minimized == this.WindowState)  
         {  
           int numberOfChildWindow = this.OwnedWindows.Count;  
           childWindows = new Window[numberOfChildWindow];  
           for (int count = 0; count < this.OwnedWindows.Count; count++)  
           {  
             childWindows[count] = this.OwnedWindows[count];  
           }  
         }  
         else if ((WindowState.Maximized == WindowState) || (System.Windows.WindowState.Normal == WindowState))  
         {  
           if (childWindows != null)  
           {  
             foreach (Window aChildWindow in childWindows)  
             {  
               aChildWindow.WindowState = WindowState.Normal;  
               aChildWindow.Show();  
             }  
           }  
         }  


Note: For all child window we have to set ShowInTaskbar property false. Bcoz it will show in our main window Taskbar. 


And on every child window closing event we have set this.ShowInTaskbar = true and this.Owner = null to break down all Owner  with our main window.

so our final main window and child window will look like




Comments

Popular posts from this blog

C# run powershell script as administrator

Recently I was fetching a problem that I need to run a PowerShell script that will change TFS user Display name and SID. I was trying to run that script from C# that was not working due to TFS security update and TLS certificate. Using this code block I resolve the Issue. var newProcessInfo = new System.Diagnostics.ProcessStartInfo(); newProcessInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"; newProcessInfo.Verb = "runas"; // Define Run as administrator newProcessInfo.Arguments = script; //Define your powershell script newProcessInfo.UseShellExecute = false; newProcessInfo.RedirectStandardOutput = true; // This will enable to read Powershell run output newProcessInfo.RedirectStandardError = true; Process proces = System.Diagnostics.Process.Start(newProcessInfo); proces.WaitForExit(); // I want to read the output string from powershell window StringBuilder output = new StringBuilder(); output.Append("Started"); while (!proces.St

ASP.NET MVC razor SAP Crystal report

Crete a new project: Add a aspx Master Page Create a new folder Reports and 2 sub folder crystal & crystalviewer Now add a web form page in crystalviewer  folder. Add the master page namespace in your web form page. MasterPageFile ="~/Views/Shared/ReportSite.Master" Replace your web form by this code < asp : Content ID ="Content1" ContentPlaceHolderID ="ContentPlaceHolder1" runat ="server">      </ asp : Content > Now go to design mode of your web form drag & drop the crystal report viewer in your web form. After that your page will be look look like this. Replace the code: < CR : CrystalReportViewer ID ="EmployeeList" runat ="server"   HasCrystalLogo ="False"     AutoDataBind ="True"   Height ="50px"   EnableParameterPrompt ="false" EnableDatabaseLogonPrompt

mvc razor textboxfor change event change another textboxfor value

Based on value of Weight, Rate , CNF & AWB it will change the value of Freight , TTLCNF anfd TTLFright . Freight= Weight*Rate; TTLCNF  = Weight*CNF; TTLFright=  Freight+ TTLCNF  + AWB; @Html.TextBoxFor(model => model.Weight, new { onChange="return GetWight(this);"}) @Html.TextBoxFor(model => model.Rate, new { onChange="return GetWight(this);"})/Kg @Html.TextBoxFor(model => model.Freight, new {disabled = "disabled" , @readonly = "readonly" ,onChange="return GetTTLFright(this);"}) @Html.TextBoxFor(model => model.CNFPK, new { onChange="return GetCNFPK(this);"}) @Html.TextBoxFor(model => model.TTLCNF, new {disabled = "disabled" , @readonly = "readonly",onChange="return GetTTLFright(this);" }) @Html.TextBoxFor(model => model.AWB, new { onChange="return GetTTLFright(this);"}) and script <script> function GetW