There is a list with data. Clicking 2 times opens the form with this detailed data. There is an identification code or series. It is necessary to prohibit the opening of the form on the identification code if it is already open.

Is it possible?

  • it may be better to simply open the detailed data form as the only available over the main data list, such as formDetails.ShowDialog(); ? - Alias
  • The easiest way, if business logic agrees, then this is a modal dialog box. If business logic requires a multi-window interface, create a list of series identifier strings in the parent form. Accordingly, when opening - check if there is already this series there. When closing onClose (), delete the corresponding identifier from this list of the parent form. - Alexander Muksimov
  • It is possible in the public property of the data window to return your identifier, and before opening to check whether there is an open form with such an identifier, if not, open a new one, if there is, we bring to the forefront what is already open. You can combine with the option of Alexander. - rdorn
  • the rdorn colleague is right, the parent form already has a list of descendants, and therefore it is better to link the series to the form, and double click to check if there is a form with such a series, and if there is, bring it to the fore. - Alexander Muksimov

3 answers 3

Just blocking the opening is not good, because This may be misunderstood by the end user and will increase the number of calls to tech support.

In any case, you will need to somehow identify with what data the form / window / controll is open. To do this, add a public property in which the identifier will be returned for identification.

Then you can use this option:

  • When opened, the form is registered in the dictionary as open, with the identifier as the key. (in this place there are possible variants in the implementation, for example, to use a list of child forms, registration in which occurs automatically upon opening, but the essence does not change from this)
  • Before opening a new check whether the form is registered in the open.
  • If it is registered, we bring it to the foreground; if not, we create a new one, register it, open it.
  • When closing, delete the registration.

    Maybe!

    The principle is simple, you need a static Bul, when you open the desired window, you check its state, if false make it true and open the form, well, if it is True then print the message, and when you close the form, you do it back to false .

    I hope it is clear that true - means the form is open and false - the form is closed.

    • In this variant, you cannot open two forms with different data, and this was not required to be prohibited. But for the case when, in principle, more than one instance should not be open, modal windows are ideal. - rdorn
    • Like about 2 or more forms nothing was said, and the question is not about how to open a simple form! - Abs3akt
    • Even in this case, the usual modal (dialog) window already implements the necessary functionality without having to write something from itself. Even the original form does not need to change, just open correctly. - rdorn
    • Yes, of course he can use a modal window. But the question was not about how to create this form and where, but about the other, read the question and understand - Abs3akt

    We define the global variable list where we will save the code when opening the form and when closing close the code from there.

     internal static class GlobabalModule { public static List<string> CodeList { get; set; } public static void InitList() { CodeList = new List<string>(); } } 

    How to declare a global project level variable