C#/MonoDevelop: GTK MessageDialogs require a doubleclick to close - why? - c#

I'm a newbie programmer writting a program in MonoDevelop in C# and have a porblem with my gtk MessageDialogs.
The close button on the window boarders of my GTK Message dialogues require a double click to actually close them. The close button on the dialogue its self works fine. Could someone please tell me how I can fix this below is the code:
if (fchDestination.CurrentFolder == fchTarget.CurrentFolder) {
MessageDialog msdSame = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "Destination directory cannot be the same as the target directory");
msdSame.Title="Error";
if ((ResponseType) msdSame.Run() == ResponseType.Close) {
msdSame.Destroy();
}
return;
}
if (fchTarget.CurrentFolder.StartsWith(fchDestination.CurrentFolder)) {
MessageDialog msdContains = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "error");
msdContains.Title="Error";
if ((ResponseType) msdContains.Run() == ResponseType.Close) {
msdContains.Destroy();
}
return;
}

The response value given when you click on a dialog's "close window" button is not CLOSE, but DELETE_EVENT. That's why the destroy method is never called and the dialog lingers. The second time you close it (out of the context of the run method), the dialog is destroyed normally.
In short, you also need to check for ResponseType.DeleteEvent.
Update:
In code:
MessageDialog msdSame = ...
...
ResponseType response = (ResponseType) msdSame.Run();
if (response == ResponseType.Close || response == ResponseType.DeleteEvent) {
msdSame.Destroy();
}
Or, as ptomato mentions, you don't need to check the response, considering the user only has one choice: "close".
MessageDialog msdSame = ...
...
msdSame.Run();
msdSame.Destroy();

May be both the conditions get satisfied and hence you get two msgbox and it appears you've to give a double click to close it

A sample class could be:
using System;
using Gtk;
namespace Visitors.Clases.MessageBox
{
public static class MessageBox
{
public static Gtk.ResponseType Show(Gtk.Window window, Gtk.DialogFlags dialogflags, MessageType msgType,ButtonsType btnType,string Message,String caption)
{
MessageDialog md = new MessageDialog (window,dialogflags,msgType,btnType, Message);
md.Title = caption;
ResponseType tp = (Gtk.ResponseType)md.Run();
md.Destroy();
return tp;
}
}
}
The class in use:
ResponseType result = MessageBox.Show(this,DialogFlags.Modal,MessageType.Error,ButtonsType.Ok,Error,"ERROR");
if (result == Gtk.ResponseType.Yes)
{
MessageBox.Show (this, DialogFlags.Modal, MessageType.Other,ButtonsType.Ok, "YES", "EJEMPLO");
}
else
{
MessageBox.Show (this, DialogFlags.Modal, MessageType.Other,ButtonsType.Ok, "NO", "EJEMPLO");
}

Related

Capture Button Click event inside a MessageBox in another application

