如何处理表单的KeyPress事件?

时间:2015-05-12 14:29:15

标签: c# .net winforms

我正在编写一个程序,我正在使用此代码,

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{          
    if (e.KeyChar >= 48 && e.KeyChar <= 57)
    {
        txtbox.Text = txtbox.Text + e.KeyChar.ToString();
    }
}

但我没有按下文本框上的按键。我无法找到失败原因。

1 个答案:

答案 0 :(得分:1)

根据this,您必须将Form.KeyPreview设置为<?php /** * Created by PhpStorm. * User: avivpaz * Date: 12/25/14 * Time: 12:07 PM */ namespace PicoCore\Objects; class User extends APIUser implements ConvertibleObjects { /** * @var string User's Name */ public $name; /** * @var string User's Push Id */ public $pushId; /** * @var UserDevice User's device type */ public $userDevice; /** * @var double The location latitude */ public $latitude; /** * @var double The location longitude */ public $longitude; public function __construct() { $this->label = USER_LABEL; } /** * init with APIUser * @param $APIUser APIUser * @return User type */ public static function withAPIUser($APIUser) { $user = new User(); $user->label=USER_LABEL; $user->id = $APIUser->id; $user->facebookAccessToken = $APIUser->facebookAccessToken; $user->picoAccessToken = $APIUser->picoAccessToken; $user->accessTokenExpires = $APIUser->accessTokenExpires; return $user; } public function ConvertObjectToCypherSyntax() { return '{id:"' . $this->id . '",name:"' . $this->name . '",picoAccessToken:"' . $this->picoAccessToken . '",accessTokenExpires:"' . $this->accessTokenExpires->getTimestamp() . '",latitude:' . $this->latitude . ',longitude:' . $this->longitude . '}'; } static public function ConvertNodeToObject($results) { $user = new User(); if (is_null($results)) return $user; $user->id = $results->getProperty('id'); $user->name = $results->getProperty('name'); $user->picoAccessToken = $results->getProperty('picoAccessToken'); $user->latitude = $results->getProperty('latitude'); $user->longitude = $results->getProperty('longitude'); // $user->userDevice = UserDevice::ConvertNodeToObject($results['userDevice']); if (!$results->getProperty('accessTokenExpires') == "") $user->accessTokenExpires = getDateTimeFromTimeStamp($results->getProperty('accessTokenExpires')); return $user; } static public function ConvertArrayToObject($array) { $user = new User(); if (array_key_exists("id", $array)) $user->id = $array["id"]; if (array_key_exists("name", $array)) $user->name = $array["name"]; if (array_key_exists("pico_access_token", $array)) $user->picoAcceessToken = $array["pico_access_token"]; if (array_key_exists("access_token_expires", $array)) $user->accessTokenExpires = getDateTimeFromString($array["access_token_expires"]); if (array_key_exists("device_type", $array)) $user->deviceType = $array["device_type"]; if (array_key_exists("latitude", $array)) $user->latitude = $array["latitude"]; if (array_key_exists("longitude", $array)) $user->longitude = $array["longitude"]; if (array_key_exists("userDevice", $array)) $user->userDevice = UserDevice::ConvertArrayToObject($array["userDevice"]); return $user; } public function toArray() { $user = array(); if (!empty($this->id)) $user["id"] = $this->id; if (!empty($this->name)) $user["name"] = urldecode($this->name); if (!empty($this->picoAccessToken)) $user["pico_access_token"] = $this->picoAccessToken; if (!empty($this->accessTokenExpires)) $user["access_token_expires"] = getMysqlDateTime($this->accessTokenExpires); if (!empty($this->latitude)) $user["latitude"] = $this->latitude; if (!empty($this->longitude)) $user["longitude"] = $this->longitude; if (isset($this->userDevice)) { $userDevice = $this->userDevice->toArray(); if (!empty($userDevice)) $user["userDevice"] = $userDevice; } if (count($user) > 0) return $user; return null; } } ,默认为false。否则,您无法处理表单的true事件。

  

当此属性设置为KeyPress时,表单将收到所有true,   KeyPressKeyDown个事件。