Problems opening/closing a 'popped-up' window - c#

I have a web app that launches from an online portal into a new browser window. Everything works out fine until I try to open a new window or close the window from the webapp. Everything works fine when I test it locally though.
From what I've understood so far, after some Googling, it seems that this is because you can't call client side code from server side, for security reasons. I'll post what I've tried to implement for the Open and Close functions below:
Open
String javaScript = "window.open('http://www.google.com', null, 'height=555,width=760,
status=yes,toolbar=no,menubar=no,
location=no, scrollbars=yes');";
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", javaScript, true);
Close
Close Window
as well as a simple
Close
in the HTML itself.
I know this question has been asked countless times before, but I've gone diving in the forums and Google and still can't find something that works, so I appreciate any help in this.
For now I'm suspecting that the problem lies in trying to call a pop-up window from another pop-up window for the Open function, while for the Close function, the server does not recognize the handle of the window that opened the pop-up.
Edit:
My apologies to everyone if I didn't make myself clear enough. I'll try to explain in more detail here.
Basically, in my web application, I have these 2 buttons, Open and Close.
The Open button will open up a link to another website in a brand new browser window.
The Close button will close the browser window that the application is residing in.
When I tested it out locally, i.e. localhost:xxxxx/Game.aspx, everything works fine. However, the app currently does not reside in a separate pop-up window called from another browser window
On the test server, the application is launched into a new browser window from an online game repository portal.
So just to re-illustrate the way I want it to work:
1. User logs into game repository page
2. User chooses a game to play
3. Game pops up in a new browser window
4. When user clicks on Open button, another new window pops up to a specific website(Does not work)
5. When user clicks on Close button, the game window closes itself(Does not work)
I hope this makes the issue clearer. Thanks.

I'm not totally getting what you are trying to say, but hopefully i understand enough. You need to call Close() on the window object that you opened. So change your javascript to this:
string js = "var myWindow = window.open('http://www.google.com', null, 'height=555,width=760,
status=yes,toolbar=no,menubar=no,
location=no, scrollbars=yes');";
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", js, true);
and then in your page:
Close
If it helps then check this MSDN article.

Associate the popup windows to a variable which can in turn help you control the window which you will be closing, hence:
// Define the variable for the popup window
// You can use this to check if the window was indeed opened, before attempting to close
var myOpenedWindow = '';
// Open Window
myOpenedWindow = window.open('http://www.google.com', Null, 'height=555,width=760, status=yes,toolbar=no,menubar=no, location=no, scrollbars=yes');
// Close Window Function
function closeMyWindow() {
// Check for window instance being open
if (myOpenedWindow != '') {
// Close the window
myOpenedWindow.close();
// Clear the variable
myOpenedWindow = '';
}
}
// Close Window [DIRECT]
closeMyWindow();
/* OR */
// Close Window [FROM POPPED WINDOW]
window.opener.closeMyWindow();
There's plenty of ways to control your popped windows, and hopefully with these examples you can better understand how to control them.

Related

Why does a folder dialog immediately close unless a window is displayed in WPF?

I'm developing a WPF application that's meant to live in the tool tray, so it doesn't involve any windows. Right-clicking the tool tray icon brings up a menu with a Configure Report Path... option, and I'd like to display a folder browser dialog to the user when this is clicked:
What I'm finding is that when the option is selected, a dialog opens and immediately closes unless I assign some window to Application.Current.MainWindow and show it before opening the dialog. This is the code I'm using:
public CounterIconViewModel(IMessenger messenger)
{
void ConfigureReportPath()
{
// Application window must be created and displayed.
Application.Current.MainWindow = new Window();
Application.Current.MainWindow.Show();
var browseDialog = new VistaFolderBrowserDialog { ShowNewFolderButton = false };
if (browseDialog.ShowDialog() != true)
{
return;
}
// (Separate issue) Command doesn't execute unless I comment out the line below.
//messenger.Send(browseDialog.SelectedPath, "ReportPath");
}
ConfigureReportPathCommand = new RelayCommand(ConfigureReportPath);
ExitApplicationCommand = new RelayCommand(Application.Current.Shutdown);
}
In this case I'm using VistaFolderBrowserDialog from Ookii.Dialogs.Wpf, but I've tried the same thing with another WPF browser dialog and notice identical behaviour.
Is there a reason why a browser dialog seems to require a window to be displayed to remain open, and any workarounds?
Update
I've found that if I initialize and pass an instance of Window to browseDialog.ShowDialog, the dialog remains open without me having to assign the main application window and display it:
if (browseDialog.ShowDialog(new Window()) != true)
I don't understand why this works. I'll post this as an answer if no others appear so that at least people in a similar situation are aware of this workaround.
Update 2
The other dialog I tested it with was CommonOpenFileDialog from Microsoft.WindowsApiCodePack-Shell:
var browseDialog = new CommonOpenFileDialog { IsFolderPicker = true };
browseDialog.ShowDialog();
My tool tray icon displays a rich tool-tip (a custom UserControl) if I hover over it, and with this browser dialog I found that:
If I hover over the icon to make the tool-tip display, then the browser dialog works fine when I try to open it on the first and every subsequent attempt.
If I try to open the browser dialog before displaying the tool-tip display, the browser dialog opens and closes immediately on the first try, but then remains open on every subsequent attempt.
This dialog also accepts a Window instance in ShowDialog but it makes no difference if I pass one or not.
My workaround (initializing and passing a blank window to the Ookli dialog browser) seems to work fine regardless of whether I first bring up the tool-tip, so I'm sticking with that for the time being.