I want to capture the OK Button's Click event on a MessageBox shown by another WinForms application.
I want to achieve this using UI Automation. After some research, I have found that IUIAutomation::AddAutomationEventHandler will do the work for me.
Though, I can capture the Click event of any other button, I'm unable to capture a Click event of the MessageBox.
My code is as follows:
var FindDialogButton = appElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "OK"));
if (FindDialogButton != null)
{
if (FindDialogButton.GetSupportedPatterns().Any(p => p.Equals(InvokePattern.Pattern)))
{
Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, FindDialogButton, TreeScope.Element, new AutomationEventHandler(DialogHandler));
}
}
private void DialogHandler(object sender, AutomationEventArgs e)
{
MessageBox.Show("Dialog Button clicked at : " + DateTime.Now);
}
EDIT:
My Complete code is as follows:
private void DialogButtonHandle()
{
AutomationElement rootElement = AutomationElement.RootElement;
if (rootElement != null)
{
System.Windows.Automation.Condition condition = new PropertyCondition
(AutomationElement.NameProperty, "Windows Application"); //This part gets the handle of the Windows application that has the MessageBox
AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);
var FindDialogButton = appElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "OK")); // This part gets the handle of the button inside the messagebox
if (FindDialogButton != null)
{
if (FindDialogButton.GetSupportedPatterns().Any(p => p.Equals(InvokePattern.Pattern)))
{
Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, FindDialogButton, TreeScope.Element, new AutomationEventHandler(DialogHandler)); //Here I am trying to catch the click of "OK" button inside the MessageBox
}
}
}
}
private void DialogHandler(object sender, AutomationEventArgs e)
{
//On Button click I am trying to display a message that the button has been clicked
MessageBox.Show("MessageBox Button Clicked");
}
I tried to keep this procedure as generic as possible, so that it will work whether the application you're watching is already running when your app is started or not.
You just need to provide the watched Application's Process Name or its Main Window Title to let the procedure identify this application.
Use one of these Fields and the corresponding Enumerator:
private string appProcessName = "theAppProcessName"; and
FindWindowMethod.ProcessName
// Or
private string appWindowTitle = "theAppMainWindowTitle"; and
FindWindowMethod.Caption
passing these values to the procedure that starts the watcher, e.g., :
StartAppWatcher(appProcessName, FindWindowMethod.ProcessName);
As you can see - since you tagged your question as winforms - this is a complete Form (named frmWindowWatcher) that contains all the logic required to perform this task.
How does it work:
When you start frmWindowWatcher, the procedure verifies whether the watched application (here, identified using its Process name, but you can change the method, as already described), is already running.
If it is, it initializes a support class, ElementWindow, which will contain some informations about the watched application.
I added this support class in case you need to perform some actions if the watched application is already running (in this case, the ElementWindow windowElement Field won't be null when the StartAppWatcher() method is called). These informations may also be useful in other cases.
When a new Windows is opened in the System, the procedure verifies whether this Window belongs to the watched application. If it does, the Process ID will be the same. If the Windows is a MessageBox (identified using its standard ClassName: #32770) and it belongs to the watched Application, an AutomationEventHandler is attached to the child OK Button.
Here, I'm using a Delegate: AutomationEventHandler DialogButtonHandler for the handler and an instance Field (AutomationElement msgBoxButton) for the Button Element, because these references are needed to remove the Button Click Handler when the MessageBox is closed.
When the MessageBox's OK Button is clicked, the MessageBoxButtonHandler method is called. Here, you can determine which action to take at this point.
When the frmWindowWatcher Form is closed, all Automation Handlers are removed, calling the Automation.RemoveAllEventHandlers() method, to provide a final clean up and prevent your app from leaking resources.
using System.Diagnostics;
using System.Linq;
using System.Windows.Automation;
using System.Windows.Forms;
public partial class frmWindowWatcher : Form
{
AutomationEventHandler DialogButtonHandler = null;
AutomationElement msgBoxButton = null;
ElementWindow windowElement = null;
int currentProcessId = 0;
private string appProcessName = "theAppProcessName";
//private string appWindowTitle = "theAppMainWindowTitle";
public enum FindWindowMethod
{
ProcessName,
Caption
}
public frmWindowWatcher()
{
InitializeComponent();
using (var proc = Process.GetCurrentProcess()) {
currentProcessId = proc.Id;
}
// Identify the application by its Process name...
StartAppWatcher(appProcessName, FindWindowMethod.ProcessName);
// ... or by its main Window Title
//StartAppWatcher(appWindowTitle, FindWindowMethod.Caption);
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
Automation.RemoveAllEventHandlers();
base.OnFormClosed(e);
}
private void StartAppWatcher(string elementName, FindWindowMethod method)
{
windowElement = GetAppElement(elementName, method);
// (...)
// You may want to perform some actions if the watched application is already running when you start your app
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement,
TreeScope.Subtree, (elm, e) => {
AutomationElement element = elm as AutomationElement;
try
{
if (element == null || element.Current.ProcessId == currentProcessId) return;
if (windowElement == null) windowElement = GetAppElement(elementName, method);
if (windowElement == null || windowElement.ProcessId != element.Current.ProcessId) return;
// If the Window is a MessageBox generated by the watched app, attach the handler
if (element.Current.ClassName == "#32770")
{
msgBoxButton = element.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "OK"));
if (msgBoxButton != null && msgBoxButton.GetSupportedPatterns().Any(p => p.Equals(InvokePattern.Pattern)))
{
Automation.AddAutomationEventHandler(
InvokePattern.InvokedEvent, msgBoxButton, TreeScope.Element,
DialogButtonHandler = new AutomationEventHandler(MessageBoxButtonHandler));
}
}
}
catch (ElementNotAvailableException) {
// Ignore: this exception may be raised if you show a modal dialog,
// in your own app, that blocks the execution. When the dialog is closed,
// AutomationElement element is no longer available
}
});
Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, AutomationElement.RootElement,
TreeScope.Subtree, (elm, e) => {
AutomationElement element = elm as AutomationElement;
if (element == null || element.Current.ProcessId == currentProcessId || windowElement == null) return;
if (windowElement.ProcessId == element.Current.ProcessId) {
if (windowElement.MainWindowTitle == element.Current.Name) {
windowElement = null;
}
}
});
}
private void MessageBoxButtonHandler(object sender, AutomationEventArgs e)
{
Console.WriteLine("Dialog Button clicked at : " + DateTime.Now.ToString());
// (...)
// Remove the handler after, since the next MessageBox needs a new handler.
Automation.RemoveAutomationEventHandler(e.EventId, msgBoxButton, DialogButtonHandler);
}
private ElementWindow GetAppElement(string elementName, FindWindowMethod method)
{
Process proc = null;
try {
switch (method) {
case FindWindowMethod.ProcessName:
proc = Process.GetProcessesByName(elementName).FirstOrDefault();
break;
case FindWindowMethod.Caption:
proc = Process.GetProcesses().FirstOrDefault(p => p.MainWindowTitle == elementName);
break;
}
return CreateElementWindow(proc);
}
finally {
proc?.Dispose();
}
}
private ElementWindow CreateElementWindow(Process process) =>
process == null ? null : new ElementWindow(process.ProcessName) {
MainWindowTitle = process.MainWindowTitle,
MainWindowHandle = process.MainWindowHandle,
ProcessId = process.Id
};
}
Support class, used to store informations on the watched application:
It's initialized using the App's Process Name:
public ElementWindow(string processName)
but of course you can change it as required, using the Window Title as described before, or even remove the initialization's argument if you prefer (the class just need to not be null when the watched Application has been detected and identified).
using System.Collections.Generic;
public class ElementWindow
{
public ElementWindow(string processName) => this.ProcessName = processName;
public string ProcessName { get; set; }
public string MainWindowTitle { get; set; }
public int ProcessId { get; set; }
public IntPtr MainWindowHandle { get; set; }
}

