3

KeyPressイベントでDataGridViewの一部のデータを検索し、入力文字列が見つかった行をフォーカスするメソッドがあります(見つかった場合)。

private string input;
    private void Find_Matches()
    {            
        if (input.Length == 0) return;

        for (int x = 0; x < dataGridViewCustomers.Rows.Count; x++)
            for (int y = 0; y < dataGridViewCustomers.Columns.Count; y++)
                if (dataGridViewCustomers.Rows[x].Cells[y].Value.ToString().Contains(input))
                    dataGridViewCustomers.Rows[x].Selected = true;

     }

private void dataGridViewCustomers_KeyPress(object sender, KeyPressEventArgs e)
    {            
        input += e.KeyChar.ToString();
        Find_Matches();
    }

キーを押す間の遅延をカウントし、1秒を超える場合は、「入力」文字列をクリアする方法を教えてください。中断のない検索に必要です。

ありがとう。

4

4 に答える 4

1

System.Threading.Timerを使用する必要があります。入力をクリアするタイマーにコールバックを渡します。KeyPressイベントが発生するたびに、タイマーの間隔を1000ミリ秒に更新する必要があります

timer.Change(0,1000);

また

timer.Change(1000,0);

Changeメソッドの正しい引数シーケンスを覚えていません。試してみてください

于 2012-09-10T10:33:39.140 に答える
1

それを活用System.Timers.Timerすることは次のように行われます:

private Timer myTimer = new Timer(1000); //using System.Timers, 1000 means 1000 msec = 1 sec interval

public YourClassConstructor()
{
    myTimer.Elapsed += TimerElapsed;
}

private void TimerElapsed(object sender, EventArgs e)
{
    input = string.Empty;
    myTimer.Stop();
}

// this is your handler for KeyPress, which will be edited
private void dataGridViewCustomers_KeyPress(object sender, KeyPressEventArgs e)
{            
    if (myTimer.Enabled) myTimer.Stop(); // interval needs to be reset
    input += e.KeyChar.ToString();
    Find_Matches();
    myTimer.Start(); //in 1 sec, "input" will be cleared
}
于 2012-09-10T10:46:04.230 に答える
0

1秒間隔に設定されたタイマーを使用し、KeyPress()メソッドでタイマーをリセットできます。また、ハンドラーのタイマーをリセットします。これにより、最後のキーストロークから1秒が経過すると、タイマーのハンドラーが呼び出されます。

タイマーのハンドラー内で、最後のキーストロークから1秒が経過したときに必要なことをすべて実行します。

このタイマーの使用をお勧めします:http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

SynchronizingObjectをフォーム/コントロールに設定することで、UIスレッド(これはあなたが望むと思います)でコールバックさせることができます。

http://msdn.microsoft.com/en-us/library/system.timers.timer.synchronizingobject.aspx

于 2012-09-10T10:33:25.297 に答える
0

最初に新しいものを作成し、System.Windows.Forms.Timer次のように構成します。

_TimerFilterChanged.Interval = 800;
_TimerFilterChanged.Tick += new System.EventHandler(this.OnTimerFilterChangedTick);

次に、コードに次のメソッドを追加します。

private void OnTimerFilterChangedTick(object sender, EventArgs e)
{
    _TimerFilterChanged.Stop();
    Find_Matches();
}

キープレスイベントハンドラーは、次のように変更する必要があります。

private void dataGridViewCustomers_KeyPress(object sender, KeyPressEventArgs e)
{            
    input += e.KeyChar.ToString();
    _TimerFilterChanged.Stop();
    _TimerFilterChanged.Start();
}
于 2012-09-10T10:41:00.473 に答える