0

ok so im making a windows forms application in microsoft visual C# 2010. this is how i got it to change form:

page_2 page2 = new page_2();
page2.Visible = true;
this.Visible = false;
page2.SetDesktopLocation(50, 50);

the form its on is page_1 and its going to page_2. this works fine, the only problem is that all changes made to page_1 get removed and reset to default when using the same code to return from page_2. a friend of mine said to make page1 and page2 into global variables but i dont really know how to do that.

i found another topic on here asking about global variables and they said to put it in a static class but that didn't solve the problem, it wouldn't even work.

when i try using "page2.Visible = true" it says "page2 does not exist in the current context", im thinking that means i haven't told it where it is properly.

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Jake110
0

figure out a way to switch back and forth between 4 different pages, my example and testing is only using 2 pages at the moment. the code i posted is the code i attached to a button and it opens the second form called page_2, i think its creating an instance of it called page2. the problem is that since thats the only way i know of to change between active forms, it re-creats the other form before opening, causing all the data to reset to the original values.

0

Did you think about something like this:
PS: code is meant to work with 2 forms (form2, and form3). If you want for, add 2 more.

public partial class Form1 : Form
    {
        Form[] forms;
        Form2 f2;
        Form3 f3;
        int counter;
        public Form1()
        {
            InitializeComponent();
            forms = new Form[] { f2, f3 };
        }

        private void buttonFor_Click(object sender, EventArgs e)
        {           
            if (counter < forms.Length)
            {
                CommonMethod();
                counter++;
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            if (counter > 0)
            {
                counter--;
                CommonMethod();               
            }
        }

        private void CommonMethod()
        {
            for (int i = 0; i < forms.Length; i++)
            {
                if (forms[i] == null)
                {
                    if (i == 0)
                    {
                        f2 = new Form2();
                        forms[i] = f2;
                    }
                    else
                    {
                        f3 = new Form3();
                        forms[i] = f3;
                    }
                }
                if (counter == i)
                {
                    forms[i].Location = new Point((this.Location.X + this.Width + 10), this.Location.Y);
                    forms[i].Show();
                }
                else
                    forms[i].Hide();
            }
        }
    }

There is some work to be done, but this is working approximatelly.

Edited by Mitja Bonca: n/a

0

that works for getting off the starting page but when i try using the code for the button on a different form it doesn't work, i think its because it doesn't like it when i try making it use variables from other forms, all the forms are there own classes but it still doesn't like it even when the functions are set to public.

0

ok i've got a work around sorted, im using a seperate form as a page selecter thats always running. same code as you posted. thanks for the help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.