disable browser close button in asp.net application?

I am doing online Exam application using asp.net in this i have to disable the titlebar so that the user has no option to exit with in the time period.So please help with this one
Its not good practice to force user to stay on the page if they don't wish to, but you can have some work around if you want to confirm the close event before they leave the browser tab
function internalHandler(e) {
return "Please don't leave the page you can be fail in exams!";
}
if (window.addEventListener) {
window.addEventListener('beforeunload', internalHandler, true);
} else if (window.attachEvent) {
window.attachEvent('onbeforeunload', internalHandler);
}
If you prevent user to close it any way you don't have control over ALT + F4 or closes it from Task Manager
you can do it using javascript like this
var message = "You have not completed exam. Are you sure you want to leave?";
window.onbeforeunload = function(event) {
var e = e || window.event;
if (e) {
e.returnValue = message;
}
return message;
};
and you can unload it when user finish the exam
window.onbeforeunload = null;
or you can create your own browser application using c# windows forms. where you can set this custom option without having close button. You load your web application form in windows forms application easily.
onbeforeunload & onunload will help you out. You can't disable but you can show user an alert.
var showMsgTimer;
window.onbeforeunload = function(evt) {
var message = 'Don't Discard';
showMsgTimer = window.setTimeout(showMessage, 500);
evt = evt || window.evt;
evt.returnValue = message;
return message;
}
window.onunload = function () {
clearTimeout(showMsgTimer);
}
function showMessage() {
alert("You're Right!");
}
If this is not the one you expect. Then please try https://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/

