Refresh or Close wpf window - wpf

I have a wpf window that shows Bing map whith som bushpin on it,
The window is getting opened by button click in code behind.
Each time i click on the button, new window shows up with current new data.
How can i close the "allready" opened window and open the a new one with the new data, or maybe refresh the current opened window with the new data.
here is my code:
IList<object> rowsToExport = getRows();
BingMapWindow window = new BingMapWindow(rowsToExport);
// somthing like this
if(window.IsOpened)
window.Close;
window.show();
OR
if(window.IsOpened)
window.refresh();

Have you considered storing reference to that window outside method and just checking it for null?
BingMapWindow window;
private void CloseWindow()
{
if(window != null)
{
window.Close();
window = null;
}
}
private void OpenWindow(BingMapWindow window)
{
this.window = window;
this.window.Show();
}
private void ButtonHandler()
{
CloseWindow();
var bingWindow = new BingMapWindow();
OpenWindow(bingWindow);
}

Related

Dispose the dialog box from back command

I've a dialog box which I want dispose by from back command as well. But it is not happening. Why is it?
selectDialog = new Dialog();
selectDialog.showPacked(BorderLayout.CENTER, false);
selectDialog.setDisposeWhenPointerOutOfBounds(true);
//why doesn't following code dispose dialog box?
Command back = new Command("") {
public void actionPerformed(ActionEvent ev) {
if (selectDialog != null) {
System.out.println("beck");
selectDialog.dispose();
}
}
};
setBackCommand(back);
Instead of:
setBackCommand(back);
Use:
selectDialog.setBackCommand(back);

Custom MessageBox using Window

In regard to the code below.
If I use the built in MessageBox, then the previous MessageBox has to be closed before the next one is displayed.
How can I achieve this with a Window so that I can create a custom message box? I tried using the ShowDialog method, but whilst this does create Modal windows, it still shows them all at the same time cascaded.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 3; ++i)
{
Dispatcher.BeginInvoke(new Action(() => ShowDialog2()));
}
}
void ShowDialog2()
{
//MessageBox.Show("A message");
Window w = new Window() { Width = 200, Height = 200, Content = "SomeText" };
w.ShowDialog();
}
}
Open first instance of window with ShowDialog and consequent instances of window with Show method.
Show open a non-modal window whereas ShowDialog open modal window.

How to stop a new window to be opened every time?

I have a WPF application in which on a click of a menu item a window is opened. If the same menu item is clicked again when the window is already open, it is opening a new window but I don't want a new window to be opened every time.
What I need is, if the window is already open, the same window should be focused not a new window.
//First we must create a object of type the new window we want the open.
NewWindowClass newWindow;
private void OpenNewWindow() {
//Check if the window wasn't created yet
if (newWindow == null)
{
//Instantiate the object and call the Open() method
newWindow= new NewWindowClass();
newWindow.Show();
//Add a event handler to set null our window object when it will be closed
newWindow.Closed += new EventHandler(newWindow_Closed);
}
//If the window was created and your window isn't active
//we call the method Activate to call the specific window to front
else if (newWindow != null && !newWindow.IsActive)
{
newWindow.Activate();
}
}
void newWindow_Closed(object sender, EventArgs e)
{
newWindow = null;
}
I think this solve your problem.
Att,
If your opened windows is used as simple dialog box you can use following code
window.ShowDialog();
when the dialog will show you cannot press any menu items unit you close this window
A rather brute force approach like this also works:
bool winTest = false;
foreach (Window w in Application.Current.Windows)
{
if (w is testWindow)
{
winTest = true;
w.Activate();
}
}
if (!winTest)
{
testWindow tw = new testWindow();
tw.Show();
}
You can create a field and check if it's set:
private Window _dialogue = null;
private void MaekWindowButton_Click(object sender, RoutedEventArgs e)
{
if (_dialogue == null)
{
Dialogue diag = new Dialogue();
_dialogue = diag;
diag.Closed += (s,_) => _dialogue = null; //Resets the field on close.
diag.Show();
}
else
{
_dialogue.Activate(); //Focuses window if it exists.
}
}

Using IVMRWindowlessControl to display video in a Winforms Control and allow for full screen toggle

