cannot make a visible window modal delphi

For example I created a small project as for you to understand what I’m trying to achieve.

I’ve got a ModalForm that has some buttons on it created at runtime, but when the user presses a » special button» I want all the buttons from the form to be deteled as other buttons will be created on runtime. Here is an example code

Now in this example I have 3 buttons on the form, when I click on button 1 I want a 4th button to appear. But if I run my code I get the error

«Cannot make a visible window modal»

I read that has something to do with the fact that I don’t properly close my Form2. If I close Form2 and click on the button from Form1 ( same code as the button from Form2) it works and I get my 4th button the Form2.

My question is now, how can I achieve this result I get from clicking on the button from Form1 with clicking on the button from Form2.

Содержание

  1. 2 Answers 2
  2. Cannot make a visible window modal
  3. Cannot make a visible window modal
  4. RE: Cannot make a visible window modal
  5. RE: Cannot make a visible window modal
  6. Red Flag Submitted
  7. Reply To This Thread
  8. Posting in the Tek-Tips forums is a member-only feature.

2 Answers 2

You do not need to close and (re)show a form in order to dynamically add controls to it. In your TForm2.Button1Click method get rid of Form2.CloseModal and Form2.ShowModal calls, ie

BTW adding an button to Form2 in TForm1.Button1Click() is just bad design, don’t do that (one form shouldn’t change other like that). Rather have a method in Form2 which creates the button and then other forms can call that function. Or override the constructor of the Form2 so that it takes an extra parameter which indicates should the special button be visible or not.

To remove buttons you just call Free on them. Ie to delete all the buttons on the form

But if you have predefined number of buttons it might be better to create them all at design time and then just change the Visible property as needed.

There are a couple of obvious things wrong with this code.

Call to ShowModal from the constructor

The form’s OnCreate event is fired during construction. At that point it is too early to show the form modally. So you must not call ShowModal there. I believe that you should simply remove TForm2.FormCreate . The form is shown modally from TForm1.Button1Click , and that suffices.

Spurious calls to CloseModal and ShowModal from TForm2.Button1Click

There’s no need to call CloseModal and ShowModal in that method. You can perfectly well create a new button from that method, and there is no need to interfere with the modal loop. That’s the cause of your error message. Remove those calls.

Use of global variable Form2 rather than Self

Although your form is clearly a single instance form, you should still avoid using the Form2 global variable from inside TForm2 methods. At some point in the future you might want to have two instance of the form. Or remove the global variable altogether. Remember that instance methods can use Self to refer to the instance. So, for instance, in TForm2 methods replace

Likewise, in TForm2 methods, any time you feel compelled to write Form2.Foo , instead write Foo .

Enumerating controls inside another container

Use the ControlCount and Controls[] property of TWinControl to find all the controls in a container. For instance, if you know the parent of the buttons is the form, use ControlCount and Controls[] on the form to find its children. Then you can delete any buttons by calling Free on those buttons. Test for the control being buttons using Controls[Index] is TButton .

Note that we enumerate the controls in reverse index order. This trick avoids indexing problems that arise from us modifying the list whilst iterating over it.

Cannot make a visible window modal

Cannot make a visible window modal

RE: Cannot make a visible window modal

Not completely sure how you have your forms setup, but maybe this will solve the problem. I’m assuming you are not creating an MDI application.

From your Project menu select options, go to the forms tab and make your three secondary forms available, but not auto-created. They need to be listed in the right list box, your main form on the left list box.

add the units of each of the modal forms to the uses clause of your main form.

To open the secondary forms (only one open at a time)

Now on your modal forms, put two buttons on it, One with ‘Yes’ One with ‘No’ Captions, DELETE any code already associated with the button — as in Close; and then (while the button is selected) go to the object inspector and select the property ModalResult select mrYes for the Yes button and select mrNo for the No button (obviously you can select any from the list, not just Yes and No. But you’ll need to change the code above from mrYes to the one you selected)

And last, make sure you set the modal form’s visible property to FALSE.

RE: Cannot make a visible window modal

You can not show a form modally when the Visible property is set to true. Set it to false (in design or otherwise) before you call ShowModal.

(the error message is quite literal)

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Вызываю из одной формы другую:

Затем из второй закрываю ту и открываю первую:

Первый раз работает нормально, но вот при повторной попытке открытия второй формы выскакивает ошибка «cannot make a visible window modal». В чём дело.

Источник: computermaker.info

Понравилась статья? Поделиться с друзьями:
Ок! Компьютер
Добавить комментарий