• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            twzheng's cppblog

            『站在風(fēng)口浪尖緊握住鼠標(biāo)旋轉(zhuǎn)!』 http://www.cnblogs.com/twzheng

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              136 隨筆 :: 78 文章 :: 353 評論 :: 0 Trackbacks
            .NET Framework 類庫  

            Control.KeyPress 事件

            在控件有焦點(diǎn)的情況下按下鍵時發(fā)生。

            
            
            [Visual Basic]
            Public Event KeyPress As KeyPressEventHandler
            [C#]
            public event KeyPressEventHandler KeyPress;
            [C
            ++]
            public: __event KeyPressEventHandler* KeyPress;

            [JScript] 在 JScript 中,可以處理由某個類定義的事件,但不能定義自己的事件。

            事件數(shù)據(jù)

            事件處理程序接收一個 KeyPressEventArgs 類型的參數(shù),它包含與此事件相關(guān)的數(shù)據(jù)。下列 KeyPressEventArgs 屬性提供特定于此事件的信息。

            屬性 說明
            Handled 獲取或設(shè)置一個值,該值指示是否處理過 KeyPress 事件。
            KeyChar 獲取與按下的鍵對應(yīng)的字符。

            備注

            鍵事件按下列順序發(fā)生:

            1. KeyDown
            2. KeyPress
            3. KeyUp

            非字符鍵不會引發(fā) KeyPress 事件;但非字符鍵卻可以引發(fā) KeyDownKeyUp 事件。

            要僅在窗體級別處理鍵盤事件并且不允許其他控件接收鍵盤事件,請將窗體的 KeyPress 事件處理方法中的 KeyPressEventArgs.Handled 屬性設(shè)置為 true。

            有關(guān)處理事件的更多信息,請參見使用事件

            .NET Framework 精簡版平臺說明:  除了 Control 基類外,.NET Compact Framework 還支持具有 Service Pack 2 及更高版本的控件上的該事件。Smart Devices Developer Community(智能設(shè)備開發(fā)人員團(tuán)體)Web 站點(diǎn)提供了有關(guān)可用 Service Pack 的信息,請參見 http://go.microsoft.com/fwlink/?LinkId=16561。

            示例

            
            
            [Visual Basic] 
            ' Boolean flag used to determine when a character other than a number is entered.
            Private nonNumberEntered As Boolean = False
               
               
            ' Handle the KeyDown event to determine the type of character entered into the control.
            Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
                 
            Handles textBox1.KeyDown
                
            ' Initialize the flag to false.
                nonNumberEntered = False
              
                
            ' Determine whether the keystroke is a number from the top of the keyboard.
                If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
                    
            ' Determine whether the keystroke is a number from the keypad.
                    If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                        
            ' Determine whether the keystroke is a backspace.
                        If e.KeyCode <> Keys.Back Then
                            
            ' A non-numerical keystroke was pressed. 
                            ' Set the flag to true and evaluate in KeyPress event.
                            nonNumberEntered = True
                        
            End If
                    
            End If
                
            End If
            End Sub
             'textBox1_KeyDown
               
               
            ' This event occurs after the KeyDown event and can be used 
            '
             to prevent characters from entering the control.
            Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _
                
            Handles textBox1.KeyPress
                
            ' Check for the flag being set in the KeyDown event.
                If nonNumberEntered = True Then
                    
            ' Stop the character from being entered into the control since it is non-numerical.
                    e.Handled = True
                
            End If
            End Sub
             'textBox1_KeyPress
            End Class 'Form1 
            [C#] 
            // Boolean flag used to determine when a character other than a number is entered.
            private bool nonNumberEntered = false;

            // Handle the KeyDown event to determine the type of character entered into the control.
            private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
            {
                
            // Initialize the flag to false.
                nonNumberEntered = false;

                
            // Determine whether the keystroke is a number from the top of the keyboard.
                if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
                
            {
                    
            // Determine whether the keystroke is a number from the keypad.
                    if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                    
            {
                        
            // Determine whether the keystroke is a backspace.
                        if(e.KeyCode != Keys.Back)
                        
            {
                            
            // A non-numerical keystroke was pressed.
                            
            // Set the flag to true and evaluate in KeyPress event.
                            nonNumberEntered = true;
                        }

                    }

                }

            }


            // This event occurs after the KeyDown event and can be used to prevent
            // characters from entering the control.
            private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
                
            // Check for the flag being set in the KeyDown event.
                if (nonNumberEntered == true)
                
            {
                    
            // Stop the character from being entered into the control since it is non-numerical.
                    e.Handled = true;
                }

            }

            [C++
            // Boolean flag used to determine when a character other than a number is entered.
            private:
            bool nonNumberEntered;

            // Handle the KeyDown event to determine the type of character entered into the control.
            void textBox1_KeyDown(Object* /*sender*/, System::Windows::Forms::KeyEventArgs* e)
            {
               
            // Initialize the flag to false.
               nonNumberEntered = false;

               
            // Determine whether the keystroke is a number from the top of the keyboard.
               if (e->KeyCode < Keys::D0 || e->KeyCode > Keys::D9)
               
            {
                  
            // Determine whether the keystroke is a number from the keypad.
                  if (e->KeyCode < Keys::NumPad0 || e->KeyCode > Keys::NumPad9)
                  
            {
                     
            // Determine whether the keystroke is a backspace.
                     if(e->KeyCode != Keys::Back)
                     
            {
                        
            // A non-numerical keystroke was pressed.
                        
            // Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = true;
                     }

                  }

               }

            }


            // This event occurs after the KeyDown event and can be used to prevent
            // characters from entering the control.
            void textBox1_KeyPress(Object* /*sender*/, System::Windows::Forms::KeyPressEventArgs* e)
            {
               
            // Check for the flag being set in the KeyDown event.
               if (nonNumberEntered == true)
               
            {
                  
            // Stop the character from being entered into the control since it is non-numerical.
                  e->Handled = true;
               }

            }

            [JScript] 沒有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,請單擊頁左上角的“語言篩選器”按鈕 語言篩選器。

            要求

            平臺: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列

            請參見

            Control 類 | Control 成員 | System.Windows.Forms 命名空間 | OnKeyPress

            posted on 2007-05-17 00:02 譚文政 閱讀(1154) 評論(0)  編輯 收藏 引用 所屬分類: vc++.net
            久久er99热精品一区二区| 久久人妻无码中文字幕| 久久婷婷国产麻豆91天堂| 国产精自产拍久久久久久蜜| 性做久久久久久免费观看| 久久99国内精品自在现线| 午夜精品久久久内射近拍高清 | 久久久久人妻一区精品性色av| 99久久精品国产免看国产一区| 久久一区二区三区99| 国产亚洲美女精品久久久久狼| 无码任你躁久久久久久久| 国产一区二区三区久久精品| 精品伊人久久久| 精品久久久久久国产三级| 97久久天天综合色天天综合色hd| 人人狠狠综合88综合久久| 日本一区精品久久久久影院| 欧美黑人激情性久久| 亚洲а∨天堂久久精品9966| 91超碰碰碰碰久久久久久综合| 国产精品久久午夜夜伦鲁鲁| 久久久久亚洲精品日久生情 | 久久久精品国产Sm最大网站| 999久久久免费精品国产| 亚洲精品乱码久久久久久久久久久久 | 欧美精品九九99久久在观看| 久久国产成人午夜AV影院| 国产福利电影一区二区三区,免费久久久久久久精 | 99久久99久久| 精品久久久久久国产| 久久A级毛片免费观看| 久久精品人人做人人爽97| 久久久久亚洲AV无码专区体验| 欧美一区二区三区久久综| 久久亚洲精品无码AV红樱桃| 伊人久久久AV老熟妇色| 久久婷婷色综合一区二区| 无遮挡粉嫩小泬久久久久久久| 国产成人精品综合久久久久| 久久这里只有精品18|