In my application I am using a popup in a page. When I press on back key, I need to close the popup if it is open and stay on the page. When I pressed on the back key, the popup not closed and the page goes to previous one. To close the popup now I use the following code in the back key press event.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (myPopup.IsOpen)
myPopup.IsOpen = false;
else if (NavigationService.CanGoBack)
NavigationService.GoBack();
}
When using this code, the popup will close but also goes to the previous page. I need to go back only if popup is in closed state. How can I do this?
You can also do the following:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (myPopup.IsOpen)
{
myPopup.IsOpen = false;
e.Cancel = true;
}
}
Problem Solved.
Instead of using
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
}
I used the back key press event of the page
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
if (myPopup.IsOpen)
myPopup.IsOpen = false;
else if (NavigationService.CanGoBack)
NavigationService.GoBack();
}
Related
I know how to subscribe to the CoreWebview2.DownloadStarting event and use handled = true to stop the Download dialog from showing while a download (i.e. an image) is being made, but the problem is the DownloadStarting event never fires if you right click on a web page and choose "Save as" or "Print > Save as PDF", even though the Download dialog will appear as if a regular download was being made. Does anyone know any workaround for this?
My code:
public Form1()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
if (webView != null)
webView.Dispose();
webView = new WebView2();
await webView.EnsureCoreWebView2Async();
webView.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
panel1.Controls.Add(webView);
webView.Dock = DockStyle.Fill;
webView.Source = new Uri("https://www.microsoft.com");
}
private void CoreWebView2_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
{
e.Handled = true;
}
I just found a solution:
subscribe to CoreWebView2.IsDefaultDownloadDialogOpenChanged event:
webView.CoreWebView2.IsDefaultDownloadDialogOpenChanged += webView_CoreWebView2_IsDefaultDownloadDialogOpenChanged;
Close the Download dialog if it's open:
private void webView_CoreWebView2_IsDefaultDownloadDialogOpenChanged(object sender, object e)
{
if (webView.CoreWebView2.IsDefaultDownloadDialogOpen) webView.CoreWebView2.CloseDefaultDownloadDialog();
}
I use a RepositoryItemSearchLookUpEdit.
I need to prevent SearchLookupEdit from Closing When PopupCloseMode is CloseUpKey.
private void repositoryItemLookUpEdit_CloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e)
{
if (e.CloseMode == PopupCloseMode.CloseUpKey)
{
//Prevent from Closing
}
}
What should I write Instead of //Prevent from Closing to Achieve this?
As a workaround, you can subscribe to QueryCloseUp event. And check previous pressed key:
private void repositoryItemLookUpEdit_QueryCloseUp(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
I am implementing an application, and if I click on a menu item:
<MenuItem Name="menuAlgemeneGeg" Header="Algemene gegevens" Click="AlgemeneGegevensClick" />
the method is :
private void AlgemeneGegevensClick(object sender, RoutedEventArgs e)
{
ToetsBeheerViewModel vm = (ToetsBeheerViewModel)this.DataContext;
EditAlgemeneGegevens window = new EditAlgemeneGegevens(vm.Examination);
window.ShowDialog();
menuAlgemeneGeg.IsEnabled = false;
}
Now what I want is, that when you click on it the menuitem is disabled. But from the moment the user closes this window it must be enabled again. I can disable the menuitem but can't change this back to enable it.
Someone who can help me out please?
you can handle window closed event.. and enable menu in even handler
private void AlgemeneGegevensClick(object sender, RoutedEventArgs e)
{
ToetsBeheerViewModel vm = (ToetsBeheerViewModel)this.DataContext;
EditAlgemeneGegevens window = new EditAlgemeneGegevens(vm.Examination);
window.Closed += new EventHandler(Window_Closed);
window.ShowDialog();
menuAlgemeneGeg.IsEnabled = false;
}
void Window_Closed(object sender, EventArgs e)
{
menuAlgemeneGeg.IsEnabled = true;
}
I'm having a issue with the following scenario on Windows Forms:
I created a Form with two buttons, each button have been assigned with the behaviour DialogResult OK and DialogResult Cancel, respectively.
But based on certain conditions, I need to stop the execution of the OK button. The problem is that if I just made a return like this:
private void btnOk_Click(object sender, EventArgs e)
{
foreach(Control control in tblTable.Controls)
{
if (control.GetType() == typeof(TextBox))
{
if (control.Text.Trim() == "")
{
control.Focus(); return;
}
}
else
{
}
}
}
The dialog result keeps returning the OK answer to the parent form.
I need to stop the execution of the event and not return any answer until the user corrects the info on the form. In other words, the user should be taken back to the form to correct any missing or wrong data.
As Hans Passant mentions in an comment, just set the DialogResult to None!
Like this:
private void btnOk_Click(object sender, EventArgs e)
{
if(ValidationFailed())
{
this.DialogResult = DialogResult.None;
return;
}
//...
}
Personally I wouldn't use DialogResults on buttons in this scenario. I only tend to set the DialogResult when there's only distinct options that do not require any additional logic (i.e. making a custom MessageBox).
What I would do is to just send the DialogResult yourself on success:
private void btnOk_Click(object sender, EventArgs e)
{
if (allIsOK())
{
this.DialogResult = DialogResult.OK;
}
}
Consider tapping into the Forms's Closing event, and use the Cancel property of the event args to cancel form closing.
Here's a web page that discusses the idea; it's VB, but you'll get the idea:
http://www.vbinfozine.com/t_wfdlg.shtml
In Case you have used DialogResult on a button in a child Form (Which is a dialog), and want to return to form with no dialog result use a function on the form_closing() event:
private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (notValidated)
{
e.Cancel = true;
}
}
I have to handle both the single click and the double click of a button in a WPF application with different reaction.
Unfortunately, on a doubleclick, WPF fires two click event and a double click event, so it's hard to handle this situation.
It tried to solve it using a timer but without success...I hope you can help me.
Lets see the code:
private void delayedBtnClick(object statInfo)
{
if (doubleClickTimer != null)
doubleClickTimer.Dispose();
doubleClickTimer = null;
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new VoidDelegate(delegate()
{
// ... DO THE SINGLE CLICK ACTION
}));
}
private void btn_Click(object sender, RoutedEventArgs e)
{
if (doubleClickTimer == null)
doubleClickTimer = new Timer(delayedBtnClick, null, System.Windows.Forms.SystemInformation.DoubleClickTime, Timeout.Infinite);
}
}
}
private void btnNext_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (doubleClickTimer != null)
doubleClickTimer.Change(Timeout.Infinite, Timeout.Infinite); // disable it - I've tried it with and without this line
doubleClickTimer.Dispose();
doubleClickTimer = null;
//.... DO THE DOUBLE CLICK ACTION
}
The problem is that the 'SINGLE CLICK ACTION' called after the 'DOUBLE CLICK ACTION' on doubleclick. It's strange that I set thedoubleClickTimer to null on double click but in the delayedBtnClick it's true :O
I've already tried to use longer time, a bool flag and lock...
Do you have any ideas?
Best!
If you set the RoutedEvent's e.Handled to true after handling the MouseDoubleClick event then it will not call the Click Event the second time after the MouseDoubleClick.
There's a recent post which touches on having different behaviors for SingleClick and DoubleClick which may be useful.
However, if you are sure you want separate behaviors and want/need to block the first Click as well as the second Click, you can use the DispatcherTimer like you were.
private static DispatcherTimer myClickWaitTimer =
new DispatcherTimer(
new TimeSpan(0, 0, 0, 1),
DispatcherPriority.Background,
mouseWaitTimer_Tick,
Dispatcher.CurrentDispatcher);
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Stop the timer from ticking.
myClickWaitTimer.Stop();
Trace.WriteLine("Double Click");
e.Handled = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myClickWaitTimer.Start();
}
private static void mouseWaitTimer_Tick(object sender, EventArgs e)
{
myClickWaitTimer.Stop();
// Handle Single Click Actions
Trace.WriteLine("Single Click");
}
You could try this:
Button.MouseLeftButtonDown += Button_MouseLeftButtonDown;
private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
if (e.ClickCount > 1)
{
// Do double-click code
}
else
{
// Do single-click code
}
}
If neccessary, you could require mouse click and wait until mouse up to perform the action.