locked
Exit from button by tabbing to be disabled RRS feed

  • Question

  • I have a form with a command button.

    I do not want the user to exit from this button by tabbing, as the click event does not fire when he/she exits by a tab.

    Can this be achieved?

    Thanks

    Mohan


    MohanSQL

    Tuesday, December 15, 2015 8:40 AM

Answers

  • Dear Fouad              16 Dec

    Thank you!

    When the command button gets focus, the user can either click on Button (or hit enter key) executing the code in button_click, or press Tab, which will take the user to the next control in Tab order without executing button_click.

    I do not want the user to exit from button without executing button_click.

    I hope I have explained my situation.

    Thanks

    Mohan


    MohanSQL

    • Marked as answer by MohanSQL Thursday, December 17, 2015 3:03 AM
    Wednesday, December 16, 2015 3:33 AM

All replies

  • I didn't get your question, the Tab button is used for navigation over the form controls and it won't cause the exit to happen unless there is custom code behind doing this. So how the user is able to exit by Tab button in your case?

    Fouad Roumieh

    Tuesday, December 15, 2015 9:21 AM
  • This does seem a bit of an odd requirement.

    You can handle ProcessCmdKey using an override and check which control has focus.

    You also need to set KeyPreview true on the window.

            public Form1()
            {
                InitializeComponent();
                this.KeyPreview = true;
            }
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                if (keyData == Keys.Tab)
                {
                    return button1.Focused;
                }
    
                return base.ProcessCmdKey(ref msg, keyData);
            }
    The above will just stop tab working whilst button1 is focussed.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, December 15, 2015 11:06 AM
  • Dear Fouad              16 Dec

    Thank you!

    When the command button gets focus, the user can either click on Button (or hit enter key) executing the code in button_click, or press Tab, which will take the user to the next control in Tab order without executing button_click.

    I do not want the user to exit from button without executing button_click.

    I hope I have explained my situation.

    Thanks

    Mohan


    MohanSQL

    • Marked as answer by MohanSQL Thursday, December 17, 2015 3:03 AM
    Wednesday, December 16, 2015 3:33 AM
  • Hi aNDY     16 Dec

    Thank you. Looks like overkill, but will do what I want.

    Currently I use validation logic to discard work where button_click has not been executed

    Mohan


    MohanSQL

    Wednesday, December 16, 2015 3:36 AM

  • I do not want the user to exit from button without executing button_click.


    Normally we put Button controls on the Form to allow the user to interact with the code execution. If if we want to force something to be clicked we normally alert the user, like: Please click save button to save changes to the DB.

    For example, the Form has FormClosing event that you can use and execute code the code when form is Closing.

     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (commandExecuted==false) {
                    MessageBox.Show("Please click button.");
                    e.Cancel = true;
                }
                
            }

    I don't prefer that you force/block any UX functionality that is known for a user, if the user is clicking TAB button it should execute as supposed, for me if I set infront of an application that is blocking the Tab or the Enter key, I'll maybe throw the laptop from the window. 

    It will be better to tell about the functionality behind this scenario, so maybe we help with a better idea, I mean what is the reason behind that.


    Fouad Roumieh

    Wednesday, December 16, 2015 4:25 AM
  • Dear Fouad                16 Dec

    Thank you! And I agree with you completely that the designer should NOT change the default functionality of the I/F.

    In my case, the form has about 40 controls, almost all of which are exited with TAB.

    Command button Button is in the middle of the tab order. When the user clicks this button, It asks the user if he is happy. If he is not, the form lets him make correction to a few textboxes. If he is happy, it appends a finished record to a table on the DBO, and then proceeds to activate an area of the form where slave records for the one appended are entered.

    If the user exits Button with a Tab, then the above code is not executed, an focus goes to the next enabled control, which is unfortunately an EXIT button,  leaving an inconsistent order.

    I could not put the "Are you OK" question in the validating event of the previous control, since Button can get focus from either of two controls, and putting the question in the validating event of both can give rise to situation where the question is asked twice.

    The system is a date entry form for orders executed from multiple locations across the country, and I want NO confusion to be created in the minds of the remote users when there is no system person to advise.

    I am currently checking the consistency in a later validation routine

    I hope I am clear: We can have a variety of validation routines, but I would like to help the user to enter data where validation routine do not catch errors

    Mohan


    MohanSQL

    Wednesday, December 16, 2015 4:45 AM
  • You are making confusion when you say "all of which are exited with TAB". Lets say are navigated with the TAB key.

    I understand that you have an Exit button on the Form that exits/closes the Form, and you don't want this button to be clicked when there are changes made and if it is clicked without Command/Save button those changes are lost. If this is the scenario you are facing then you simply enable/disable the exit button by tracking changes on the form.

    • Lets say we have a Form that contains 1 Text box and a Save (Disabled by default) and Exit (Enabled by default) button.
    • From within TextChanged event of the TextBox I can disable Exit button and Enable Save button 
    • If no changes were made (TextChanged not fired) then Exit will be enabled and to exit in this case without clicking save button should not be a problem.


    Fouad Roumieh

    Wednesday, December 16, 2015 5:11 AM
  • Dear Fouad            16 Dec

    Yes: I will have to do something like this.

    However, if I disable the EXIT button, anyway the next control in the tab order gets focus.

    I am now leaving the form as is, and using a validation routine to prevent the DBO being updated with inconsistent data till I get a user-friendly solution.

    Thanks again

    Mohan


    MohanSQL

    Wednesday, December 16, 2015 5:20 AM
  • Hi MohanSQL,

    I'm not really clear where you're going with this one.

    If you have the solution for what you asked for then please consider marking a post as answer.

    The forum threads are read and searched by many people so even if you've changed your mind and decided to do something else then a post which answers a question is still a valid answer to that question. Other people may want it.

    .

    As I implied earlier, I think stopping tab working is usually a bad plan unless you know users will definitely work using keyboard only.

    That's a real possibility in some environments so I guessed maybe your users have no mouse or are very keyboard focussed somehow.

    Generally speaking, what I do is use Validator.tryvalidateobject to check a whole object is valid before enabling a button which will save or when the user hits that button in order to report errors.

    That's rather a different question though.

    If you have further questions on this process please start a new thread. Each one should be one question and the header should define that for searches. Think of it as a support ticket you raise which forms part of a knowledge base of resources.

    .

    Thank you for your consideration.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Wednesday, December 16, 2015 11:38 AM
  • Hi Andy!

    As I mentioned earlier, there are about 40 controls in the form: most of which are text boxes or list/comboboxes.

    These cannot be exited by the Enter key: only by the Tab key. The Tab order is very specific.

    After about 20 controls, I have a command button. When this is Clicked or the Enter Key, Button_click code runs which asks for confirmation.

    If the answer is no, the user is focused back at an earlier control to correct entries.

    If the answer is Yes, information is appended to a table in SQL, and additional controls are enabled which accept inputs which create slave recordS for the record just appended.

    Button_Click code does not execute if the Command Button is exited with the Tab key. I want to avoid this situation of the command button code not being executed

    I may be committing some error on the way. Regret if I have caused any confusion.

    Mohan


    MohanSQL

    Thursday, December 17, 2015 3:11 AM