winform mdi catch child form focus c# - winforms

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.");
}

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.

What event is raised on Grid.Children.Add

In my WPF application, I have a single Main window with a Grid. The Login and Shell are 2 separate UserControls added as children to a grid. I need to find out when the Shell is loaded and start a timer from the Main window.
I just need to know as to what event is raised when a UserControl is added using Grid.Children.Add method, so that I can check if Login is loaded or the Shell and start the timer.
I'm not quite sure what you're trying,
but it sounds like you're looking for the Load event:
UserControl MyControl = new UserControl();
MyControl.Loaded += new RoutedEventHandler(MyControl_Loaded);
public void MyControl_Loaded(object sender, RoutedEventArgs e)
{
if (((UserControl)sender).IsLoaded)
{
..... do something
}
}
Hope it helps

closing a win form when datagridview has validation errors

I'm using typed dataset in winforms app, .net 3.5 (VS 2010). A form has DataGridView. In FormClosing event I ask user to save changes. If user doesn't want to save I'd like to let the from close. However, when DataGridView has validation errors (I validate dataset in datatables's ColumnChanging event) the form won't close. Even if I do not catch FormCLosing event the form refuses to close. I guess I have to clear validation errors in datagridvIew somehow. Can someone suggest a solution?
Edit: One more detail: the form is mdi child form. Needless to say, mdi parent won't close also.
You may override validation and (force close) close the form the by setting false to FormClosingEventArgs.Cancel property of Closing handler argument.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult res = MessageBox.Show("Close it?", "Close", MessageBoxButtons.YesNo);
if (res == DialogResult.No)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
Ok, it was my mistake. mdi parent had some handlers for mdi child events, but when the child form closed not all handlers were removed.

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.

Detect when application becomes active

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

Resources