Prevent multiple instances of windows - wpf

How could I prevent opening multiple windows from a wpf application?
I need to open a window from the menu and if I click to open it again, I want the already opened window to become active.
Any suggestions?

You could use the Application.Current.Windows collection for that. Just check whether this collection contains the window you are about to open and if it does, activate it, otherwise create new window and show it:
var existingWindow = Application.Current.Windows.Cast<Window>().SingleOrDefault(w => /* return "true" if 'w' is the window your are about to open */);
if (existingWindow != null) {
existingWindow.Activate();
}
else {
// Create and show new window
}

Here is one way to do it:
private Window _otherWindow;
private void OpenWindow()
{
if (_otherWindow == null)
{
//Pass in a reference to this window so OtherWindow can call WindowClosed when it is closed..
_otherWindow = new OtherWindow(this);
_otherWindow.Show();
}
else
_otherWindow.Activate(); //Or whatever the method is to bring a window to the front
}
public void WindowClosed()
{
_otherWindow = null;
}

Related

How to code an iPhone style popup menu in CN1?

It has probably been covered before, but I couldn’t google anything. What is the best approach for making an iPhone-style pop-up selection menu like attached picture? I've tried with a Dialog, but I haven't found an elegant way to add the Commands so they appear nicely and both trigger the action and close the dialog at the same time. And showing a Cancel entry separately is not supported by a ComponentGroup.
See this sample:
Form hi = new Form("Pop");
Button pop = new Button("Pop");
pop.addActionListener(e -> {
Dialog dlg = new Dialog();
// makes the dialog transparent
dlg.setDialogUIID("Container");
dlg.setLayout(BoxLayout.y());
Command optionACmd = new Command("Option A");
Command optionBCmd = new Command("Option B");
Command optionCCmd = new Command("Option C");
Command cancelCmd = new Command("Cancel");
dlg.add(
ComponentGroup.enclose(
new Button(optionACmd),
new Button(optionBCmd),
new Button(optionCCmd)
)).
add(ComponentGroup.enclose(new Button(cancelCmd)));
Command result = dlg.showStretched(BorderLayout.SOUTH, true);
ToastBar.showMessage("Command " + result.getCommandName(), FontImage.MATERIAL_INFO);
});
hi.add(pop);
hi.show();
Which results in this:
Thanks Shai!
I made it into a component in case anybody has a similar need:
class MyPopupMenu extends Dialog {
private Command cancelCmd = null;
MyPopupMenu(boolean includeCancel, Command... commands) {
this(includeCancel?new Command("Cancel"):null, commands);
}
MyPopupMenu(Command cancelOptional, Command... commands) {
super();
setDialogUIID("Container");
setLayout(BoxLayout.y());
setDisposeWhenPointerOutOfBounds(true); //close if clicking outside menu
ComponentGroup group = new ComponentGroup();
for (Command cmd : commands) {
group.add(new Button(cmd));
}
add(group);
this.cancelCmd = cancelOptional;
if (cancelCmd != null) {
add(ComponentGroup.enclose(new Button(cancelCmd)));
}
/**
* show the menu and execute the selected Command,
* or do nothing if Cancel is selected
*/
public void popup() {
Command choice = showStretched(BorderLayout.SOUTH, true);
if (choice != null && choice != cancelCmd) {
choice.actionPerformed(null);
}
}
}
This is awesome, thanks guys. Any reason my buttons seem to be so small? Trying to figure out which style needs to change to increase the height. Changing the Button padding did not seem to change anything. I used the Business theme as a starting point.

WinForms ShowDialog() - No modal dialog box opens

I want to open a dialog as a Login-Box in my Explorer-NamespaceExtension. When I call ShowDialog() the first time the box opens but not as a modal dialog. I can click elements in the Explorer. If I close these dialog and open it again, it is a modal dialog and interaction with the Explorer isn`t possible. Thats what I want to achieve with the first open.
My idea is that I call the form first from the wrong thread. Thats the reason why I used the following code, but it doesn`t solve the problem :/
public delegate void myDelegate();
public void ShowDialogThreadSave()
{
if (this.InvokeRequired)
{
myDelegate d = new myDelegate(ShowDialogThreadSave);
this.Invoke(d);
}
else
{
this.ShowDialog();
}
}
I hope you have an idea :-)
Thanks!
Edit:
The call is fired from a background class. I have 3 possibilities to login into the extension, so i encapsulated the call:
public bool LogIn()
{
bool connected = BackEnd.isConnected();
if(loginDialog == null)
{
LogIn logIn = new LogIn();
}
else
{
if (!connected && !Utils.AlreadyLoggedIn() && !loginDialog.IsAccessible && !loginDialog.Visible)
loginDialog.ShowDialogThreadSave();
else if (!connected && !Utils.AlreadyLoggedIn() && !loginDialog.Visible)
loginDialog.ShowDialogThreadSave();
else if (!connected && !Utils.AlreadyLoggedIn() && loginDialog.Visible)
LOG.DebugFormat("error");
else
Utils.ConnectWithoutLoginWindow();
}
connected = BackEnd.isConnected();
return connected;
}
Edit2: With debugging I found out that the ShowDialogThreadSave() is always called by the UI Thread and I will never use the if... What is the problem?

