Hi
I have a user control which has 1 textbox and a Save button.If a user type some text in the textbox and forgets to save and moves to another tab,I want to show a dialog result.If the user selects yes then the user will move to another tab.But if the user selects no I want the user to stay in this form.How can I do it?
thnx in advance

I not sure how you can do it from within your user control, but you can experiment around using the tabcontrol's selecting event handler. Have the save button set the _saved var.

public partial class Form1 : Form
    {
        private bool _saved = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if ( !string.IsNullOrEmpty(textBox1.Text) && !_saved && !e.TabPage.Equals(textBox1.Parent))
            {
                if (MessageBox.Show("You have not saved\r\nDo you really want to leave me?"
                    , "Not Saved"
                    , MessageBoxButtons.YesNo
                    , MessageBoxIcon.Question) == DialogResult.No)
                {
                    e.Cancel = true;
                    textBox1.Focus();
                }
            }
        }
    }

Hi
I have a user control which has 1 textbox and a Save button.If a user type some text in the textbox and forgets to save and moves to another tab,I want to show a dialog result.If the user selects yes then the user will move to another tab.But if the user selects no I want the user to stay in this form.How can I do it?
thnx in advance

Hi
Thnx,but i dont have tab control selecting evevt in my user controls.So I am unable to do it.

Am i correct in thinking you placed your user control inside a tabControl? If not, what do you mean by moving to another tab?
If you have used a TabControl then this has a TabIndexChanged event, alternatively, each TabPage in the control has its own Enter and Leave events which fire when you move between the tabs.
Check my tutorial to see how you can view a controls events and create the event handlers for them.

This article has been dead for over six months. Start a new discussion instead.