I've been searching for a solution for couple of days now, but can't find a simple answer.
I've tried a number of examples found on internet (delegates, properties, even breaking OOP making everything public) but none of these seem to work.
Can someone please write a simplest possible code for the following problem:
I have MDI parent form, and a child form. MDI parent form has a status-strip label. Child form has a button. All I want to do is update the MDI label on click of child form button.
Thanks!!!
It is not the best solution. However, it is the easiest one:
1- Change the access modifier of the status-strip label to public.
2- Unbox the parent form to its real type to be able to access the label:
((ActualMdiParentFormType) this.MdiParent).statusStripLabel.Text = "Value";
There is another solution which is to create an event in the child window and register the parent window to that event. In case that the event fires, the parent window will be notified and in the corresponding event handler of the parent window we can update OUR control.
This is a more "MVVM" like approach.
Check these links for more information:
Pass value between forms using events
http://www.c-sharpcorner.com/uploadfile/yougerthen/mvvm-implementation-for-windows-forms/
MVVM: Tutorial from start to finish?
Hope that helps,
Related
I have an application using C# wherein I have a form which is MdiContainer form named parentmdiform in which all my child form gets open.
From submenu of this parentmdiform a form named studentmasterform gets open.from this form on a click of a button a new form named existingstudent form gets open.
When I want go back to studentmasterform again from Existingstudentform, a click of a button on which studentmasterform gets open.But the problem is my earlier opened studentmasterform from parent form does not get focused or disposed off.That means I get two separate object of same form that is Studentmasterform which exists in my parentmdiform.
What I want is ,that same form object should get either focused or disposed of when i click on any other forms within my MDI application to access earlier form.
Can anyone tell me how?
You need to track the creation of studentmasterform. And if it is there, created, don't create new one, do Keyboard.Focus(oldOne);
P.S. There is studentmasterform.Closed event to help you keep the track.
I hope I understand what are you want to do.
First, if you want that a Form closes if it loses focus, take a look at the "Deactivate" event of the Form class. [MSDN Deactive Event]: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.deactivate.aspx
When you need a Form as only once opened, you can check "parentmdiform.MdiChildren" to get all the children of your MDI From. You can give your Forms an unique name, so it's easier to focus the right.
regards, C#er
When I open from the main screen of the application a child form I also display a user control that should be displayed until the child form is closed or the user closes it. If I set the child form to be usercontrol's parent, the user control is not displayed (so the parent of the user control is the desktop). I used SetWindowPos with HWND_TOPMOST and I get the correct behaviour. now when I close the child form I want the usercontrol to close as well. Do you know what I shoud do to get this?
thanks,
When you Show() the child you can also subscribe to it's Closed or Closing event and use that to properly close the UC.
var f = new ChildForm();
// show userctl
f.Closed += MainForm_ChildClosed_Handler;
void MainForm_ChildClosed_Handler(object s, EventArgs e)
{
// close/hide userctl
}
If you keep a reference towards your usercontrol, you should be able to Close (or even Dispose) it from the Closingevent of his parent.
I don't know what you are doing, a UserControl is meant to be a child control, it should be embedded in a Form. It is technically possible to turn a UC into a top level window (like Form), you'd have to set the TopLevel property to True. That however isn't very productive, the window doesn't have the chrome to make it friendly to use. It is missing the title bar, no easy way for the user to move it around, minimize it, close it. And no easy way to solve focus problems, it can disappear behind another window with no easy way for the user to bring it back to the front. Making it top-most is a hack of sorts to get at least the focusing problem solved, but at rather a deer cost.
Just use the UC as it was intended to be used: put it inside a form. That form should probably be the child form you open. It could also be a third form, say a tool window. Use the Show(owner) overload to keep its Z-order in check and cannot do the disappearing act. That also causes it to automatically close when the owner closes.
I've had so much luck developing my application... until now.
My application's main form is a MDI parent, and I didn't think of adding any MDI children in my tests until tonight.
To my surprise, the MDI parent seems to never "get focus" now. The Focus event and the OnFocus method are never called! I mean... it appears focused but none of the in-code focusing events/methods work. Instead a MDI child reports the focus.
How do I fix this?
This is by design. A form acts as a container for other windows, controls. The controls get the focus, the user interacts with, say, a button or text box. Only when the form doesn't have any controls will it get the focus, only because there's nothing else that can get it. The same thing will happen with the MDI child form as soon as you put a control on it. Or with a Panel or UserControl, other container control types.
A form has the Activate and Deactivate events. ActiveForm tells you with one is currently active. Note the distinction between active and focused.
I have a parent form that is set to be TopMost and then I have another form that opens when a button is clicked. This child form is also set to be TopMost. The first issue I had was that when I opened the child form, the application would basically freeze because you couldn't access anything. I decided that instead of using ShowDialog() to open the child form, I would use Show(this). This did fix the initial problem but now I have a new one. The childforms start postition is set to be CenterParent and when I use Show(this), it doesn't work. Is there any way I can make the childform open while both it and the parent form are set to topmost while having the childforms start position set to CenterParent? Thank you.
I've find something useful to share with you, guys. Instead following code
form2.TopMost = true;
use this code in main form:
form2.Owner = this;
If you use Form.TopMost property, the form will overlap all other non-topmost forms, but also those from other applications. Instead of this, set the Form.Owner property to the parent form – the one which should be under the form (e.g. the main form).
G00d luck :)
You could try clearing the TopMost property of the parent form for the duration that the child form is visible.
This would solve the problem of which form should be top most, as there will only ever be one.
Hmm. I've created To forms. Then i set TopMost = true on both. The i add button to first and wrote
new Form2().ShowDialog();
And all just fine. Form2 active and clickable. Form1 not since ShowDialog was called
And second variant works fine. Form2 opened in center of the screen.
May be i misunderstood something?
HI,
I have a form in C# app. On this form I capture a KeyDown event Alt+U which will open a second form. In the second form I have a toolStripButton with shortcutkey Alt+U (the same which I used to open the form with) which prints a document. Now, my problem is when I open the second form It will automatically trigger the event of clicking toolstripbutton since it has the same shortcutkey as I used to open the form with. How can I prevent this from happen.
Regards Johan
This doesn't answer your question, but you really should think about making two different shortcut keys for these two very different actions. Having two identical shortcut keys that do two entirely different actions is very confusing IMO.
To answer your question though, I would have some property on the second form like "ShouldRaise" or something, and only raise the Alt + U event in the second form if that flag is true. Set it to false initially, but then in the KeyUp in the first form, set it to true.
Would it not just be easier to change the shortcut of one to something else? I agree with BFree its not the best design have the same shortcut for 2 completely different functions. All shortcuts/accelerator keys should be unique.
Why not change the shortcut for the form page changing to something like:
Ctrl+Right (Go to next page)
Ctrl+Left (Go to previous page)
On the second form do you actually have a ToolStripMenuItem instead? (a ToolStripButton doesn't have the ShortcutKeys property).
Do you instantiate a new form when the user presses Alt-U on the parent form?
Did you check the sender object on the handler that prints the document to see if it was the parent form?
Can't seem to reproduce your problem; a little explaining would help.
Another easy solution is to do some check of what form you are in from the event listener. You could just return inside the event handler inside your second form.
Again not the most elegant solution but should be a decent fix.