Here's my problem : I have my first WPF window, and I create a second when changing the value of the SelectedItem of a Combobox. And I want to close the window I created when the value is changed again. I tried this :
var cCEntityWindow = new Windows.CCEntityWindow(dptList);
cCEntityWindow.CloseWindow();
from the codebehind of my first Window, but it doesn't work, so I create a simple method in my second Window :
public void CloseWindow()
{
this.Close();
}
and I call it from my first Window, but it doesn't work either, and I don't know why !
How should I do this ?
You should call this.Close() from the Window that you want to close, not from the other one. You could try this method, by passing the windowName
Window wintoclose = Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.Name == "Window Name");
wintoclose.Close();
Related
I programatically created a new window and frame, in order to navigate to a separate page (Authentication). Upon closing the window, I want to do some stuff, but the if statement is never returning false.
Window newWindow = new Window();
Page authentication = new Authentication();
Frame newFrame = new Frame();
newWindow.Title = "Authentication";
newWindow.Content = newFrame;
newFrame.NavigationService.Navigate(authentication);
newWindow.Show();
if (IsWindowOpen<Window>("Authentication") == false)
{
//DO THINGS HERE
}
This is my IsWindowOpen method:
public static bool IsWindowOpen<T>(string name = null) where T : Window
{
return string.IsNullOrEmpty(name)
? Application.Current.Windows.OfType<T>().Any()
: Application.Current.Windows.OfType<T>().Any(w => w.Title.Equals(name));
}
I manage to open the new window and run the separate page, but upon closing nothing happens. I've added a print line in the if statement to check, and it doesn't print.
Calling Show() won't block the main thread.
If you want to do something when the window closes you should handle Closing event and do things there.
If you want to block the current thread until the window is closed, you should use ShowDialog() instead.
I have a C# winform project that displays a list of results based on a user's search criteria. For each item on the list, the user can open a modeless dialog box showing more details about the selected item.
Every time the user opens an instance of my details window, this code runs:
public void showDetails()
{
GetDetails route = new GetDetails();
route.myParent = this;
route.Show();
}
In order to compare details between two or more items, the user is allowed to open as many instances of this dialog box as it likes. I'd like to be able to close any and all open instances of this window when the user conducts a new search from the main form window? I've tried Googling, but no luck ... does anyone know how to do this?
Application.OpenForms is a collection of open forms owned by the application
try find all details dialogs and close them like this:
foreach(var f in Application.OpenForms.OfType<GetDetails>().ToList())
{
f.Close();
}
You don't really tell, but I assume your GetDetails is a System.Windows.Forms.Control (probably a form, a dialog box, a message box, etc).
If you look closely to your Form.InitializeComponent, you'll see that Form has a property Controls. All child controls are added to the control collection.
If you add each created route to your control collection you can ask this collection for all objects of type GetDetails and order them to close:
public void ShowDetails()
{
var route = new GetDetails();
route.myParent = this;
this.Controls.Add(route);
route.Show();
}
public void CloseAllRoutes()
{
foreach (var route in this.Controls.Where( control => control is GetDetails))
{
route.Close();
}
}
You need to be certain that when a rout is closed, or disposed or something the following code is called:
private void OnRouteClosed (object sender, ...)
{
if (sender is GetDetails)
{
this.Controls.Remove(sender);
}
}
I have an app that works in two screens, the second screen is a settings panel. From the settings panel i need to refresh the content of the frame in the main window. I'm using this code:
MainWindow winFirst= new MainWindow();
var primaryScreen = System.Windows.Forms.Screen.AllScreens.Where(s => s.Primary).FirstOrDefault();
if (primaryScreen != null)
{
winFirst._mainFrame.Source = new Uri("DynamicSource.xaml", UriKind.Relative);
winFirst.Show();
}
with this code i can load in main window what i need by pressing a button in second window. But this code load a new MainWindow class every time i launch it...I think it's not the best approch, maybe i will encounter some memory related problem. So if this is a problematic approch how i cand do to access the frame of the current mainWindow and change it without reinit ?
Application curApp = Application.Current;
Window mainWnd = curApp.MainWindow;
i have also try this code but in this way i can't find in mainWnd the _mainframe element.
The main window can always be accessed by Application.Current, but it is returned as a Window. However since you know what it should be, you can just cast it:
var mainWin = Application.Current.MainWindow as MainWindow;
Debug.Assert( mainWin != null ); //sanity check
//do things with mainWin
Newbie question! I have "MainWindow" and "EditSettings", which are classes. As far as I can gather, (please correct me if wrong) they are both classes derived from the Window class:
public partial class MainWindow : Window
When I actually want to create a window, I create an instance of the derived class, like so:
EditSettings winEditSettings = new EditSettings();
This means that winEditSettings is an instance of class EditSettings, which is derived from Window.
If I wanted to then write a method which accepts ANY of my windows as an argument, what would I write as the argument? I initially had:
protected void OpenWindowOnce(Window win)
{
//This method tells the user whether the window is already open or not
foreach (Window n in Application.Current.Windows)
if (n == win)
{
MessageBox.Show("The window is already open");
}
else
{
MessageBox.Show("You have not opened that window yet");
win.Show();
}
It seems as though the IF part of the statement is not working - it keeps telling me that the window is not open yet, and then opening a new version of it. I called the method as follows:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
EditSettings winEditSettings = new EditSettings();
OpenWindowOnce(winEditSettings);
}
That's all I have in my program so far - I'm just trying to write a method that prevents windows from being opened more than once. Running this code gives me:
"You have not opened that window yet" --> The window "winEditSettings" is opened.
If I press the button again, I get two messages saying "You have not opened that window yet" and it opens another instance of winEditSettings.
EDIT
How can I rewrite my OpenWindowOnce method to accept any instance of any window class? I want to be able to pass winEditSettings, or winMainWindow, or winAbout as parameters.
Since you pass instances to your method, you have to pass the instance of the opened window to see if it is opened.
Otherwise, just try comparing their types:
void ShowWindowOnce(Window window)
{
foreach (Window n in Application.Current.Windows)
{
if (typeof(n) == typeof(window)
{
// A window with the given type is opened, no need to show another one so return
return;
}
}
// No open form with that type was open, show it
window.Show();
}
You pass the instanced Window to your method, so you need to know if the Window is loaded or not.
private void OpenOnce(Window win)
{
foreach (Window n in Application.Current.Windows)
{
if (n.GetType() == win.GetType())
{
if (n.IsLoaded)
{
MessageBox.Show("The window is already open");
return;
}
}
}
win.Show();
}
I'm working on a term paper that performs operations with complex numbers. Application looks like this:
If you click on "Calculate", will calculate the operands and the result is displayed in a new dialog box. So I have two dialogs - a main window (up on pic) to display the read result (down on pic). Unfortunately I was not able to save result in the main dialog box, which is then written to a text file. There is important piece of code:
Thank you for ideas.
In your MainForm class declare a property as
public class MainForm:Form
{
public string CalculationResult { get; set; }
...
...
//Your code here
...
...
}
then on method of calculate change it to
if(resultBool!=null)
{
CalculationResult = resultBool.ToString());
formResult dlg=new formResult(CalculationResult);
dlg.Owner=this
dlg.StartPosition=FormStartPosition.CenterParent;
dlg.ShowDialog();
}
else
{
...
...
//your code
...
...
}
and change the following line
sw.WriteLine("Result: ");
to
sw.WriteLine("Result: " + CalculationResult);
in saveData method which is in MainForm class. Hope this will work for you. Happy coding
You have to use a reference on the target form to write on it.
You have to assure that you use the correct targetForm reference. You could either create a new targetForm or use an existing one. You could for example use the "parent" form of your dialog box and set the text of the main form textbox, from a dialog box event (using this as MSDN reference):
Dim targetForm As Form = Me.parentForm
targetForm.targetTextBox.Text = "text"
Hope I helped!
In formResult create a variable which keep a reference of Main form
private MyMainForm _mainForm;
Then create one more constructor which take reference of your Main form as parameter:
public formResult(MyMainForm mainform)
{
this.InitializeComponent();
this._mainForm = mainform;
//Now you have access to public property ResultOut
this.textBoxResult.Text = this._mainForm.ResultOut;
}
After this you can use all public properties and method of your main form in second form(formResult)