I've recently switched from using the IVideoWindow interface to IVMRWindowlessControl in my custom Winforms control to display video.
The reason for this was to allow zoom capabilities on the video within the control.
However in switching over, I've found that the FullScreen mode from IVideoWindow is not available and I am currently trying to replicate this using the SetVideoWindow() method.
I'm finding that I size the video in my control to be at the same resolution as the screen however I can't get the control to position itself to the top/left of the screen and become the top most window.
Any ideas on how to achieve this since the IVideoWindow::put_FullScreenMode just did it all for you?
Resolved the FullScreen problem by hosting the video control in a fresh form which I resized to the size of the current screen, then handled the 'Escape' key press in the form, to toggle back to the normal size video. Here's an extract of the code:-
Members
private Rectangle fullScreenRectangle;
private bool fullScreen;
private Form fullScreenForm;
private Control fullScreenParent;
Toggle FullScreen code
/// <summary>
/// Toggle Full Screen Mode
/// </summary>
public bool FullScreen
{
get
{
return this.fullScreen;
}
set
{
this.fullScreen = value;
if (this.fullScreen)
{
// If switch to full screen, save the current size of the control
this.fullScreenRectangle = new Rectangle(this.Location, this.Size);
// Get the current screen resolution and set that to be the control's size
Rectangle screenRect = Screen.GetBounds(this);
// Create a new form on which to host the control whilst we go to full screen mode.
this.fullScreenForm = new Form();
this.fullScreenForm.Location = PointToScreen(new Point(0, 0));
this.fullScreenForm.Size = new Size(screenRect.Width, screenRect.Height);
this.fullScreenForm.BackColor = Color.Black;
this.fullScreenForm.ShowInTaskbar = false;
this.fullScreenForm.ShowIcon = false;
this.fullScreenForm.FormBorderStyle = FormBorderStyle.None;
this.fullScreenForm.KeyPreview = true;
this.fullScreenForm.PreviewKeyDown += new PreviewKeyDownEventHandler(fullScreenForm_PreviewKeyDown);
this.fullScreenParent = this.Parent;
this.fullScreenForm.Controls.Add(this);
this.fullScreenForm.Show();
this.windowlessControl.SetVideoPosition(null, screenRect);
}
else
{
// Revert to the original control size
this.Location = PointToScreen(new Point(this.fullScreenRectangle.Left, this.fullScreenRectangle.Top));
this.Size = new Size(this.fullScreenRectangle.Width, this.fullScreenRectangle.Height);
this.windowlessControl.SetVideoPosition(null, this.fullScreenRectangle);
if (this.fullScreenForm != null)
{
this.fullScreenForm.Controls.Remove(this);
if (this.fullScreenParent != null)
this.Parent = this.fullScreenParent;
this.fullScreenForm.PreviewKeyDown -= new PreviewKeyDownEventHandler(fullScreenForm_PreviewKeyDown);
this.fullScreenForm.Close();
}
}
}
}
void fullScreenForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
var viewer = this.Controls[0] as ViewerControl;
if (viewer != null)
viewer.FullScreen = false;
}
}

How do I access a MessageBox with white?

I have a simple message box in a WPF application that is launched as below:
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Howdy", "Howdy");
}
I can get white to click my button and launch the message box.
UISpy shows it as a child of my window I couldn't work out the method to access it.
How do I get access to my MessageBox to verify its contents?
Found it! The window class has a MessageBox method that does the trick:
var app = Application.Launch(#"c:\ApplicationPath.exe");
var window = app.GetWindow("Window1");
var helloButton = window.Get<Button>("Hello");
Assert.IsNotNull(helloButton);
helloButton.Click();
var messageBox = window.MessageBox("Howdy");
Assert.IsNotNull(messageBox);
Please try this
Window messageBox = window.MessageBox("");
var label = messageBox.Get<Label>(SearchCriteria.Indexed(0));
Assert.AreEqual("Hello",label.Text);
Contained in the White source code are some UI tests projects (to test White itself).
One of the test includes MessageBox tests, which includes a way to obtain the displayed message.
[TestFixture, WinFormCategory, WPFCategory]
public class MessageBoxTest : ControlsActionTest
{
[Test]
public void CloseMessageBoxTest()
{
window.Get<Button>("buttonLaunchesMessageBox").Click();
Window messageBox = window.MessageBox("Close Me");
var label = window.Get<Label>("65535");
Assert.AreEqual("Close Me", label.Text);
messageBox.Close();
}
[Test]
public void ClickButtonOnMessageBox()
{
window.Get<Button>("buttonLaunchesMessageBox").Click();
Window messageBox = window.MessageBox("Close Me");
messageBox.Get<Button>(SearchCriteria.ByText("OK")).Click();
}
}
Evidently, the label used to display the text message is owned by the window displaying the messagebox, and its primary identification is the max word value (65535).
window.MessageBox() is a good solution!!
But this method would stuck for a long time if the messagebox doesn't appear. Sometimes I want to check "Not Appearance" of a messagebox (Warning, Error, etc.). So I write a method to set the timeOut by threading.
[TestMethod]
public void TestMethod()
{
// arrange
var app = Application.Launch(#"c:\ApplicationPath.exe");
var targetWindow = app.GetWindow("Window1");
Button button = targetWindow.Get<Button>("Button");
// act
button.Click();
var actual = GetMessageBox(targetWindow, "Application Error", 1000L);
// assert
Assert.IsNotNull(actual); // I want to see the messagebox appears.
// Assert.IsNull(actual); // I don't want to see the messagebox apears.
}
private void GetMessageBox(Window targetWindow, string title, long timeOutInMillisecond)
{
Window window = null ;
Thread t = new Thread(delegate()
{
window = targetWindow.MessageBox(title);
});
t.Start();
long l = CurrentTimeMillis();
while (CurrentTimeMillis() - l <= timeOutInMillsecond) { }
if (window == null)
t.Abort();
return window;
}
public static class DateTimeUtil
{
private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long currentTimeMillis()
{
return (long)((DateTime.UtcNow - Jan1st1970).TotalMilliseconds);
}
}

Resources