Warn user with messageBox if they navigate away from the current user control

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
I've tried to achieve this by creating a leave event on the user control, but the message Box isn't appearing for some reason. I also have a panel in front of the user control which I'm thinking might be the issue? Below is the code i have so far:

C#:
private void Uc_inputtingPaymentDetails_Leave_1(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you want to navigate away from this page?", "Stop", MessageBoxButtons.YesNo);

            if (dialogResult == DialogResult.Yes)
            {
                string query = "DELETE from Bookings WHERE bookingId = (select max(BookingId) FROM Bookings)";
                _IsqlDataFunctions.ManagingData(query);
                string query_1 = "UPDATE Coach set numberOfSeats = numberOfSeats+1 WHERE CoachId = '" + CoachId + "' ";
                _IsqlDataFunctions.ManagingData(query_1);
                ShowUserControl(new Uc_addBooking());

            }

I have a Modify journey button on the panel, which works fine with the code above, however if i attempt to click on one of the menu tabs across the top such as "My Account" or "Add Booking" which takes the user away from the payment screen i want the application to trigger a MessageBox.

EDIT: when i click on one of the tabs across the top, the message box appears, however if i attempt to put through another booking, get to the payment stage and click on "my account" again. The messageBox doesn't appear
 
Last edited:
To reproduce I created a user control with a textbox on it. Added an instance to a form and attached Leave event handler.
When I run the application and leave the textbox (and enter another control on form) the Leave event is raised for user control.
I also have a panel in front of the user control
What?
 
To reproduce I created a user control with a textbox on it. Added an instance to a form and attached Leave event handler.
When I run the application and leave the textbox (and enter another control on form) the Leave event is raised for user control.

What?
in my user control i have a panel, which has all of the controls attached to it.
1616191901968.png


so i have the userControl and then the panel in front of it.
 
in my user control i have a panel, which has all of the controls attached to it.
So: user control > panel > textbox

Still works fine. Leave event cascades through those layers.
 
sorry I'm so confused, I'm pretty new to windows forms apologies. so I will need to create 3 leave events ?
 
No.
 
okay I've made some progress, I've managed to get the message box to appear when the user clicks on the menu buttons, however the message will always trigger despite what user control I'm on. I need the message Box to only appear when the user has accessed the uc_paymentdetails panel. I was thinking of putting something in the if statement along the lines of "if paymentPanel is at the front" only then can the message box be triggered but I'm not sure how to do this, and i know its probably something simple.

This is what i have now. The problem I have with this is that the paymentPanel is always visible, so the Message box is always going to be triggered. I tried putting the visibility to off, and then in a load event turn the visibility to true, but that didn't work.

C#:
       public void WarningUser(Control control)
        {
            if(paymentPanel.Visible == true)
            {
                DialogResult dialogResult = MessageBox.Show("Are you want to navigate away from this page?", "Stop", MessageBoxButtons.YesNo);

                if (dialogResult == DialogResult.Yes)
                {
                    string query = "DELETE from Bookings WHERE bookingId = (select max(BookingId) FROM Bookings)";
                    _IsqlDataFunctions.ManagingData(query);
                    string query_1 = "UPDATE Coach set numberOfSeats = numberOfSeats+1 WHERE CoachId = '" + CoachId + "' ";
                    _IsqlDataFunctions.ManagingData(query_1);
                    ShowUserControl(control);
                }
                else if (dialogResult == DialogResult.No)
                {
                    paymentPanel.Focus();                  
                }
            }
            else
            {
                ShowUserControl(control);
            }


Here is what i have in the menu Button:

C#:
        private void button4_Click(object sender, EventArgs e)
        {
            _InputtingPaymentDetails.WarningUser(new User_Control.Uc_addBooking());          
        }

Does anyone have any suggestions on how i could solve this?
 

Latest posts

Back
Top Bottom