"Only a single ContentDialog can be open at any time." error while opening another contentdialog

I am using Windows.UI.Xaml.Controls.ContentDialog to show a confirmation. And based on the response from the first dialog I would (or would not) show another dialog. But, when I am trying to open the second content dialog it throws : "Only a single ContentDialog can be open at any time." error. Even though in the UI, first dialog would be closed but somehow I am still not able to open the second dialog. Any idea?
I have created some code to handle this type of conundrum in my Apps:
public static class ContentDialogMaker
{
public static async void CreateContentDialog(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
public static async Task CreateContentDialogAsync(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
static async Task CreateDialog(ContentDialog Dialog, bool awaitPreviousDialog)
{
if (ActiveDialog != null)
{
if (awaitPreviousDialog)
{
await DialogAwaiter.Task;
DialogAwaiter = new TaskCompletionSource<bool>();
}
else ActiveDialog.Hide();
}
ActiveDialog = Dialog;
ActiveDialog.Closed += ActiveDialog_Closed;
await ActiveDialog.ShowAsync();
ActiveDialog.Closed -= ActiveDialog_Closed;
}
public static ContentDialog ActiveDialog;
static TaskCompletionSource<bool> DialogAwaiter = new TaskCompletionSource<bool>();
private static void ActiveDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args) { DialogAwaiter.SetResult(true); }
}
To use these Methods, you need to create the ContentDialog and its content in a variable, then pass the variable, and bool into the Method.
Use CreateContentDialogAsync(), if you require a callback in your app code, say if you have a button in your Dialog, and you want wait for a button press, and then get the value from the form in code after the dialog.
Use CreateContentDialog(), if you don't need to wait for the Dialog to complete in your UI Code.
Use awaitPreviousDialog to wait for the previous dialog to complete before showing the next Dialog, or set false, to remove the previous Dialog, then show the next Dialog, say, if you want to show an Error Box, or the next Dialog is more important.
Example:
await ContentDialogMaker.CreateContentDialogAsync(new ContentDialog
{
Title = "Warning",
Content = new TextBlock
{
Text = "Roaming Appdata Quota has been reached, if you are seeing this please let me know via feedback and bug reporting, this means that any further changes to data will not be synced across devices.",
TextWrapping = TextWrapping.Wrap
},
PrimaryButtonText = "OK"
}, awaitPreviousDialog: true);
William Bradley's approach above is good. Just to polish it up a bit, here is an extension method to submit and await the showing of a content dialog; the dialog will be shown after all the other content dialogs that have already been submitted. Note: by the time the user clicks through earlier backlogged dialogs you may no longer want to show the dialog that you have submitted; to indicate this you may pass a predicate that will be tested after the other dialogs have been dismissed.
static public class ContentDialogExtensions
{
static public async Task<ContentDialogResult> EnqueueAndShowIfAsync( this ContentDialog contentDialog, Func<bool> predicate = null)
{
TaskCompletionSource<Null> currentDialogCompletion = new TaskCompletionSource<Null>();
TaskCompletionSource<Null> previousDialogCompletion = null;
// No locking needed since we are always on the UI thread.
if (!CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess) { throw new NotSupportedException("Can only show dialog from UI thread."); }
previousDialogCompletion = ContentDialogExtensions.PreviousDialogCompletion;
ContentDialogExtensions.PreviousDialogCompletion = currentDialogCompletion;
if (previousDialogCompletion != null) {
await previousDialogCompletion.Task;
}
var whichButtonWasPressed = ContentDialogResult.None;
if (predicate == null || predicate()) {
whichButtonWasPressed = await contentDialog.ShowAsync();
}
currentDialogCompletion.SetResult(null);
return whichButtonWasPressed;
}
static private TaskCompletionSource<Null> PreviousDialogCompletion = null;
}
Another way might be to use a SemaphoreSlim(1,1).
"Only a single ContentDialog can be open at a time"
This statement is not entirely true. You can only ShowAsync one ContentDialog at a time. All you need to do is hide the current ContentDialog before opening another one. Then, after the "await ShowAsync" of the second ContentDailog, your simply call "var T = this.ShowAync()" to unhide it. Example:
public sealed partial class MyDialog2 : ContentDialog
{
...
}
public sealed partial class MyDialog1 : ContentDialog
{
...
private async void Button1_Click(object sender, RoutedEventArgs e)
{
// Hide MyDialog1
this.Hide();
// Show MyDialog2 from MyDialog1
var C = new MyDialog2();
await C.ShowAsync();
// Unhide MyDialog1
var T = ShowAsync();
}
}
I know this is slightly old, but one simpler solution instead of going through all this pain is to just register a callback for the ContentDialog_Closed event. By this point you can be sure the previous dialog has been closed, and can open your next dialog. :)
Only a single ContentDialog can be open at any time.
That is a fact. (I was really surprised, but just for a moment)
You can't have more than one at any time and it is more like guideline from Microsoft, because it's really messy to have multiple dialogs on top of each other filled with content.
Try to change your UX to display only one sophisticated ContentDialog and for all other messages use MessageDialog - it supports multiple buttons(only two for phones, but more on desktop) for user response but without Checkboxes or similar "smart"-content stuff.
In my case MessageDialogs were really helpful, but in some areas I used chained ContentDialogs but for that you must await the first one, and open second right after without any exceptions. In your case it seems like ContentDialog was not fully closed when you tried to open next one.
Hope it helps!
I like this answer https://stackoverflow.com/a/47986634/942855, this will allow us ot handle binding all events.
So extended it a little to check the multiple calls to show dialog.
private int _dialogDisplayCount;
private async void Logout_OnClick(object sender, RoutedEventArgs e)
{
try
{
_dialogDisplayCount++;
ContentDialog noWifiDialog = new ContentDialog
{
Title = "Logout",
Content = "Are you sure, you want to Logout?",
PrimaryButtonText = "Yes",
CloseButtonText = "No"
};
noWifiDialog.PrimaryButtonClick += ContentDialog_PrimaryButtonClick;
//await noWifiDialog.ShowAsync();
await noWifiDialog.EnqueueAndShowIfAsync(() => _dialogDisplayCount);
}
catch (Exception exception)
{
_rootPage.NotifyUser(exception.ToString(), NotifyType.DebugErrorMessage);
}
finally
{
_dialogDisplayCount = 0;
}
}
modified predicate
public class Null { private Null() { } }
public static class ContentDialogExtensions
{
public static async Task<ContentDialogResult> EnqueueAndShowIfAsync(this ContentDialog contentDialog, Func<int> predicate = null)
{
TaskCompletionSource<Null> currentDialogCompletion = new TaskCompletionSource<Null>();
// No locking needed since we are always on the UI thread.
if (!CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess) { throw new NotSupportedException("Can only show dialog from UI thread."); }
var previousDialogCompletion = _previousDialogCompletion;
_previousDialogCompletion = currentDialogCompletion;
if (previousDialogCompletion != null)
{
await previousDialogCompletion.Task;
}
var whichButtonWasPressed = ContentDialogResult.None;
if (predicate == null || predicate() <=1)
{
whichButtonWasPressed = await contentDialog.ShowAsync();
}
currentDialogCompletion.SetResult(null);
return whichButtonWasPressed;
}
private static TaskCompletionSource<Null> _previousDialogCompletion;
}

