本文发表在 rolia.net 枫下论坛vb.net会 ,但是用着不习惯,用C#来写。
此题关键是考你event和win form 键盘输入的知识,还是能考出面试者一些功底的,太晚了,明天再作上面2道
// define a bool just to let listbox judge whether to run ABC() or not
private bool DownKeyPressed=false;
private void Form1_Load(object sender, System.EventArgs e)
{
for(int i=1;i<=1000;i++)
this.listBox1.Items.Add(i);
// add the key up and down event to the listbox
this.listBox1.KeyDown+=new KeyEventHandler(OnKeyDown);
this.listBox1.KeyUp+=new KeyEventHandler(OnKeyUp);
}
// the key up handler function
void OnKeyUp(object o, KeyEventArgs e)
{
if(e.KeyCode==Keys.Down)
{
int i=this.listBox1.SelectedIndex;
this.listBox1.SelectedIndex=0;
this.DownKeyPressed=false;
this.listBox1.SelectedIndex=i;
}
}
// the key down handler function
void OnKeyDown(object o,KeyEventArgs e)
{
if(e.KeyCode==Keys.Down)
this.DownKeyPressed=true;
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(this.DownKeyPressed==true && this.listBox1.SelectedIndex<999)
return;
ABC();
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
此题关键是考你event和win form 键盘输入的知识,还是能考出面试者一些功底的,太晚了,明天再作上面2道
// define a bool just to let listbox judge whether to run ABC() or not
private bool DownKeyPressed=false;
private void Form1_Load(object sender, System.EventArgs e)
{
for(int i=1;i<=1000;i++)
this.listBox1.Items.Add(i);
// add the key up and down event to the listbox
this.listBox1.KeyDown+=new KeyEventHandler(OnKeyDown);
this.listBox1.KeyUp+=new KeyEventHandler(OnKeyUp);
}
// the key up handler function
void OnKeyUp(object o, KeyEventArgs e)
{
if(e.KeyCode==Keys.Down)
{
int i=this.listBox1.SelectedIndex;
this.listBox1.SelectedIndex=0;
this.DownKeyPressed=false;
this.listBox1.SelectedIndex=i;
}
}
// the key down handler function
void OnKeyDown(object o,KeyEventArgs e)
{
if(e.KeyCode==Keys.Down)
this.DownKeyPressed=true;
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(this.DownKeyPressed==true && this.listBox1.SelectedIndex<999)
return;
ABC();
}更多精彩文章及讨论,请光临枫下论坛 rolia.net