MDI child form is not allowing other child forms to get focus - focus

The application is an MDI container app. A specific child form (Form1) when loaded gets focus and will not release the focus. If another form is opened up, the user is not able to select any field within that form if the Form1 form is open.
Also, within the Form1, the focus will not leave a combobox even to set focus to another field on the same form.
I am not using ShowDialog and TopMost is set to false.

Check to see if you have any methods assigned to the Leave event for the combobox, it sounds as though your application is setting the combobox to the active control whenever it loses focus.
Your form may also have the same type of code implemented, setting it to the active form when it loses focus.

Related

Parent components show over child form

I've been following the below page in order to have a child Form that I can click on its components (such as an edit) and have proper focus:
Working with MDI Forms in Delphi
The problem now is that the components on the parent Form show on top of the child Form. If I use the SendToBack() command for those components, they completely disappear. Here is a screenshot of what happens:
EDIT:
I have a Form with buttons and a ListView that displays client info. When I click on a client, I can click a button to view or edit that client, alternatively add a new one. The Edit/Add pops up a Form where I can input the info and save it. I'm using an OnClick event for each Edit that has a SetFocus(). That gets focus on each Edit, but then all the text is selected, so I cannot click on the line and start editing text - it just overrides unless I use an arrow first.
Initially, I used regular Forms and that's where I had the focus issue. MDI fixed that, but now it's the parent components that show on top of the child.
Concerning MDI forms: that is how MDI works. The main form is not supposed to have anything else than a menu bar and optionally a toolbar, aligned alTop. The child forms use the rest of the space. Well as #RemyLebeau suggested, you can add other controls too, if you align them.
But it turned out that the actual problem with using ordinary secondary forms was that the text in an edit control on the secondary form becomes selected when you set focus to the edit control in a button click. That is easy to change, right after you set the focus to an edit control:
Edit2.SelLength := 0; // nothing selected, cursor at beginning
Edit3.SelStart := Length(Edit3.Text); // nothing selected, cursor at end of text

Firemonkey forms opened with TForm.Show are get hiden when OnClick of the main form is performed

When a form is opened with TForm.Show, and you click outside of this form, it will hide. On the VCL it doesn't happens.
I could use the property FormStyle set to StayOnTop, but it would not be good for the kind of application i'm building.
What could i do to solve this?
I had to set the Parent of the second form. Just like this:
SecondForm.Parent := MainForm;
Doing this, when you click on the MainForm, the focus is changed, but the second form doesn't hide behind the MainForm.

How to tell a non-modal has lost/regained focus

I have a main form and a secondary form both with some DBAware controls from a common database. Currently I am using ShowModal but I would like to be able to use Modal to go back to the main form and navigate the database.
In the secondary I can replace the TDBEdits with TEdits and stuff them with data when I Show the secondary form. There is no means of navigating the database in the secondary form, but, if the user can go back to the main form where they can navigate, I will need to reset the database cursor when they return to the secondary.
How can I tell that the secondary form has just lost focus? I can grab the database cursor position.
How can I tell when the secondary form gets focus again? So I can reset the database cursor if it was moved before returning.
Thanks
p.s. Please no questions on why and/or alternative suggestions. It is an existing application and I really do not want to have to fix miles of code. As crappy as it is, it has been working for years and the customer wants the change of possible. :)
Use the form's OnActivate and OnDeactivate events. OnActivate is called when the form gains focus, and OnDeactivate is called when it loses it.
Note that these events are only triggered when focus is transferred within your own application. If you need to know when your application itself loses or gains focus, use TApplication.OnActivate and TApplication.OnDeactivate instead.
You can take a look at the onActivate and onDeactivate events of the secondary form. I think they are what you need.
Note: OnDeactivate works only if focus is switched to another form of project. For example I have project1.exe which creates 2 forms Form1 and Form2 . So Form1 OnDeactivate event triggers if I ckick Form2. But it will not be trigger if I click Notepad window.

Modal Form in Delphi 7

In Delphi 7, I have a model dialog and i would like to display inside the main form. Right now Its displaying outside the main window.
Im new to Delphi and I was unable to find the answer.
I want to embed the modal form inside the main form.
When I coded Parent := Application.MainForm, the application runs and the form is loaded after that the application become struck in such a way that I cant do any anything.
Set Position property of the modal form to poMainFormCenter. Or specify Left and Top of the modal form to place it where you want to. A user still will be able to move the modal form.
AFAIK a control cannot be enabled if it is a child of a disabled window. So Parent := Application.MainForm will not do the trick: ShowModal explicitly disables all top-level windows before showing the modal form.
you could also use "frames" for this.

Modify a Delphi DFM resource to close upon showing?