Close child windows from postbacked parent window

I need to close child windows which has been loaded by a parent window.
The child windows are opened by using window.open() method.
I need to close this child windows by clicking a logout button or close button which is in parent window.
My code:
var childWin = [];
//child window open event
function child_open(url)
{
childWin[childWin.length] = window.open(url);
}
//a logout button or close button event
function parent_close()
{
for (i=0; i<childWin.length; i++)
{
if (childWin[i] == null) return false;
childWin[i].close();
}
window.close();
}
This code is OK if the parent window don't postback to server.
When a postback occured in parent window,the value of variable(childWin) disappeared and I can't close child windows by this code.
Problem is - want to close child windows even the parent postbacked.
Is there a solution for this?
Thanks for all of your interests and replies.
Your array childWin will be cleared each time the page is loaded. So after post back there will be nothing in the array. Thats why the child windows are not getting closed.
A work around is mentioned here
Try something like this [not tested, and not sure it will work , just give a try :) ]
Parent Window (all pages)
var childStatus = {};
Child Window
var timerHandler,
windowName = window.name,
popupHandle = "";
funciton ChildCallBack()
{
try
{
if(popupHandle == "" || popupHandle == null)
{
popupHandle = window.opener.childStatus[windowName];
//ChildCallBack(); // no need of ChilCallBack here, since we already have timer
}
else
{
window.opener.childStatus[windowName] = popupHandle;
}
}
catch(e)
{
}
}
timerHandler = window.setInterval(ChildCallBack, 500);
function window_onclose()
{
try
{
window.clearInterval(timerHandler);
window.opener.childStatus[windowName] = null;
}
catch(e)
{
}
}
window.onclose = window_onclose;
Your Child window open function
//child window open event
function child_open(url)
{
var winHandle = window.open(url, "GIVE SOME UNIQUE NAME FOR EACH WINDOW HERE");
winHandle.popupHandle = winHandle;
}
Your close button event
//a logout button or close button event
function parent_close()
{
for (var key in childStatus)
{
if (childStatus[key] != null)
{
childStatus[key].close();
}
}
window.close();
}
Possible fix for query >> But, after a postback of child ,an error occurs in parent_close() (the value in childStatus[key] is not object and it can not do childStatus[key].close())
Replace
timerHandler = window.setInterval(ChildCallBack, 100);
with
if(popupHandle == "" || popupHandle == null)
{
// get the popupHandle from parent window
popupHandle = window.opener.childStatus[windowName];
timerHandler = window.setInterval(ChildCallBack, 100);
}

