How to check if ANY ContentDialog is open? - c#

So we can only have one opened content dialog at a time. This is fine. But in my app there are several possible content dialogs that can be opened, and I would like to avoid making my own variable because I can forget to add it somewhere and the whole app will crash (because trying to open second content dialog throws exception).
So my question is: How to check if any ContentDialog is open?
Note:
I don't want to check for specific ContentDialog.
I would like to avoid creating my own variables.

ContentDialog is shown in the PopupRoot so using VisualTreeHelper.GetOpenPopups() will help you get it.
var openedpopups = VisualTreeHelper.GetOpenPopups(Window.Current);
foreach (var popup in openedpopups)
{
if(popup.Child is ContentDialog)
{
//some content dialog is open.
}
}

Tested accepted answer (by Vignesh) on target Windows 10 build 18362 and find that ContentDialog is never a child of popup. In my case simple check of the count works best:
protected bool IsAnyContentDialogOpen()
{
return VisualTreeHelper.GetOpenPopups(Window.Current).Count > 0;
}
Please feel free to comment if there's any problems with this approach. Thanks.

Related

Why does my function to only open one Window not work?

so I want in my Project to only open 1 instance of my Window. So I gave the Window a Title and tried to track every opening with that:
foreach (Window window in Application.Current.Windows)
{
if (window.Title == "QUALI-NET")
{
temp++;
}
}
and then i wanted to call my Function when this if statement is true:
if (temp == 1)
I have build this 2 in an extra Class and have an Switch Case around this. Above the Switch Case I initiliaze this:
QualiWindow WPFQuali = new QualiWindow(Mandant, Data.GetValue<string>("Artikelnummer"));
But The problem when I Open one Window and then open and another Window then it wont open but when I close the first started Window, I cant open the Window ever Again? I just want to allow one Instance of this Window to open. What am Im doing wrong?
I already tried the solutions from here:
How can I make sure only one WPF Window is open at a time?
But none of that is working. Is there a Way to get every opened Window from taskbar or something and just allow one Window with that name XY. to open
If you're keeping a count of the open windows with that title you also need to decrement the count whenever you close the window.

Disabling Dialog Flashing C#

I have a populated ListView dialog. If a user clicks on an option in the ListView, a new dialog is shown above the ListView.
My problems is that when I click off of the new top-most dialog (onto the ListView behind it), the new dialog's borders flash/blink several times. The icon on the taskbar also flashes. I wish to disable the flashing, but cannot find a property to change.
To show my dialog, I use the following code:
if (detail == null)
detail = new Details(opt, val, user, desc, m_l);
else
detail = null;
detail.ShowDialog();
This is intended behavior, it's because the new dialog is modal. It's drawing attention to the fact that something needs to be done.
If you need to make a non-modal form, instead of using ShowDialog(), simply use Show().
Sounds like to me you are creating modal windows each time. And you cannot resume the previous dialogs until you dismiss your new top-most window.
Take a look at this wikipedia article for information about modal dialogs.
I would advise you look at how you are creating/showing your windows.
In WPF you show windows via Show() or ShowDialog(), however, I do not know which type of ListView you are using
EDIT:
Per your comment, you want modal dialogs. The only ways I can think of even trying to remove the flashing is going into WINAPI. This doesn't seem like a job for .NET.
I want to suggest a few things:
Take a look at options for showing each window. See this MSDN page
Take a look at the options for styling each window. See this MSDN page
Reconsider your design. I know this may take a lot of work, but having so many layers of windows is kind of unappealing to most users. Ultimately, I believe this option will make your application the best.
Thank you all for your answers and guidance. I have found the best way to handle my problem.
I was using an event ItemActivated. This event was called when an a highlighted item on the ListView was clicked. This became a problem when the user would double click on an already selected item. This would cause the new dialog to show, but also flash several times.
By using the DoubleClick event instead, a single click on a selected object does nothing. A double click on either a selected or non-selected item opens the dialog without the flashes. The flashes still appear if you try to click off of the dialog box, but are not as much of an issue.

Custom dialog box in wpf issue