Java: focus is not on pop-window during window handling

I have opened the website and applied the Login then popup window opens, i want to click from window popup but i am not able to switch on popup.
driver.get("https://hdfcbank.com/");
driver.findElement(By.id("loginsubmit")).click();
String loginWindow = driver.getWindowHandle();
driver.switchTo().window(loginWindow);
driver.findElement(By.xpath("//*[#id='wrapper']/div[6]/a/img")).click();
I am not able click on popup element at line 5. can you check the code.
Check the accepted answer for a similar question
How to handle Pop-up in Selenium WebDriver using Java
You need to - getWindowHandles - & then iterate over them.
Here is the working solution in case you still haven't figured it out (this is for the HDFC example)...
String test_URL = "http://www.hdfcbank.com/";
String css_login = "img#loginsubmit";
String css_popup_continue = "img[alt='Continue']";
browser = new FirefoxDriver();
browser.navigate().to(test_URL);
List<WebElement> objLogin = browser.findElements(By.cssSelector(css_login));
if (objLogin.size() > 0) {
objLogin.get(0).click();
String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Iterator<String> windowIterator = browser.getWindowHandles().iterator();
while(windowIterator.hasNext()) {
String windowHandle = windowIterator.next();
popup = browser.switchTo().window(windowHandle);
if (popup.getTitle().contains("NetBanking")) {
List<WebElement> objPopupElement = popup.findElements(By.cssSelector(css_popup_continue));
if(objPopupElement.size() > 0){
System.out.println("Switched to Popup and found element...");
objPopupElement.get(0).click();
//Do any other operations...
break;
}
}
}
//always safe to switch back to parent window to avoid any null pointers, unless parent process got closed...
browser.switchTo().window(parentWindowHandle);
}
else {
System.out.println("Logon button not found...");
}

Is that possible to have open dialog window inside child window in Silverlight?

I need to have OpenDialog window being inside browser window of Silverlight application. I am wondering if it possible. Any help is highly appreciated!
Below is the code I have to open child window and OpenDialog:
private void openChildWindow_Click(object sender, System.Windows.RoutedEventArgs e)
{
Add_ChildWindow ap = new Add_ChildWindow();
ap.Show();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "Packages (*.sprj)|*.sprj|Packages (*.sprj)|*.sprj";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
{
// Open the selected file to read.
//textBox1.Text = openFileDialog1.File.Name;
System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
{
// Read the first line from the file and write it the textbox.
// tbResults.Text = reader.ReadLine();
}
fileStream.Close();
}
}
An Open Dialog window is a system window. Silverlight being sandboxed has to use the system open dialog I believe.
In other words, I don't think this is possible.

How to create input gesture for page/window from an user control

I have a reusable usercontrol that uses a few commands and corresponding keyboard gestures,
(specifically Escape and Ctrl+1...Ctrl+9)
Now as I use this usercontrol in multiple locations I'd like to define the input gestures in the usercontrol, which works fine as long as the focus is within the usercontrol. However, I'd need it to work as long as focus is within the current page/window.
How can I do it, or do I really have to do command/input bindings on every page?
You could handle the Loaded event of the UserControl and walk up the logical tree to find the owning page/window, then you can add the bindings there.
e.g.
public partial class Bogus : UserControl
{
public Bogus()
{
Loaded += (s, e) => { HookIntoWindow(); };
InitializeComponent();
}
private void HookIntoWindow()
{
var current = this.Parent;
while (!(current is Window) && current is FrameworkElement)
{
current = ((FrameworkElement)current).Parent;
}
if (current != null)
{
var window = current as Window;
// Add input bindings
var command = new AlertCommand();
window.InputBindings.Add(new InputBinding(command, new KeyGesture(Key.D1, ModifierKeys.Control)));
}
}
}

Resources