I have a working WinForm that handles the search functionality in my customer database. It contains the following controls:
- A textBox where you type your search string (A)
- A ‘search’ button (B)
- A DataGridView (C) that returns the result of the search
- An ‘OK’ button (D) and a ‘Cancel’ button (E)
What I am trying to accieve next is this:
- When A is active the AcceptButton (enter) should be linked to B
- When B or enter is pressed C should become active
- When C is active the AcceptButton should be linked to D
I realise this is a somewhat big question so don’t hesitate to answer just one of the bullet marks if you know the answer.
EDIT: I have solved the implementation of requirement 1 and 3 above, but I am still looking for an answer to the second one. To clarify, when the search is initiated (meaning i have pressed enter on the keyboard or the search button with the mouse) I want focus to shift to the first line in the DataGridView.
When you get the text changed event for textBox set the AcceptButton to be ‘Search’:
There will probably want to be more code in here, checking the length of the string etc.
Then once you’ve done the search and filled in the DataGridView you can then set the AcceptButton to be ‘OK’.
Though you probably wouldn’t want to use this event.
‘Cancel’ is always the CancelButton.
EDIT: OK for part 2 you want the following code:
This will make it active as soon as the search button is pressed, but you probably want to do it when the DataGridView has been populated – just in case the search returned no results.