When I create a form (window) in PowerShell, I can usually display the form using .ShowDialog():
$form = New-Object System.Windows.Forms.Form
$form.ShowDialog()
.Visible is set to False before and after .ShowDialog().
But when I do a .Show() nothing is displayed on the screen:
$form.Show()
And .Visible is now set to True (presumably because .Show() made the form officially visible.)
When I now try to .ShowDialog() the form again, I get the following error message:
"Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog."
But when I follow the instructions to .ShowDialog() again
$form.Visible=0
$form.ShowDialog()
the result is that nothing is displayed on the screen and PowerShell hangs and cannot recover (ctrl-c doesn't seem to work). I assume this is because the form is being displayed modally somewhere where I cannot see it (or tab to it). But why?
The coordinates of the form haven't changed. So how does the form decide when it is physically visible and when it isn't?
Avoid using Show() from PowerShell as it requires a message pump and that isn't something the PowerShell console provides on the thread that creates your form. ShowDialog() works because the OS does the message pumping during this modal call. Creating the form and calling ShowDialog() works reliably for me.
My problem: When using ShowDialog() as part of a powershell logon script, the first form window would not show and powershell would seem to freeze up on logon. Symptoms were simular to the original post.
Solution I found: Instead of using $form.showDialog(), use:
[System.Windows.Forms.Application]::Run($form)
Works great for me now, and only the first form in the series needed the change. All my other forms that come up afterwards in the script still use showDialog.
Related
I just discovered unit test projects in Visual Studio .NET and am using test methods to set up examples of global code I have developed.
Some global methods involve displaying reusable dialogs like a date-picker and input box. Trouble is, sometimes forms will display and sometimes they won't.
I thought it was related to modality, because I have a report preview form that can be shown modally or non-modally. When I show that non-modally, it does not display. When I show it modally, it does.
Trying my input box never works:
string input = "";
using (InputBox box = new InputBox(Title, Prompt, Default))
{
DialogResult result = box.ShowDialog();
input = box.txtInput.Text.Trim();
}
return input;
Execution stops at the "box.ShowDialog()" line, and at that point I can inspect box and see that its dimensions, location, and visibility are all properly configured -- yet I cannot see the form. I've got to cancel the test to stop everything.
I'd love to use a unit testing project to act as a playground and showcase of existing code, but it seems very limited if I can't display certain forms. I realize this is not really what unit testing is meant for, but I hoped I could build a fun little sandbox this way to help get my developers up to speed.
I finally had some consistent success (and lack thereof) based on a single form property: ShowInTaskbar.
When a form had that property set to true, such forms would NOT display from a unit test method. When that property is false, all forms display. So, here are the rules as far as I know them to make sure a form can display from a unit test:
The form should be created as a standard Windows Form item in the project.
The form should have its ShowInTaskbar property set to FALSE.
The form needs to be displayed modally (i.e. with ShowDialog()).
This has let me display and test all of my utility forms like date selectors and login screens.
I had the same issue. The dialog was blinking visible then disappearing. It was already TopMost, and I tried the other trick of setting it to visible then not visible before showdialog was called, but nothing worked.
Then I checked the other dialog that was working fine and it had ControlBox = true. I tried that, and it worked!
This is definitely applicable to the question.
I have PowerShell script that creates a WPF form from inserted XML code. This form will run on a kiosk station and has no window (so no maximize, minimize, or close buttons) and consumes the entire screen.
Even though the form itself has no window buttons, a person can still press the WIN key on a keyboard, get the task bar, right click on the form in the task bar and choose to minimize, maximize, or close for the form.
So, I set an action for close to lock the workstation (which is better option for this kiosk instead of preventing the form from closing at all) by doing this:
$form.Add_Closing({
$shell = New-Object -ComObject "Wscript.Shell"
$shell.Run("%windir%\System32\rundll32.exe user32.dll,LockWorkStation")
})
I would also like to do this for Minimize if possible, but can’t seem to figure out how.
FYI. I’m not intending this to be a security feature per-se, but more so something that is better done then not for this kiosk station.
if $form is your form, then you could try this (wrapping the repetitive part in a function to stay DRY):
function Lock-Workstation {
$shell = New-Object -ComObject "Wscript.Shell"
$shell.Run("%windir%\System32\rundll32.exe user32.dll,LockWorkStation")
}
$form.Add_Closing({ Lock-Workstation })
$form.Add_StateChanged({
if($form.WindowState -eq "Minimized") {
Lock-Workstation
}
})
Since you don't want the window to be resized anyway, you could simply use:
$form.Add_StateChanged({ Lock-Workstation })
Simply I would like one form to open on top of another (automatically).I've searched the net and found nothing that helps or works.
In some more details - When opening the database, the system automatically loads up "Form1", which looks very nice in the middle of not only my screen, but others as well, but I would like to hide all the visible sign's of access. Yes I can hide the tool bars etc, but what I was looking for was to create a form "Background", load that automatically and then load up "Form1" on top.
For some reason I can't seem to get form1 to load so it's visible. Yes it loads, but under the background form. I've tried all settings of popup, modal, visible, onload, onopen. I've set the database to open minimized (but that does not hide the fact that it is access).
Please can anyone come up with a solution.
Just to clear things up:
You want a hidden form (background) that loads another form that is visible?
All this needs to be done in a windows form application (c#)?
Is this what you want?
You could try VBA.
This is how a form is loaded.
Private Sub Form1_Load()
OpenForm(FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs)
End Sub
I don't have access to ms-Access now, but I believe this should work.
These are parameters you should/can fill in:
FormName, View, FilterName, WhereCondition, DataMode, WindowMode & OpenArgs
More info: https://msdn.microsoft.com/en-us/library/office/ff820845.aspx
I have a form that has a WebBrowser control. Onload, it navigates to some URL.
On DocumentCompleted event handler, after getting the needed data the form is closed. This works most of the time, but sometime it pops up an IE window w/ the same URL after the form has already closed.
I noticed that in DebugView, it logs m_useSurfacePresenter 1. When this gets logged before the Close() is called, there is no popup. But when this gets logged after the Close() is called, then the popup appears.
You need to check the JavaScript .onunload event in that document, if it is set to open a new window (browser) when that page is unloading, then it will be triggered when you close your winform which is hosting the webbrowser control, and in turn, the page.
So, when you close the winform, the page's onUnload event in the webbrowser control will also be triggered as a result due to the way the events are chained in a Document/Page --> WB Control --> WinForms Form setup.
You can actually set this to nothing, by running a particular JS command directed to the page in the WB control, you can set the .OnUnload event to "function () {};", which will ensure whatever it is set to will be turned into Nothing/Null. Here is a code example:
window.onunload = function () { };
Let me know how it goes, and I will help you out if that doesn't fix it. Notice the above is JS, and there are ways to execute JS directly from teh WB Control, many examples on the web, there are actually 2 ways, one using .execScript and another one using the newer method, which is .InvokeScript.
I have a VB6 form with buttons with the text 'Continue' and 'Cancel'. I want to check which one was clicked. In C# every form has a dialog result and I could set it before exiting the form depending on which button was clicked. I don't see this in VB6.
Is there a dialog result? If not what is the best practice for checking the dialog result?
To simulate the .net WinForms behaviour, you will need a helper function in your form's code:
Public Function ShowDialog() As VbMsgBoxResult
Me.Show vbModal
ShowDialog = Iif(Cancelled, vbCancel, vbOk)
Unload Me
End Function
The form level Cancelled variable can be set by the button event functions before calling .Hide() or .Close(), or you could have a variable containing the result code directly.
In VB6 a dialog generally returns an integer value, which may correspond to vbYes, vbNo, vbCancel, etc.
See this article for details: http://www.vb6.us/tutorials/understanding-msgbox-command-visual-basic
http://www.code-vb.com/fragments/Dialogs.htm#Msgbox OK-Cancel
You'll have to specify it on your form if you've created the form yourself.
The last answer in this post has a hint that may help: http://www.xtremevbtalk.com/archive/index.php/t-306663.html