Detect when application becomes active - winforms

In MDI application which event can the child Form class use to detect when the application becomes active?
I tried Form.Acivated event but it occurs only when the form is activated and doesn't when the application gets focus.

It is the MDI parent form that gets the Activated event. You can subscribe to the event in your child form's Load event. Be careful, you have to make sure you unsubscribe the event when the child gets closed or you'll leak the child form instance. Make it look like this:
protected override void OnLoad(EventArgs e) {
var main = this.MdiParent;
main.Activated += main_AppActivated;
this.FormClosed += (o, ea) => main.Activated -= main_AppActivated;
}
void main_AppActivated(object sender, EventArgs e) {
// Etc...
}

Have you tried the GotFocus event?

While WPF has such a notion, WinForms does not to the best of my knowledge; you'd need to use Form-level events (like GotFocus from the earlier answer).

Related

How To Navigate From Child Window To User Control Silverlight?

I have a child window with two buttons. When a button click event occurs I need to navigate to a specific user control. How to do this in Prism Framework?
If you are using PRISM will be simple as fires a notification event using the event aggregator. Anyway if not, this will solve your problem.
You need when you fire the childwindow to add the close event to that childwindow, like this:
YourChildWindow childWindow = new YourChildWindow ();
childWindow.Closed += new EventHandler
(YourChildWindowClosed);
childWindow.Show();
Then,
?
void YourChildWindowClosed(object sender, EventArgs e)
{
YourChildWindow yourChildWindow = ((YourChildWindow)sender);
if
(yourChildWindow.DialogResult.Value)
{
... do your things here ...
}
}
I hope this can help you.

How to handle child event in parent control

In my main window, I have a child control(user control) which contains a text box . How can I handle the textchange event of the text box of child control in main(parent) window.
Please provide me some example with code as I am new to routing of events.
You should just be able to hook the event from the parent control. But since your parent control doesn't have a TextChanged event of its own, you'll need to use attached-property syntax:
<Window ...
TextBox.TextChanged="ChildTextBoxChanged">
and in your codebehind:
private void ChildTextBoxChanged(object sender, TextChangedEventArgs args)
{
...
}
You don't have to put the TextBox.TextChanged= on the Window specifically -- just any control that's a parent of the TextBox, i.e., any control that's a parent of your UserControl. The event will bubble up to each parent in turn, all the way up to the top-level Window, and can get handled anywhere along the way.
(Note that if anyone hooks the event and sets e.Handled = true, the event won't bubble past that point. Useful to know if you have handlers at multiple levels.)
this also helped me. I will have a event in the container of the child control and define the event in the code behind file. The event will handle all the text changed events for all the children.
<StackPanel TextBoxBase.TextChanged="test_TextChanged" Name="test">
<userControl/>
</StackPanel>
Create a event in your childcontrol -
public event TextChangedEventHandler TextChanged;
now add a handler for TextChanged event of TextBox in childcontrol -
private void TextBox_TextChanged(object sender, TextChangedEventArgs args)
{
if (TextChanged != null)
{
TextChanged.Invoke(this, args);
}
}
also update XAML for this handler -
<TextBox ... TextChanged="TextBox_TextChanged" ... />
Now, you have created a event in your childcontrol that fires when the Textbox's textchanged fires.
Now you only to add a handler for this event in mainwindow -
private void ChildControl_TextChanged(object sender, TextChangedEventArgs args)
{
//TODO: Add your further code here.
}

Hotkeys in Silverlight?

Is there a way to create global keys in Silverlight? I added a KeyDown event handler to the topmost Page element. I can catch the key signal when elements like a Button, TextBox, Calendar, and RichTextBox have focus. But the DatePicker won't let me handle the key down event at the Page level. Any way to make a hotkey in non-OOB SL?
P.S. I am assuming that the DatePicker control behaves differently than others as it is still in the Silverlight Toolkit.
have you tried using the AddHandler method? With this method you can add a handler and define if you want to handle already handled events, too.
In my simple example I added it to the RootVisual in the app.xaml.cs.
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
RootVisual.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(HandleKeyDown), true);
}
private void HandleKeyDown(object sender, KeyEventArgs e)
{
//Add your keyhandling code here
Debug.WriteLine(e.Key);
}
I tried it with a DatePicker and it works.
Hope this helps!
BR,
TJ
This may help you. The author provides two ways using HTML bridge (when your Silverlight does not have focus) and using the KeyUp event on Application.RootVisual.
I have not tried it myself so I wouldn't know if it's working.

WPF Routed events firing?

I have a WPF form with 3 buttons and have routed events on them, commands are binded on start...
private void InitCommandBinding(UIElement frameworkElement) {
CommandBinding commandBinding;
commandBinding = new CommandBinding(ViewModelCommands.Save, Save_Executed, Save_CanExecute);
frameworkElement.CommandBindings.Add(commandBinding);
commandBinding = new CommandBinding(ViewModelCommands.SaveAndClose, SaveAndClose_Executed, SaveAndClose_CanExecute);
frameworkElement.CommandBindings.Add(commandBinding);
commandBinding = new CommandBinding(ViewModelCommands.Delete, Delete_Executed, Delete_CanExecute);
frameworkElement.CommandBindings.Add(commandBinding);
}
the details ui has code like
private void Delete_Executed(object sender, ExecutedRoutedEventArgs e) {
try
{do validations }
}
private void Delete_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = viewModel.IsValid(); (returns bool)
}
Validity enables and disables buttons etc.
The form has an instance of an object new or old and validations take place on the data
My issue is that the event just excute all the time and the form just hangs cause validation code does poll db etc to check....
how to I just get them to fire once when the form is loaded mmm....
If I understand you well it is only necessary to check the validity of the data at form load and the IsValid method is resource intensive?
Why don't you change the IsValid() method to an IsValid property and set this is in the Form_Loaded event?
The CanExute method will be checked any time the UI fires an event like TextChanged, LostFocus etc. So you better make such methods very lightweight.

winform mdi catch child form focus c#

I would like to be able to catch a child window focus event, in an mdi form.
If i loose focus to the entire mdi application, then click on a child, it works, but if I had two child forms open, I cannot catch the focus event when clicking between them.
I am using Dotnet Framework 2.0, and I need the code solution that will run fine on a windows 2000 machine, and up.
Thanks in advance for all help and advice,
-regards
Jeremy
I think you're looking for the Form.MdiChildActivate event. This event will be fired in your MDI parent form.
override the child forms Activated event.
sample code:
private void addChild(){
frmChild mychild = new frmChild();
mychild.Activated += FActivated;
mychild.MdiParent = this;
mychild.Show();
}
private void FActivated(object sender, EventArgs e)
{
MessageBox.Show("Activated one of the child.");
}

Resources