c++/cli best way to retrieve wpf window result - c#

I have a WPF window and I'm calling it from a C++/cli code and it works fine. What I need is what's the best way to intercept user action I mean whether he click OK or Cancel. Wath I think to do is to define a Boolean property in my window and set it depends on user action. Here is my code :
MyWindowView^ window = gcnew MyWindowView();
System::Windows::Interop::WindowInteropHelper^ windowInteropHelper = gcnew System::Windows::Interop::WindowInteropHelper(window);
windowInteropHelper->Owner = (IntPtr)AfxGetMainWnd()->m_hWnd;
window->WindowStartupLocation = System::Windows::WindowStartupLocation::CenterScreen;
window->ShowDialog();
if ()
{
//some action
}
else
{
//
}
Also I want to know do I need to delete the window object ?

Window.ShowDialog returns dialog result. I can't see any reason to get this result in some way, that differs from C# or VB .NET:
System::Nullable<System::Boolean> result = window->ShowDialog();
if (result.HasValue)
{
// OK or Cancel
if (result.Value)
{
// OK clicked
}
else
{
// Cancel clicked
}
}
else
{
// dialog closed via system menu or Alt+F4
}
do I need to delete the window object ?
No, you don't. See this answer.

Related

MVC in the controller decide to open a popup or continue to nextpage

I have the following scenario:
When the user click on button next, I have to do some validation that must be done in the controller.
If there is an error, it will show the error messages in a popup with a Yes to continue or No to cancel and close the popup.
But if there are no errors, user will continue to next page without showing a popup.
Here is what I have:
[ActionName(PA.FlowActionName), AcceptPost("nextAanvullen")]
[NoAsyncTimeout]
public ActionResult Pagina_nextAanvullen(OffrerenFlow flow)
{
var medewerkers = ((MedewerkersAanvullenPakket)(flow.CurrentBusinessObject)).Medewerkers;
//validate here
if (true) // for test use true
{
var blok = medewerkers.Deelnemers;
return View("DeelnemersFout", medewerkers); // This doesnt open a popup
}
else
{
// Go to next page
DoWorkEventHandler handler = (o, e) =>
{
AsyncManager.Parameters["navigation"] = Navigate(new NavigationData<OffrerenFlow>((OffrerenFlow)flow), PA.Next,
true);
};
ExecuteAsyncMethod(handler);
}
}
I am not getting an popup, the view just display the view "DeelnemersFout". I need it to be in a popup. Any suggestions are welkom
Assign a value in ViewBag inside IF block
if (true) // for test use true
{
ViewBag.ShowPopup = true;
var blok = medewerkers.Deelnemers;
return View("DeelnemersFout", medewerkers); // This doesnt open a popup
}
and on Cshtml capture this value in a javascript variable and check if there is a value in this variable or not, and show the popup accordingly.

Custom MessageBox Through Synchronization Context - Cannot Access Disposed Object

I am in a fairly odd situation here. I have created a custom MessageBox form (instead of the built-in MessageBox.Show). The below code is what I use to call up the form, when I need it:
internal DialogResult ShowCustomMessageBox(string message, string caption, Icon icon = null)
{
var result = DialogResult.None;
MainForm.Get.UISynchContext.Send(s =>
{
var messageBox = new DialogBox
{
Icon = icon,
Text = caption,
rtbInDialogBox = { Text = message }
};
result = messageBox.ShowDialog();
messageBox.Dispose();
}, null);
return result;
}
When I run this, I get an error message on messageBox.ShowDialog(); that the messageBox instance has already been disposed.
When I post the code to the synchronization context I was pretty sure MainForm would run the code itself (as opposed to other threads), and I am not sure why it tells me that the messageBox has already been disposed.
Any ideas?
I found the problem. Apparently you need to specify the owner of the dialog box: result = messageBox.ShowDialog(MainForm.Get);

Popup's in selenium webdrivers