C# WatiN - Add an AlertDialogHandler to click ok button on every Alert dialog window

Hello
Those who have used WatiN likely also used DialogHandlers.
Well can someone teach me how can i assign a DialogHandler that will handle any Alert Box window.alert(), of a specific IE instance under WatiN control .
The DialogHandler only has to click in the OK button for very alert dialog box, in that case i think we need an AlertDialogHandler that basically only has to click the OK button.
AlertDialogHandler.OKButton.Click()
I've search the web and found a few examples.. But they work for a small period of time or the time you specify, i need one that will work forever, until i choose to stop it by clicking a button.
This as been bugging my head for hours, any help is appreciated. Thanks.
Note: Sometimes the alert dialog window has two buttons. Thats why i really need to click the OK button, not just Close the dialog window.
Create class:
public class OKDialogHandler : BaseDialogHandler
{
public override bool HandleDialog(Window window)
{
var button = GetOKButton(window);
if (button != null)
{
button.Click();
return true;
}
else
{
return false;
}
}
public override bool CanHandleDialog(Window window)
{
return GetOKButton(window) != null;
}
private WinButton GetOKButton(Window window)
{
var windowButton = new WindowsEnumerator().GetChildWindows(window.Hwnd, w => w.ClassName == "Button" && new WinButton(w.Hwnd).Title == "OK").FirstOrDefault();
if (windowButton == null)
return null;
else
return new WinButton(windowButton.Hwnd);
}
}
After creating instance of IE, attach dialog handler to it:
ie.AddDialogHandler(new OKDialogHandler());
This dialog handler will handle all windows, that contains a button with "OK" caption, by clicking on that button.

Categories

Resources