I have an application, ParentApp, which launches another application, ChildApp.
ParentApp displays a form while the ChildApp is running - just a kind of Childapp is currently running display with a Cancel button which kills ChildApp.
I want ParentApp to be unusable while ChildApp is running, so whenever someone clicks on ParentApp I want to bring the ChildApp to the foreground, which is fine.
So I've added this event handler to the ParentApp which responds to the Activated event of the form.
private void ParentAppForm_Activated(object sender, EventArgs e)
{
IntPtr childHwnd = _childApp.MainWindowHandle;
SetForegroundWindow(childHwnd );
}
(GotFocus didn't seem to work)
Unfortunately, if the user clicks the Cancel button on the ParentAppForm, the event handler for that button is never hit, because the Activated event fires first and sets the foreground window to another process.
Is there around this - to allow the button event to fire even though the application is not in the foreground?
As a quick workaround, you could check whether the mouse pointer in inside the area described by the Button.Bounds when the Form is activated.
You can translate the mouse pointer position to the Form.Bounds coordinates using PointToClient with the Cursor.Position coordinates.
Something like this:
private void ParentAppForm_Activated(object sender, EventArgs e)
{
if (this.button1.Bounds.Contains(this.PointToClient(Cursor.Position)))
ChildForm.Close(); //Or ChildApp.Kill()
else
ChildForm.BringToFront(); //Or SetForegroundWindow(ChildApp.Handle)
}
Related
We have some Forms that have no border / no toolbox (overlays)
Whenever the user clicks somewhere else in the underlaying window, the overlay-form is send to the background (regular non-modal-form behaviour)
Is there an Event for this, so the "overlay" could detect it's visibility change and close itself?
Maybe it can be handled within in resize / paint event, where the "invisibility" can be catched?
Background:
Typical "Select-Or-Create-New" UseCase. Clicking "plus" shows the tiny creation-form. Currently it's "topmost", so the user needs to hit "Escape" to get rid of it. (Else there would be a mess of "background-overlays", hence the question)
Would be more "userfriendly" if a click on something else closes that "tiny form":
You can use the Deactivate event of the form:
private void Form1_Deactivate(object sender, EventArgs e)
{
Visible = false;
}
Something strange is happening in my WinForms app (and with strange things it's normally my fault).
I've got a FormClosing event that will check whether any changes has been made on the form that has not been saved.
If the user confirm to save the changes, I will call the Save button's click event. However, even if I remove all the logic in the OnClick event, the form stays open, and I have to close it again before it closes.
If I skip the line buttonSave.PerformClick(); then the form closes properly.
Why will entering the click event, cancel the close? Any work around?
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
//Check if there changes were made on the form
//Prompt user to save
if (confirmSave)
buttonSave.PerformClick();
}
private void buttonSave_Click(object sender, EventArgs e)
{
//Everything in here commented out.
}
Just call the event instead of PerformClick.
PerformClick probably executes more code that makes the form stop closing. Like showing that the button is clicked, the sound etc.
So change PerformClick to buttonSave_Click. So it will just do the save function, without doing anything with the button itself.
I'm having a Windows Mobile CE application written in C# with .Net CF.
Consider i have 2 forms in my application:
List of objects (has a listview)
Details page (should appear when something is selected in previous listview)
Currently i'm attaching callback to listview's SelectedIndexChanged event, and open a new form there.
New form opens okay (in the midde of the event callback), but when i close the form(this.Close()), then the list page isn't clickable first time, after the first click UI is interactable again.
Also the the ListViewItem clicked at first step doesn't get selected(blue background).
Here's a short (12s) video showing this problem: http://take.ms/urkme
As you see from the video, after coming back from details screen, refresh button doesn't click on the first click..
I'm showing the details form like so:
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
(new FormDetails()).ShowDialog();
}
Is there any way to show the details form after event finishes, or am i doing it completely wrong?
PS! Tried the same with a Button and it's click event, then all worked nicely..
As I know ListView's SelectedIndexChanged event fired twice on almost case not like Button's Click event which fired once, this maybe what cause that weird interaction. Maybe try changing to ItemSelectionChanged event as suggested in here.
Your problem is caused by using the SelectedIndexChanged event. When you select an item in your list you'll set the SelectedIndex, if you select the same item again the index won't be changed so you'll never call the event.
You could use the Click event to trigger the wanted response.
Here is an example:
private void listView1_Click(object sender, EventArgs e)
{
var firstSelectedItem = listView1.SelectedItems[0]; //This is your selected item.
//do stuff
}
When you use ShowDialog() you open a form in modal mode. All further processing of following code will not take place until the modal form is closed or returns a DialogResult.
Better use a modeless form using .Show().
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
(new FormDetails()).Show(); //will not stop processing of events in mainForm
}
If the new FormDetails is finished, it can use a simple Hide or Close to bring up the main form to foreground.
Remember that the main form is still there and will not wait for the FormDetails being closed (as it is a modeless dialog).
In my PowerPoint Add-in using VSTO, I am implementing an Application-level Mouse Hook to capture mouse events like double click, right click, mouse over etc, by using MouseKeyboardActivityMonitor.dll downloaded from Codeplex. The reason I am doing this is because PowerPoint doesn't have mouse related events to listen to and the ones it gives are not fired in edit-mode of the PowerPoint.
In my Add-in when user clicks a chart a menu appears which allows the user to perform various functions on the chart. All is working fine. I have captured mouse events and a customized menu is shown but the problem arises when the menu is closed after performing some action the default menu of PowerPoint appears on the screen.
Example: When user double clicks on the chart, I show my form menu like this.
//Listening to the MouseDoubleClick event
MyMouseHookListener.MouseDoubleClick += MyMouseHookListener_MouseDoubleClick;
//MouseDoubleClickEvent
void MyMouseHookListener_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
FormMenu.ShowDialog(); //Displaying menu
}
This works fine, but when the user closes the form the default double click menu of PowerPoint Charts appears. Same is the problem with other mouse events.
How can I disable PowerPoint's event menus?
Update:
There is a bool property named Cancel which is provided by PowerPoint's WindowBeforeDoubleClick and WindowBeforeRightClick events. Which if set to true cancels the default action that PowerPoint performs when an event is fired. How can i access this property in my MouseHook events?
There are several available events raised by the MouseKeyboardActivityMonitor. For a mouse down event you can choose to listen to either MouseDown or MouseDownExt. The latter supplies you with some extra options in the MouseEventExtArgs parameter, such as a Handled property. If you set this to true, the event will not propagate further.
When it comes to MouseDoubleClick event, there is no extended event available. I would therefore suggest that you implement the double click listener yourself by utilizing the MouseDownExt listener and counting the number of clicks that has occurred.
public void Initialize() {
// Initialize your listener and set up event listeners
MyMouseHookListener = new MouseHookListener(new AppHooker()) {Enabled = true};
MyMouseHookListener.MouseDownExt += MyMouseHookListenerOnMouseDownExt;
// UNDONE Delete when testing is done; included to show that the listener is never called
MyMouseHookListener.MouseDoubleClick += MyMouseHookListenerOnMouseDoubleClick;
}
private static void MyMouseHookListenerOnMouseDoubleClick(object sender, MouseEventArgs mouseEventArgs)
{
// NOTE: This listener should never be called
Debug.Print("Mouse double-click!");
}
private static void MyMouseHookListenerOnMouseDownExt(object sender, MouseEventExtArgs mouseEventExtArgs)
{
Debug.Print("Mouse down. Number of clicks: {0}", mouseEventExtArgs.Clicks);
if (mouseEventExtArgs.Clicks == 2)
{
// TODO Insert your double-click code here
mouseEventExtArgs.Handled = true;
}
}
To be exhaustive, you should perhaps also test whether it is the left or right button that has been clicked, which you can to by checking the MouseEventExtArgs.Button property. A right-double-click will now be counted as a double-click, however, this is as far as I know similar to the original MouseDoubleClick event.
Is it possible to prevent mouse events when a window is activated?
For example, I have a C# window and I change the focus to something else such as a browser. When I reactivate the c# window by clicking on it, I don't want any mouse events to be executed. The first click on the window should just activate it. Mouse Events are only fired if the window is already activated.
An event will be fired then a user clicks on your wpf controll but why don't you just handle the event?
Subscribe to the event and prevent it to bubble up the visual tree:
private void Panel_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
Have a look at this link it describes the event bubble and tunneling in WPF.