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
Related
Background:
I am creating a WinForms app using C# 4.0 and VS2013. My app has a main form named ParentForm and a secondary form named ChildForm. The application follows an Model-View-Presenter design, so I want my presenter class P to control all the form opening, closing, button clicks, and generally anything the user requests. (Both forms are actually abstracted behind "view" interfaces, but the problem is specifically with the WinForms implementation, so let's ignore the interfaces.)
Problem:
Currently P is handling FormClosing events for both ParentForm and ChildForm, and calling Form.Close or Form.Dispose if it determines that the user is allowed to close these forms.
The problem I'm having is if ChildForm is open and the user clicks the 'X' button on ParentForm. In this scenario, FormClosing is called first on ChildForm, then on ParentForm. P doesn't have any way of knowing whether the user clicked 'X' on ChildForm (which P should react to by closing ChildForm), or the user clicked 'X' on ParentForm (which P should react to by confirming if the user really wants to exit, before closing any forms).
Questions:
Is there a way to tell if FormClosing is being called in reaction to a parent form closing?
Is there any way to treat the 'X' button like a normal button?
Is there a way to prevent child form closes until after the parent form's FormClosing event (and possible user cancellation)?
You can use FormClosingEventArgs.CloseReason property, which according to MSDN
Gets a value that indicates why the form is being closed.
See the CloseReason Enumeration values, in particular FormOwnerClosing and MdiFormClosing.
so I have looked this up so much and nothing fits exactly what I want.
I have one form open called "Main" it has a button in it that opens up a form using the code
AddModification modification = new AddModification();
Modification.Show();
Now this opens the form correctly and all. But the problem is that both forms are open. So the AddModification form is a popup form that changes things in the Main Form.
So the AddModification form is opened / shown but the form itself is broken because I can't put Main main = new Main(); in the AddModification form without a stack overflow problem.
So how the heck do I access any of Main's controls in AddModification if I need to keep main and AddModification open and not disposed?
You need to pass the first form as a constructor parameter to the second form.
Have you tried something like 'this.Owner' ?
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 two forms, Form1, and a UserControl which hosts Form2. Within that UserControl on Form1 I call Form2.Show();. I have also tried Form2.Show(this);. Either way, the focus is not given to the form. I have to click once within the new form to give it focus, and then I can click items within that form.
I figured that control is passing back to my main control/form, and thus the focus is getting lost. So I am waiting until Form2 is closed via:
while (form2.Visible == true)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
This seems to work. However after I close the form, now the reverse holds true. Form1 is not given focus (even if I call this.Focus()) until I click once within the main form window.
Any ideas how to handle this properly. I want to show a child form (modeless) and immediatley be able to click on it, and when that form is closed, immediately be able to take action back on the parent form.
You should probably use .ShowDialog(), this can also be extended to give your response on if the user performed Form2's operation properly or aborted early.
This does make the form locked in focus up front and will halt your code execution on the first form till that form is closed.
use this.Activate(); in place of this.Focus();
Not sure I follow completely, but from your UC try opening Form2 like this:
form2.Show(Parent);
This should specify the UC's Parent form as form2's owner.
This came from the fact that I was overriding WndProc in order to display the form. When I received the CBN_DROPDOWN message, I would display the form. I fixed this by instead Invoke'ing the method that shows the form and it fixed it.
case CBN_DROPDOWN:
Invoke(new MethodInvoker(Show_DropDown));
return;
New to C# and .NET.
In a windows application I place a form to query a user for some input. The user fills out the form and presses 'ok'.
What is the "correct" or "appropriate" method of retrieving that data from the form? Do forms have a return statement? Do I send a message?
Thanks in advance.
you can have the parent form subscribe to events from the child form and still do the same thing or put the form results into an event arg statement
you should listen to the click event of the button and you will get a call back and you can simply read the properties from the form objects (txtBox.Text, etc . . )
After they press OK, the form will close, but (as long as the form variable is still in scope), its members will still contain all of the data. Save the input to a member variable and access it from your main program using an accessor. If the form is not modal, your main program can subscribe to the form closing event and get the information then.