ListBox 中怎样实现 KeyPress 事件  Delete  

解决方案 »

  1.   

    在<body>中加入onload="initbox();"
    <script type="text/javascript">
    function initbox(){
      var opt=document.getElementById("listbox的ID").getElementsByTagName("OPTION");
      for(var i=0;i<opt.length;i++){
        opt[i].onkeypress=function {optkeypress(this);};
      }
    }
    function optkeypress(Obj){
    //这里是你要的操作
      alert('abc');
    }
    </script>
    随手写的代码,没有调试,只是个大概意思,有问题再说吧
      

  2.   


     private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Delete)
                {
                    if (listBox1.SelectedItem != null)
                    {
                        listBox1.Items.Remove(listBox1.SelectedItem);
                    }
                }
            }        private void listBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Delete)
                {
                    if (listBox1.SelectedItem != null)
                    {
                        listBox1.Items.Remove(listBox1.SelectedItem);
                    }
                }
            }注意, KeyPress是指输入字符如果用户按下方向键、功能键是不会触发KeyPress事件的, 所以, 上面的KeyPress事件中, 只能检测到数字键盘的Del(也就是小数点键), 而不能检测到功能键Delete, 这时候, 也只能用KeyDown来辅助
      

  3.   

    呵,你写的是服务端代码,我的是客户端的,也不知道人家想要什么,唉,现在人问个问题都好懒,都不想说清楚,....
    ps:keypress是不认识delete的,如果要必须用keydown或keyup