So I'm working with selenium firefox webdrivers in c# winform and I have this code below to get the handle of the popup that shows when you click on the "webtraffic_popup_start_button" and it should get the handle of the popup but the popup handle is same as current one.
string current = driver.CurrentWindowHandle;
driver.FindElement(By.XPath("//*[#id='webtraffic_popup_start_button']")).Click();
Thread.Sleep(Sleep_Seconds);
popup = driver.CurrentWindowHandle;
Thread.Sleep(3000);
driver.SwitchTo().Window(current);
Thread.Sleep(1000);
Any help with this would be much appreciated thank you
This is what pop up looks like.
WebDriver does absolutely no tracking whatsoever to detect which window is actually in the foreground in the OS, and does no automatic switching when new browser windows are opened. That means the proper way to get the handle of a newly-opened popup window is a multi-step process. To do so, you would:
Save the currently-focused window handle into a variable so that you
can switch back to it later.
Get the list of currently opened window handles.
Perform the action that would cause the new window to appear.
Wait for the number of window handles to increase by 1.
Get the new list of window handles.
Find the new handle in the list of handles.
Switch to that new window.
In code using the .NET language bindings, that would look something like this:
string currentHandle = driver.CurrentWindowHandle;
ReadOnlyCollection<string> originalHandles = driver.WindowHandles;
// Cause the popup to appear
driver.FindElement(By.XPath("//*[#id='webtraffic_popup_start_button']")).Click();
// WebDriverWait.Until<T> waits until the delegate returns
// a non-null value for object types. We can leverage this
// behavior to return the popup window handle.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
string popupWindowHandle = wait.Until<string>((d) =>
{
string foundHandle = null;
// Subtract out the list of known handles. In the case of a single
// popup, the newHandles list will only have one value.
List<string> newHandles = driver.WindowHandles.Except(originalHandles).ToList();
if (newHandles.Count > 0)
{
foundHandle = newHandles[0];
}
return foundHandle;
});
driver.SwitchTo().Window(popupWindowHandle);
// Do whatever you need to on the popup browser, then...
driver.Close();
driver.SwitchTo().Window(currentHandle);
Alternatively, if you're using the .NET bindings, there's a PopupWindowFinder class in the WebDriver.Support assembly that is specifically designed to do these operations for you. Using that class is much simpler.
// Get the current window handle so you can switch back later.
string currentHandle = driver.CurrentWindowHandle;
// Find the element that triggers the popup when clicked on.
IWebElement element = driver.FindElement(By.XPath("//*[#id='webtraffic_popup_start_button']"));
// The Click method of the PopupWindowFinder class will click
// the desired element, wait for the popup to appear, and return
// the window handle to the popped-up browser window. Note that
// you still need to switch to the window to manipulate the page
// displayed by the popup window.
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popupWindowHandle = finder.Click(element);
driver.SwitchTo().Window(popupWindowHandle);
// Do whatever you need to on the popup browser, then...
driver.Close();
// Switch back to parent window
driver.SwitchTo().Window(currentHandle);
If the lastly opened window is your target then simply do the following after the click
driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());
EDIT
//You may need to go back to parent window to perform additional actions;
// to the new window
driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());
// to the new window
driver.SwitchTo().Window(driver.WindowHandles.ToList().First());
//or
driver.SwitchTo().DefaultContent();
I've got some code you might like. The quickest solution is to use Popup Finder, but I've made my own method as well. I would never rely on the order the Window Handles are in to select the appropriate window. Popup Window Finder:
PopupWindowFinder finder = new PopupWindowFinder(driver);
driver.SwitchTo().Window(newWin);
My Custom method. Basically you pass it the element you want to click, your webdriver, and optionally the time to wait before searching after you click the element.
It takes all of your current handles and makes a list. It uses that list to eliminate the previously existing windows from accidentally getting switched to. Then it clicks the element that launches the new window. There should always be some sort of a delay after the click, as nothing happens instantly. And then it makes a new list and compares that against the old one until it finds a new window or the loop expires. If it fails to find a new window it returns null, so if you have an iffy webelement that doesn't always work, you can do a null check to see if the switch worked.
public static string ClickAndSwitchWindow(IWebElement elementToBeClicked,
IWebDriver driver, int timer = 2000)
{
System.Collections.Generic.List<string> previousHandles = new
System.Collections.Generic.List<string>();
System.Collections.Generic.List<string> currentHandles = new
System.Collections.Generic.List<string>();
previousHandles.AddRange(driver.WindowHandles);
elementToBeClicked.Click();
Thread.Sleep(timer);
for (int i = 0; i < 20; i++)
{
currentHandles.Clear();
currentHandles.AddRange(driver.WindowHandles);
foreach (string s in previousHandles)
{
currentHandles.RemoveAll(p => p == s);
}
if (currentHandles.Count == 1)
{
driver.SwitchTo().Window(currentHandles[0]);
Thread.Sleep(100);
return currentHandles[0];
}
else
{
Thread.Sleep(500);
}
}
return null;
}

