Windows forms usinf c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nmsreddi
    Contributor
    • Jul 2006
    • 366

    #1

    Windows forms usinf c#

    Hi friends

    i am developing a windows application ,i am opening a new form in a button click,when ever i click that button a new is opened but ,i don't want that,if the form is already opened then i dont want one more form to open, how can i check whether the form is already opened or not ,

    i have used a bool variable to check that and solved my problem ,but is there any proffessionel technique in c# to find whether the form is opened or not,

    please don't suggest to disable the button till the form is closed or using a bool variable to maintain the form state

    Regards
    Nmsreddi
  • CyberSoftHari
    Recognized Expert Contributor
    • Sep 2007
    • 488

    #2
    Declare form object in current form class as private

    [CODE=cpp]
    public partial class Form1 : Form
    {
    private Form objForm = new Form2();
    //..........
    //.............
    [/CODE]

    while opening the form use the below code

    [CODE=cpp]
    objForm.Show();
    objForm.Activat e();
    [/CODE]

    Comment

    • nmsreddi
      Contributor
      • Jul 2006
      • 366

      #3
      Hello

      Thanks for your reply, i have done that but no use , in c# by default it takes as private, opening a new form is not the problem ,i should be able to recognise that the form i am trying to open is already opened or not .


      Regards
      Nmsreddi

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        Do you have an Mdi form?

        If you do, most probably you would be opening all the forms whose parent is the mdi form
        so before opening a new form cycle through each of the form contained in the mdi form, and check if they are of you particular form type
        if you find it then show that form, else instantiate a new one and show it.

        PS: Stratergy 2:
        Keep a static class with a list of forms variables which should only have 0 or one instance.
        The moment you instantiate one, set its reference to the corresponding variable in the static class. And remember to set it to null when you close the form.
        This is a bit hectic to maintain it, but reduces the find form code a lot.

        cheers

        Comment

        • nmsreddi
          Contributor
          • Jul 2006
          • 366

          #5
          Originally posted by Shashi Sadasivan
          Do you have an Mdi form?

          If you do, most probably you would be opening all the forms whose parent is the mdi form
          so before opening a new form cycle through each of the form contained in the mdi form, and check if they are of you particular form type
          if you find it then show that form, else instantiate a new one and show it.

          PS: Stratergy 2:
          Keep a static class with a list of forms variables which should only have 0 or one instance.
          The moment you instantiate one, set its reference to the corresponding variable in the static class. And remember to set it to null when you close the form.
          This is a bit hectic to maintain it, but reduces the find form code a lot.

          cheers
          Hello thanks for ur reply

          the second procedure what you have given is similar to what now i am using

          but instead of a separate class i am maintaining the form status using a bool variable ,i am not using MDI parent for all forms .

          can you suggest any method or existing property to find the form life status

          such as we have in web ,

          Regards
          N.Msreddi

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            Application.Ope nForms that will give a list of all the forms currently helld by the application

            Though I did not understand when you said "such as we have in web"

            cheers

            Comment

            Working...