1

私は何を間違っていますか?これが私が得るエラーコードです:

「System.EventArgs」には「KeyCode」の定義が含まれておらず、タイプ「System.EventArgs」の最初の引数を受け入れる拡張メソッド「KeyCode」が見つかりませんでした (using ディレクティブまたはアセンブリ参照がありませんか?)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BroZer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Reload_Click(object sender, EventArgs e)
        {
            webBrowser1.Refresh();
        }

        private void Go_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(textBox1.Text);
        }

        private void Back_Click(object sender, EventArgs e)
        {
            webBrowser1.GoBack();
        }

        private void Forward_Click(object sender, EventArgs e)
        {
            webBrowser1.GoForward();
        }

        private void textBox1_KeyDown(object sender, EventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                webBrowser1.Navigate("https://www.google.com/search?&ie=UTF-8&q=" + textBox1.Text);
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void Save_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowSaveAsDialog();
        }

        private void Print_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPrintPreviewDialog();
        }
    }
}
4

3 に答える 3

3

更新:私はそれを理解しました。あなたがしなければならないのは変更することだけです

(object sender, EventArgs e)

(object sender, KeyEventArgs e)
于 2012-07-04T01:20:43.460 に答える
2

IDE (つまり、Visual Studio) を最大限に活用するには、次のように入力します: textBox1.KeyDown +=TabTab

イベントの委任されたメソッド定義の正確な署名が得られます。


KeyDown のコードが非常に単純で、別のメソッドに配置したくない場合は、コードをインライン化することを選択できます。つまり、コードをラムダに埋め込むことができます。

textBox1.KeyDown += (s,e) => 
    if (e.KeyCode == Keys.Enter) 
        webBrowser1.Navigate(
            "https://www.google.com/search?&ie=UTF-8&q=" + textBox1.Text);

オートコンプリートは引き続きラムダで適用されます。つまり、 this:if (e.と入力KeyCodeすると、オートコンプリートのドロップダウン リストに表示されます。ラムダでは、委任されたメソッドの正確なシグネチャを知る必要はありません。

于 2012-07-04T01:27:06.783 に答える
0

KeyDownのイベントをtextBox1toに割り当てるにtextBox1_KeyDownは、デザイン モードでテキスト ボックスをクリックし、プロパティウィンドウを見て、イベントボタンをクリックし、 keydownを探してダブルクリックします。ここに画像の説明を入力

于 2012-12-29T15:33:55.303 に答える