Triggering method dependent on action on second window

Please can someone tell me the technique that would be used, for the following scenario.
I would like to authenticate users, before I allow my code to perform another action.
I have a method that opens a new window that contains my authentication form (username and password).
private bool userLogin()
{
Window loginInterface = new Window()
{
Title = "Please Login",
Content = new login(),
Height = 282,
Width = 300,
ResizeMode = ResizeMode.NoResize,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
loginInterface.Owner = this;
loginInterface.ShowDialog();
return true;
}
I'm calling this method like so, on button click:
private void perform_action(object sender, RoutedEventArgs e)
{
if (!userLogin())
{
// Failed login, do nothing
}
else
{
// Authentication successful, perform action
delete_item();
}
}
The window opens fine, but how can I now make my method return true or false based on the what the user does on the opened form?
So when the user clicks the login button named login_button, my code already validates the credentials, but I need the 'bool' value sent back.
Can I make my first window almost wait for an action to be performed on another window and get the response back?
The Window.ShowDialog() method actually already returns a bool?. This can be set at any point from within the Window by setting (for example) this.DialogResult = true. You can then close the window and access the value from the calling code.
To close the window with a result:
this.DialogResult = true;
...and then to use that result in the calling code:
var myWindow = /*create window*/;
var result = myWindow.ShowDialog();
if (result == true)
{
//...
}
To close the login screen you can set DialogResult to true or false, and ShowDialog returns this value. For other things you can create events on the second window and subscribe to them on the first.
userLogin should return something other than true.
I would do something like this (based on the code shown):
return loginInterface.WasSuccessful; // you'd have to add this property

How to make a modal dialog and receive feedback from the child form (like folderbrowser control)

So I'm trying to make a folderbrowser for my custom application in C# and the folderbrowser thing is okay, but I want to set it up like how the default one acts.
According to #Kevin I am trying to make a modal dialog.
// I create the folderbrowser control and await the directory
string CreateFileDialog(bool allowFixedDrives)
{
FolderBrowser fb = new FolderBrowser(this, allowFixedDrives);
fb.Show();
//return THE_SELECTED_FOLDER_DIR;
return "";
}
So in the folderbrowser there is a boolean to allow fixed drives and also a reference to the parent form:
// Create the control and receive whether or not it should read fixed drives and also get a reference to the parent control.
public FolderBrowser(Form1 frm1, bool allowFixed)
{
InitializeComponent();
this.allowFixed = allowFixed;
frm1.Enabled = false;
}
I freeze the main form when this dialog is created. If the user closes the form, it will return null or "" and if the user presses okay, it should return the selected directory (where THE_SELECTED_FOLDER_DIR is).
Does anyone know how I can cleanly implement a dialog that sends feedback to the parent form?
Feel free to ask if you are as confused as I am :)
So after a rather interesting discussion with #Kevin, I've decided that the best way to go about this would be to call a public function in the form and then show it. Kinda hard to explain, so I'll show you:
NB: I think this should be kept open just in case someone has the same issues I did...
So I want to simply get a selected folder name and I'll display that selected folder on a text control for example:
bool invalid = false;
string value = CreateFileDialog(true, out invalid);
if (!invalid)
{
txt_File.Text = value;
}
else
{
MessageBox.Show("A folder was not selected.");
}
Okay, so I'm creating a dialog. I check if the dialog was closed without selecting a file and display a messagebox if it's invalid....
string CreateFileDialog(bool allowFixedDrives, out bool invalid)
{
FolderBrowser fb = new FolderBrowser(this, allowFixedDrives);
return fb.ShowForm(out invalid);
}
Now here is where it gets interesting. I create the dialog here and call a function on the form to actually show it, but I pass a boolean as an out variable to detect if it was closed without selecting a file.
And finally:
public string ShowForm(out bool a)
{
ShowDialog();
frm1.Enabled = true;
if (!Directory.Exists(selectedFolder))
{
a = true;
return selectedFolder;
}
else
{
a = false;
return selectedFolder;
}
}
I return the selected folder name if the user pressed OK, otherwise don't return it and also set the out parameter to false.
There you have it.
I'd like to thank everyone who pitched in here, namely #Kevin.

Categories