Material Design DialogHost doesn't close after First Page Load in WPF

I've been working with this WPF application that integrates a UrhoSharp Window. When I click somewhere in the Urho window, there's this dialog that pops up as rendered by the main WPF Application.
The issue is that on every application startup or first page load, the dialog pops as it should but doesn't respond to any of the user actions (the dialog has buttons in it) which is supposed to close the dialog eventually. However, if I resize the window or just click the application in the Start Bar, the dialog now responds(happily ever after). This issue doesn't return until I exit the current application page and switch back to the same page. The case is also the same for every application startup.
All the buttons in the dialog executes this command:
ICommand cmd = DialogHost.CloseDialogCommand;
Execute(null);
And the CloseDialogCommand is awaited like this:
var dialog = new SomeDialog();
dialog = (SomeDialog)await Controller.dialogHandler(dialog, true);
I couldn't figure out what the issue actually is. May be some help would do good.

Open a dialog in the same window and wait for user input

Is there a way to open a new window instance within the primary application window and still wait for a user response before continuing with the main program? I just have a 3 option query with a picture to be offered to the user and a whole new window opening up seems like overkill, I would much prefer it opening in a frame within the current window.
Do something like this sans the separate window:
OptionWindow optionDialog = new OptionWindow();
optionDialog.Owner = this;
optionDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
optionDialog.ShowDialog();
if (optionDialog.DialogResult == true)
{
something;
{
Seems like you could just have your "OptionWindow" be a frame that is on the WPF form, and when you want to show it, just disable all other panels and show the frame with your dialog, and after the user completes the dialog, enable all other panels and hide your dialog frame.
After I posted my answer, I saw that Mark Hall made an edit to his comment with pretty much the same suggestion. Sorry Mark.
I guess you are looking for modal window which can be achieved using Window.Showdialog.
Opens a window and returns only when the newly opened window is
closed.

How do I open a window as a modal dialog in MonoMac using C#?

I am developing a Cocoa application with MonoMac (C#).
I have a class MyWindowController that inherits MonoMac.AppKit.NSWindowController, and open a new instance of this window like this:
MyWindowController mwc = new MyWindowController();
mwc.Window.MakeKeyAndOrderFront(this);
But how do I open it as a modal dialog? It is imperative that nothing else in my application is executed while the dialog is open, so I cannot use a window sheet (which only blocks the current window). And I can't find anything else that seems to do what I want on my controller. On Windows, I would have done this simply by calling:
mwc.ShowDialog();
So what I want is the MonoMac equivalent of ShowDialog(), I believe.
I spent almost three hours trying to figure this out before posting the question, but of course I found the solution right after asking.
It looks like I need to use the NSApplication object:
NSApplication.SharedApplication.RunModalForWindow(ewc.Window);
I had success with the answer above, but then had trouble dismissing the modal in the case that the 'close' button was pressed. The solution was to add the code
[Export ("windowWillClose:")]
public void WindowWillClose(NSNotification notification)
{
Console.WriteLine("windowWillClose:");
NSApplication.SharedApplication.StopModal ();
}
to the window controller, then set the window controller to be the window's delegate by, in Interface Builder, right-click the window and drag a line from "delegate" to the "file's owner" block.

Is it possible to force a WebBrowser to, instead of opening in a new window, replace the currently opened window?

So I have a link I'm trying to click on a WebBrowser control. The problem is it pops up in a new tab, making IE open. I can't manage the web pages after it opens in IE, so I need to force it to somehow stay within my program. It doesn't matter if another WebBrowser control needs to open or anything, just so long as it says in my program.
there are a few ways you can do this, one way is to loop through the DOM (Document Object Model) and find the link which opens in a new window, and change its "target=" attribute value to "target=_top" this way it will open in current top-level page of your current WB Control container.
Otherwise, there is a NewWindow2 event which you can intercept, write the following code inside that event (where WB1 is you WebBrowser Controls name):
Processed = True
WB1.Navigate2 URL
This will tell your WB Control that the request has been processed (tricking it into believing that it has been processed), and if you just did this, nothing would happen, so after the first line you write the second line which tells it to open the URL that it just tried to open in the new window, in the current window (WB1), so really you are just reissuing the request but for the same container/WB Control where the link was clicked.
I've written the code in VB, however I'm sure it's not a problem to understand and transfer to c#.
Let me know how you get along and if there is anything else i can do to help.

Categories

Resources