I'm calling my custom dialog window with this code:
GUI.SLDialog sd = new GUI.SLDialog();
if (sd.ShowDialog() == false)
{
return;
}
But sd.ShowDialog() always returns nothing (i think), because the function breaks, but the waypoint at return; isn't reached.
Dialog is automaticly closing when I add to button:
this.DialogResult = false;//or true
Anybody know what am I doing wrong?
Thanks in advance for your help.C.H.
#edit
This is my SLDialog:
xaml: http://wklej.org/hash/9fb67fb0c7c/
cs: http://wklej.org/hash/16e3ccc6c0d/
I don't think I can tell you much here unless you post the code for the dialog but I do have a suggestion in the mean time.
Since you're already unhappy with the standard dialog boxes and customization is clearly an option why not move towards what people are coming to expect? Instead of your standard dialog why not just create a user control that lays over the rest of your UI and blurs everything out from the background? Much like a jquery dialog box you might see on a web page.
Modality is easier to control since it's just a matter of covering your entire app window with a translucent rectangle and then make the dialog window appear however you want.
Just a suggestion.

C# WinForms Wait on form to proceed

I'm tired and hungry, so I might of missed it, but from what I can see no existing post covers this...
I'm writing a plugin for an application. My plugin loads a form to get some data specifically, it uses the webcam to scan for a barcode. Once it's found a barcode, the form hides itself (incase it's needed again later). This is how I currently call the form that does the barcode work:
string readData = null;
if (eye == null)
{
System.Windows.Forms.Application.EnableVisualStyles();
eye = new CamView();
}
eye.Show();
if (eye.found)
{
readData = eye.readData;
}
return readData;
So, my problem is that eye.show() doesn't block. It makes the form appear and carries right on before there's a chance for the barcode to appear. I imagine I need to use some form of threading or locking, but my crude attempts to do so have just frozen the interface completely.
The "eye" form is basically just a viewfinder for the webcam, and relies on the camera_OnImageCapture event to make it do it's image checks for the barcode.
Is there an elegant way to make the application calling the plugin wait for the form to finish? Or do I just need to add an accept button to the "eye form?"
Cheers. And humble apologies if this is in anyway a repost.
.ShowDialog();
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
"You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed."
You are on the right track. You change the code to show CamView as a modal dialog but do no add an Accept button. Instead change camera_OnImageCapture to close the dialog.

WatiN testing of a popup

I have an application that brings up a popup window when you click a link. I have a watin test that naviates to the page and clicks a link to open the popup. This is my current code:
[Test]
public void TestCommentBoxInput()
{
window.GoTo("mylocalurl");
window.Link(Find.ById("popuplink.aspx")).Click();
IE iepopup_1 = IE.AttachTo<IE>(Find.ByUrl("popuplinkurl.aspx"));
iepopup_1.TextField(Find.ById("txtComments")).TypeText("Commenttest");
}
As you can see I tried attatching the popup window to the created browser called window. When I run my test it just stops at the popup window and never enters text in the box. How do I go about making my program regonize that it is now to be operating on the popup and not the original window?
EDIT: I am dealing with a Modal Dialog.
I think the Find.ByUrl try to do a exact match, try with a Find.ByUrl(u => u.Contains("popuplinkurl.aspx"))
So I have figured out the problem, the problem was I was using a Modal dialog and they are handled differently. My new code is as follows in case anyone is stuck in the same position I was in. :)
public void TestCommentBox()
{
window.GoTo("mylocalurl");
window.Link(Find.ById("popuplink.aspx")).ClickNoWait();
HtmlDialog dialog = window.HtmlDialog(Find.ByTitle("TestPopup"));
dialog.TextField(Find.ById("Txtcomments")).TypeText("Commmenttest!");
}
The important lines are:
window.Link(Find.ById("popuplink.aspx")).ClickNoWait();
Notice that I am using ClickNoWait() and not just Click, I am unsure as to why this makes the difference, but it does! If someone could explain that that would be great.
HtmlDialog dialog = window.HtmlDialog(Find.ByTitle("TestPopup"));
Because I am dealing with a Modal dialog you have to declare a new HtmlDialog. Also in order to use Html dialog make sure you include Watin.Core.DialogHandlers. I hope this is helpful to someone out there! :)

Categories