Is it possible to edit a DFM (Delphi's form script format) in such a way that a form closes itself when shown?
I don't code in Delphi, so I'm not familiar with how these forms work, but it seems I could put code (but not standard Delphi code as it seems) in the OnShow or OnCreate events of the form. However, after trying several statements like Close, Exit, FormNameExit, Destroy, etc. won't work (a log will be created, stating the error that the value of the OnShow property was invalid, etc.)
The normal way of closing the form is through a button, but the button doesn't have a OnClick event, just a property, "ModalResult = 1".
Is there a way to make the window close upon opening, some standard function I could put on the OnCreate or OnShow events of the form? Or maybe, creating a checkbox on the form itself, that gives ModalResult = 1? (don't know if this works)
Thanks for any suggestion!
=)
(Note: maybe it's obvious, but I don't have the source.)
Not in DFM. You would have to modify the source.
The OnShow and OnCreate lines you're seeing are only used to give the name of a method that's already defined in the source code. You can't add much functionality at all by modifying the DFM file.
Perhaps the form already has a matching event handler that closes it: the OnClick handler for a close button or menu item, maybe? If so, you could try setting it as the OnShow or OnCreate handler.
You might be able to add a TButton to the form and set its ModalResult -- I don't recall whether you actually need a field in the form class for each control in the DFM -- but that would only work if the form is shown modally, and you'd still have to click it to make the form close.
EDIT: Seeing some of your comments added while I typed my text-wall clarifies things a bit.
I'm guessing that you you're using a resource editor to edit the DFM and modify the behaviour of the app without actually touching the source code?
In this case, the best you could try is to set the Visible property to False. However, this will have no benefit if the developer 'actively displays the form in code'. (He could have done this by calling Show, ShowModal or even by explicitly setting the Visible property.)
Unfortunately, if this is the case, then there's nothing you can do without modifying the actual source code. This is because the DFM is processed when the form is loaded; i.e. before the developer's code that shows the form. Even looking for a place to set ModalResult is useless, because the current ModalResult is cleared when ShowModal is called.
I don't think I understand exactly what you're trying to do, because it doesn't make sense.
It seems to me that you want the form to be automatically closed as soon as it is shown; and that doesn't make sense. :S
So, if I have understood you correctly, please explain why you would want to do this; there may be a better solution for your actual problem.
However, some general concepts...
If you want a form to close, you should link it to some action that closes the form. Either put a button on the form, or a menu item.
Standard forms have a standard Windows mechanism to close them by default. (I.e. the X on the top-right.)
There are two ways of showing forms, and the way in which it is shown does have an effect on how it will be closed. It can be shown modally (which means it is the only form of the application which will interact with the user), or it can be shown normally (which allows the user to switch between other forms of the application).
The point of showing a form modally is that it blocks the flow of your code until the user has finished doing something that was required; it often involves the user providing some form of answer or confirmation.
When shown modally, the form should rather be closed with a ModalResult.
When shown normally, the ModalResult has no effect.
Whenever a form is 'closed', there are a few ways in which this can be done.
The form can simply be hidden; it's still there, but invisible. Next time you want to show the form, you just make it visible again.
The form can be destroyed; meaning that it no longer exists in memory. If this is done, then next time you want to use the form, you have to recreate it.
The attempt to close the form can be actively prevented (Not usually advisable; but may be necessary in specific cases - if for example some information on the form is incorrect).
The form may be simply minimised (this is often done with MDI child forms).
NOTE: There are also a number of attributes on forms (FormStyle being the most important) that have an effect on how it behaves, displays, and can be closed. (E.g. MDI Child forms will by default either minimise, or do nothing when closed.)
NB:If the main form of an application is properly closed, then the application will shut down.
Now, some of the technicalities...
As mentioned earlier a form can be displayed either modally, or normally; using either MyForm.Show; or ModalResult := MyForm.ShowModal;
NOTE: If the form was shown modally, you then need to check the ModalResult to find out the user's answer and act accordingly.
If you displayed the form modally, you should set the ModalResult and the form will close itself. An easy way to do this is to assign a ModalResult to the buttons on the form; then the button will automatically set the form's ModalResult when clicked.
If you displayed the form normally, then all you need to do is call MyForm.Close at the appropriate point in time. NB: Note their are other ways to 'close' a form, but it is better to use this method because it allows you to handle the OnCloseQuery event which is a mechanism to confirm whether the form is allowed to close.
NOTE: While closing the form, there are two events that Delphi can call which you can handle in order to modify how the closing of the form behaves:
OnCloseQuery is called to confirm whether the form is allowed to close.
OnClose is called to find out how the form should close (as explained previously).
Coming back to your question (which sounds like you want the form closed automatically). Rather than closing the form automatically; just don't bother showing it. This is very easy to do. All forms have a Visible property; if set to True, Delphi will automatically show the form normally when it is created. So all you need to do is ensure the property is False.
You really can't do much without the source but move files around or change existing properties. If you have a MAP file for the program and there are existing events in place (onCreate/OnShow) you could patch the executable to invoke different code for those events, but it won't be easy and you have to insure that you don't inject more code than was there previously or make any external calls to routines which don't exist.

Resources