My app has multiple pages.
When I press the Close key in the toolbar, how can I detect the OnClosing event in the single page to avoid closing the app instead of going back to the MainWindow with "this.Frame.GoBack();" ?
I can only catch OnClosing on MainWindow
You could store a reference to the window in a property or field in your App.xaml.cs class as suggested here and then handle the Closing event of the window in the Page class something like this:
public sealed partial class MainPage : Page
{
private Window _parentWindow;
public MainPage()
{
this.InitializeComponent();
this.Loaded += OnLoaded;
this.Unloaded += OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
_parentWindow = (Application.Current as App)?.m_window;
if (_parentWindow != null)
_parentWindow.Closed += OnWindowClosed;
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
if (_parentWindow != null)
_parentWindow.Closed -= OnWindowClosed;
}
private void OnWindowClosed(object sender, WindowEventArgs args)
{
// Prevent the window from being closed based on some logic of yours...
args.Handled = true;
}
}
You can use the Unloaded event on Pages.
*.xaml
<local:TestPage Unloaded="TestPage_Unloaded" />
*.xaml.cs
private void TestPage_Unloaded(object sender, RoutedEventArgs e)
{
// Do your page closing work here...
}
Related
I have a class that inherits from UserControl:
public partial class MyView : System.Windows.Forms.UserControl
I want to handle the event that occurs when the user clicks on the X in the upper right corner. Maybe it is Form.Closing? But I don't see that as an option in the designer. What event is it?
Edit:
class SomeControl : UserControl
{
Form _owner;
public SomeControl()
{
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (Visible)
{
_owner = FindForm();
//_owner = ParentForm;
_owner.FormClosing += _owner_FormClosing;
_owner.FormClosed += _owner_FormClosed;
}
}
private void _owner_FormClosed(object sender, FormClosedEventArgs e)
{
throw new NotImplementedException();
}
private void _owner_FormClosing(object sender, FormClosingEventArgs e)
{
Hide();
_owner.FormClosing -= _owner_FormClosing;
_owner.FormClosed -= _owner_FormClosed;
Parent.Controls.Remove(this);
_owner = null;
}
}
I create the ButtonClick event in the UserControl below and want to handle it in a Form, but I get an error that says 'The name UserControl1.ButtonClick' does not exist in the current context.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public event EventHandler ButtonClick;
private void btnUpdate_Click(object sender, EventArgs e)
{
if (this.ButtonClick != null)
this.ButtonClick(this, e);
}
Form:
UserControl1.ButtonClick += new EventHandler(UserControl_ButtonClick);
protected void UserControl_ButtonClick(object sender, EventArgs e)
{
//handle the event
}
Form:
public PatientNumber()
{
InitializeComponent();
userControl11.updateButton.Click += button_Click;
}
protected void button_Click(object sender, EventArgs e)
{
//handle the event
}
UserControl:
public Button btnUpdatee
{
get
{
return btnUpdate;
}
set
{
btnUpdate = value;
}
}
This code worked.
Thank you
I am using windows forms C#.
I have main Form and child form. The event is in the child Form and I subscribed to this event in the main Form. The problem is that when I click button1 in child form it does nothing (it should fire the event so textBox1 prints the text) because the event is still null although the main form has already subscribed to the event. Am I doing something wrong? Please help how can I fire the event once button1 is clicked.
Child Form:
public partial class ChildForm : Form
{
public event EventHandler MyEvent;
private void button1_Click(object sender, EventArgs e)
{
if (myEvent != null)
{
MyEvent(this, e);
}
}
Main Form:
public partial class MainForm : Form
{
ChildForm ChildFrm= new ChildForm ();
ChildFrm.MyEvent += new EventHandler(HandleTheEvent);
private void button1_Click(object sender, EventArgs e)
{
ChildForm childfrm = new ChildForm ();
childfrm.ShowDialog()
}
public void HandleTheEvent(object sender, EventArgs e)
{
textBox1.Text = "event is fired";
}
You are adding the event handler to an another instance of ChildForm. Change MainForm's button1_click to look like this:
private void button1_Click(object sender, EventArgs e)
{
ChildFrm.ShowDialog()
}
And your application should work OK.
Here's the working MainForm.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ChildFrm.MyEvent += new EventHandler(HandleTheEvent);
}
ChildForm ChildFrm = new ChildForm();
private void button1_Click(object sender, EventArgs e)
{
ChildFrm.ShowDialog();
}
public void HandleTheEvent(object sender, EventArgs e)
{
textBox1.Text = "event is fired";
}
}
Subscribe to the event right after creating the form :)
private void button1_Click(object sender, EventArgs e)
{
ChildForm childfrm = new ChildForm();
childfrm.MyEvent += new EventHandler(HandleTheEvent);
childfrm.ShowDialog()
}
I have a CheckBox with IsChecked="true" property and Checked event:
<CheckBox Checked="myChBox_Checked" IsChecked="True" />
I don't want to trigger the Checked event on page load. Because I use other Controls in the method and I get NullReferenceException:
private void myChBox_Checked(object sender, RoutedEventArgs e) {
myComboBox ... // NullReferenceException
}
What am I supposed to do to stop the triggering from startup?
You can for example:
subscribe in Page.Loaded event:
this.Loaded += (sender, e) => myChBox.Checked += myChBox_Checked;
or maybe better just check in your event for null:
private void myChBox_Checked(object sender, RoutedEventArgs e)
{
if (something == null) return;
myComboBox ... // NullReferenceException
}
You can just check for null reference in the event handler, or assign it in the page Loaded event handler:
public partial class MyPage : Page
{
public MyPage()
{
InitializeComponent();
this.Loaded += page_Loaded;
}
private void page_Loaded(object sender, RoutedEventArgs e)
{
myChBox.Checked += myChBox_Checked;
}
private void myChBox_Checked(object sender, RoutedEventArgs e)
{
if (myComboBox != null)
{
myComboBox ... // Should not be null, but check anyway
}
}
}
Is it OnNavigatedTo or Loaded Event? I've been able to use both interchangeably but I would like to know explicitly which comes first.
OnNavigatedTo fires first -- source - A simple experiment
See code below
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
// Simple button Click event handler to take us to the second page
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
}
}
in OnNavigatedTo:
System.Diagnostics.Debug.WriteLine("in OnNavigatedTo");
in Loaded:
System.Diagnostics.Debug.WriteLine("in Loaded");
Run in Debug mode, see which one writes first.