Avoid SizeChanged event on startup - c#

I have a MainWindow Class which as a few Events - all of them should call a method in another class.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
getdata.MainWindow = this;
}
private void button_Click(object sender, RoutedEventArgs e)
{
getdata go = new getdata();
go.clear();
}
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
getdata go = new getdata();
go.clear();
}
private void comboBox2_DropDownClosed(object sender, EventArgs e)
{
getdata go = new getdata();
go.clear();
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) {
getdata go = new getdata();
go.clear(); //<-this causes exception on Startup
}
}
The Problem is that the MainWindow_SizeChanged Event is also triggered on Startup of the program but the clear method uses also some objects that are not yet created at Startup, which causes an error. How can I avoid this and only have this Event triggered when the size is actually changed while running the program?

You have the IsLoaded property of Window.
You can check it before executing code.

You can check if an item is null when re-sizing so you can avoid this being an issue, this can be done using the null coalescence and null conditional operators. If for example you are using the value of a textBox on startup but it has not been instantiated you can use this
string someText = tB1?.Text?
if(someText == null) return;

SizeChanged Event gets triggered before the window is loaded. You can try subscribing to the SizeChanged event as part of Loaded event and unsubscribe it in the Unloaded as shown below.
//XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Loaded="Window_Loaded"
Unloaded="MainWindow_OnUnloaded">
//Code Behind
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
Debug.WriteLine("Size Changed Triggered");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SizeChanged += Window_SizeChanged;
}
private void MainWindow_OnUnloaded(object sender, RoutedEventArgs e)
{
SizeChanged -= Window_SizeChanged;
}

Related

Winui3 OnClosin in the single page

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...
}

MouseMove Event not static and readonly

I'm trying to add a delegate for a "mousemove" event to a method in a class. I'm using the EventHandler that I would use for the Xaml "MouseMove" but it's not available and only "MouseMoveEvent" is(I don't know if that effect things).
I've looked at the 2 different types in meta Data but it doesn't tell me much. I've also tried making the method that it's linked to read only and static but it's not valid.
I'm making the delegate like this:
MainWindow.MouseMoveEvent += delegate (object sender, MouseEventArgs e) { Project.Mouse_Move(this); };
Where MainWindow is the window of my UI. This is the function it's calling
public partial class Program
{
public void Mouse_Move(MainWindow MainWind)
{
}
}
I'm trying to get the same result as if you would do it through xaml:
Title="MainWindow" MouseMove="Window_MouseMove">
and method like this:
private void Window_MouseMove(object sender, MouseEventArgs e)
{
}
MainWindow is a type. You need a reference to an actual instance of a window to be able to hook up an event handler to its MouseMove event. Try Application.Current.MainWindow:
Application.Current.MainWindow.MouseMove += delegate (object sender, MouseEventArgs e) { Project.Mouse_Move(this); };
Or Application.Current.Windows:
var window = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
if(window != null)
window.MouseMove += delegate (object sender, MouseEventArgs e) { Project.Mouse_Move(this); };

KeyDown preventing text appearing in TextBox [UWP]

I have the current UWP app targeting 10240:
<Page x:Class="App8.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ContentControl KeyDown="ContentControl_KeyDown">
<TextBox TextChanged="TextBox_TextChanged"/>
</ContentControl>
</Grid>
</Page>
And:
namespace App8
{
public sealed partial class MainPage : Page
{
public MainPage() => InitializeComponent();
private void ContentControl_KeyDown(object sender, KeyRoutedEventArgs e) => e.Handled = true;
private void TextBox_TextChanged(object sender, TextChangedEventArgs e) => Debug.WriteLine("NEVER RUNNING CODE");
}
}
When I write in the textbox I want to avoid any key events going to the main screen. In order to do that I have the KeyDown in the parent element of the textbox, and I handle the event. But If I do that the textbox doesn't write anything.
I'd like to end any key events going in the ContentControl going to the Page, but allowing the textbox to work normally. Any ideas?
I'd like to end any key events going in the ContentControl going to the Page, but allowing the textbox to work normally. Any ideas?
For your requirement, you could make bool flag to tell main screen fire some events or not when the TextBox is focused or not.
private bool IsFocus;
private void MyTextBox_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
IsFocus = true;
}
private void MyTextBox_LostFocus(object sender, RoutedEventArgs e)
{
IsFocus = false;
}
Usage
public MainPage()
{
this.InitializeComponent();
Window.Current.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
}
private void Dispatcher_AcceleratorKeyActivated(Windows.UI.Core.CoreDispatcher sender, Windows.UI.Core.AcceleratorKeyEventArgs args)
{
if (IsFocus)
{
System.Diagnostics.Debug.WriteLine("Do Not Fire Your Event ");
return;
}
else
{
System.Diagnostics.Debug.WriteLine(" Fire Your Event ");
}
}

How to call LostFocus event of another control from the other control

Calling the LostFocus event of control from other control
Example. : I want to call LostFocus event of Button1 button from the Gotfocus event of Button2 button.
code is :-
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1.gotFocus // this line giving error.
}
Use following code:
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1.RaiseEvent(new RoutedEventArgs(LostFocusEvent, button1));
}
private void button1_LostFocus(object sender, RoutedEventArgs e)
{
}
If this doesn't solve your problem, post your code and state your problem and purpose clearly so that you can get better solution.
you can just call event handler method of Button1's LostFocus event in button2_GotFocus :
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1_LostFocus(this.button1, null);
}
Try this
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1_LostFocus(sender,e)
}

C# Form UserControl OnClick events

I've created a custom user control for a windows form that will operate similar to a button (and please don't suggest that I just use a button, because I will be storing data in this user control), but I can't figure out how to get the OnClick() event to fire. I've sifted through a handful of tutorials and looked at a few similar questions on the site, but I can't seem to get the event to fire off - so I'm either doing something wrong or everyone posted incorrect code (I hope it isn't the latter)
In my custom control.cs,
namespace MobCreator {
public partial class MOBSample : UserControl {
public MOBSample() {
InitializeComponent();
}
protected override void OnMouseUp(MouseEventArgs e) {
this.BorderStyle = BorderStyle.FixedSingle;
base.OnMouseUp(e);
}
protected override void OnMouseDown(MouseEventArgs e) {
this.BorderStyle = BorderStyle.Fixed3D;
base.OnMouseDown(e);
}
public event EventHandler ButtonClick;
private void OnButtonClick(object sender, EventArgs e) {
// invoke UserControl event here
if (this.ButtonClick != null) this.OnButtonClick(sender, e);
}
}
}
And in my form.cs,
private void MobCreatorForm_Load(object sender, EventArgs e) {
UserControl1.ButtonClick += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e) {
Console.WriteLine("Click");
}
However, when I run the program my console never outputs "Click".
Check this link on MSDN: it is a simple Event tutorial, you should be able to adapt it to your scenario.
At a first look, what you are probably missing is a Delegate for your event.
Try this
private void MobCreatorForm_Load(object sender, EventArgs e)
{
CustomEvent_Handler(null,null);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
Console.WriteLine("Click");
}

Categories

Resources