How to change bool flag status on the main windows mobile form by pressing button on the other windows mobile form
-
When I run my application I am initializing some boolean flags - all of them are set to false at the begining, then just before the Load function ends - I am enabling another windows form full of buttons which are meant to be changing the flag statuses depending on which button the user presses. So when user presses the button on the second form - it run the function which is supossed to change flag status on the first form and after that it closses second form. The thing is, that the flag status is not being changed, so if I want to set this flag status to true, it's all the time false. I don't know why. Below is the example code: // declaration of values private bool isFirstActive; private bool isSecondActive; private bool isThirdActive; //splash screen form private SplashScreen splashScreen; //second menu form private SecondMenu secondForm; // initialization in constructor public MainForm() { //show the splash screen to the user while loading splashScreen = new SplashScreen(); splashScreen.Owner = this; splashScreen.Show(); Application.DoEvents(); this.Enabled = false; InitializeComponent(); isFirstActive = false; isSecondActive = false; isThirdActive = false; } private void MainForm_Load(object sender, EventArgs e) { //enable the main form this.Enabled = true; this.Visible = true; //close the splash screen splashScreen.Close(); //enable the second form secondForm = new FacultyMenu(); secondForm.Owner = this; secondForm.ShowDialog(); } So now the user will see second form with some buttons, below the code for the second form with example button click: private MainForm firstForm; public SecondMenu() { firstForm = new MainForm(); InitializeComponent(); } private void btnChangeSecond_Click(object sender, EventArgs e) { firstForm.changeSecondFlag(); Close(); } So when user press the button, it will call the following method on the first form: public void changeSecond() { isFirstActive = false; isSecondActive = true; isThirdActive = false; } Then it's closing the second form and goes back to the main form. but the isSecondActive valuse is still false. Why?
-
When I run my application I am initializing some boolean flags - all of them are set to false at the begining, then just before the Load function ends - I am enabling another windows form full of buttons which are meant to be changing the flag statuses depending on which button the user presses. So when user presses the button on the second form - it run the function which is supossed to change flag status on the first form and after that it closses second form. The thing is, that the flag status is not being changed, so if I want to set this flag status to true, it's all the time false. I don't know why. Below is the example code: // declaration of values private bool isFirstActive; private bool isSecondActive; private bool isThirdActive; //splash screen form private SplashScreen splashScreen; //second menu form private SecondMenu secondForm; // initialization in constructor public MainForm() { //show the splash screen to the user while loading splashScreen = new SplashScreen(); splashScreen.Owner = this; splashScreen.Show(); Application.DoEvents(); this.Enabled = false; InitializeComponent(); isFirstActive = false; isSecondActive = false; isThirdActive = false; } private void MainForm_Load(object sender, EventArgs e) { //enable the main form this.Enabled = true; this.Visible = true; //close the splash screen splashScreen.Close(); //enable the second form secondForm = new FacultyMenu(); secondForm.Owner = this; secondForm.ShowDialog(); } So now the user will see second form with some buttons, below the code for the second form with example button click: private MainForm firstForm; public SecondMenu() { firstForm = new MainForm(); InitializeComponent(); } private void btnChangeSecond_Click(object sender, EventArgs e) { firstForm.changeSecondFlag(); Close(); } So when user press the button, it will call the following method on the first form: public void changeSecond() { isFirstActive = false; isSecondActive = true; isThirdActive = false; } Then it's closing the second form and goes back to the main form. but the isSecondActive valuse is still false. Why?
acont wrote:
So when user press the button, it will call the following method on the first form:
No, it will call a method on an object that has the class-type as defined for the class "MainForm". There's two of those objects; one that's your actual mainform, and that you use to open the second form, and then there's a new object of the same type, created in the
SecondMenu
method.acont wrote:
Then it's closing the second form and goes back to the main form. but the isSecondActive valuse is still false.
It's closing the second form, and the mainform contained therein (in the private variable firstForm). Then it goes back to the original mainform. The boolean is set in the second instance, the original object isn't changed. How about passing a reference to your mainform in the constructor of the second form? You could cache a reference to that instance, changing things on that would correctly change the original object.
I are Troll :suss: