views:

346

answers:

4

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:

  1. When A is active the AcceptButton (enter) should be linked to B
  2. When B or enter is pressed C should become active
  3. 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.

+1  A: 

Setup handlers to track the Control.Enter event for the particular controls. When the enter is reached try:

this.AcceptButton = ControlThatShouldBeAcceptButton;
Quintin Robinson
+1  A: 

private void WhateverControl_Enter(...) { this.AcceptButton = myButton; }

SnOrfus
+2  A: 

When you get the text changed event for textBox set the AcceptButton to be "Search":

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        this.AcceptButton = SearchButton;
    }

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".

    private void dataGridView1_DataMemberChanged(object sender, EventArgs e)
    {
        this.AcceptButton = OKButton;
    }

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:

    private void SearchButton_Click(object sender, EventArgs e)
    {
        dataGridView1.Focus();
    }

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.

ChrisF
So i guess that means another dataGridView_DataMemberChanged for part 2?
Sakkle
@Sakkle: No - you could put the dataGridView1.Focus() into the same event handler.
ChrisF
Yepp... I figured that out :) Thanks
Sakkle
+1  A: 

You should implement an event listener to DataGridViewBindingCompleteEventHandler. In that event you could add the following code:

{
    if(grid.Rows.Count > 0)
    {
        grid.ClearSelection();
        grid.Rows[0].Selected = true;
    }
    grid.Focus();
}
Wouter