Jump to content

How to close and open forms using a class in C#?

I have tried the code below and i know the value 1 is passing from my class to the form as im able to view the passed value from a message box from the form. however when i try to close the form using 'this.close' command it doesnt work. any ideas why?

-------------------------------class-----------------------------------------------
  
  class testclass
    {
        public string test(string a)
        {
  
           int n = 1;
                Form1 f1 = new Form1();
                f1.pageclose(n);
          
          
        }
}
  
  
-----------------------------------------------------------------------------------




----------------------------------------------form----------------------------------
  public int pageclose(int num)
        {
            if (num < 2)
            {
                this.Close();
                string b = num.ToString();
                MessageBox.Show(b);
               
            }
            else
            {


            }
            return num;

        }

 

Link to comment
Share on other sites

Link to post
Share on other sites

Form.Close() method will not close the form if it's called from within the form.

If you want to close the form, in your test class you should just call f1.Close().

 

It's been a while since I've worked with WinForms, but if it doesn't work, let me know.

Try, fail, learn, repeat...

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, zwirek2201 said:

Form.Close() method will not close the form if it's called from within the form.

If you want to close the form, in your test class you should just call f1.Close().

 

It's been a while since I've worked with WinForms, but if it doesn't work, let me know.

Thanks for replying. f1.close() didn't work from the class. no idea why however i tried this.close(), when i tried it on the form it worked but when i try to open another form immediately after closing form1 it closes form 2 as well. so im using this.hide(). im not sure if this is the best option however it did get my job done. 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Shammikit said:

Thanks for replying. f1.close() didn't work from the class. no idea why however i tried this.close(), when i tried it on the form it worked but when i try to open another form immediately after closing form1 it closes form 2 as well. so im using this.hide(). im not sure if this is the best option however it did get my job done. 

It very much is not the best idea. I actually made a little bit of a test project and got it to work pretty well. You can find the project in attachment. If you have any other questions about it, feel free to comment or pm me.

 

If you're worried that it's a virus of some sort, I can post the code here :D

ForumTest.rar

Try, fail, learn, repeat...

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, zwirek2201 said:

It very much is not the best idea. I actually made a little bit of a test project and got it to work pretty well. You can find the project in attachment. If you have any other questions about it, feel free to comment or pm me.

 

If you're worried that it's a virus of some sort, I can post the code here :D

ForumTest.rar

Thanks for the support. I'll see to it

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×