Reputation: 131
What am I doing wrong? Here is the error code that I get:
'System.EventArgs' does not contain a definition for 'KeyCode' and no extension method 'KeyCode' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)
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();
}
}
}
Upvotes: 2
Views: 11427
Reputation:
To assign KeyDown
event of textBox1
to textBox1_KeyDown
, in design mode click on text box and look in properties window and click on events button and look for keydown and double click on it.
Upvotes: 0
Reputation: 39393
To maximize your IDE use (i.e. Visual Studio), type this: textBox1.KeyDown += TabTab
It will give you the exact signature of the event's delegated method definition.
If your KeyDown's code is very trivial and you don't want to put it on separate method, you can opt to inline the code, i.e. you can embed the code in lambda:
textBox1.KeyDown += (s,e) =>
if (e.KeyCode == Keys.Enter)
webBrowser1.Navigate(
"https://www.google.com/search?&ie=UTF-8&q=" + textBox1.Text);
Auto-complete still applies with lambda, i.e. when you type this: if (e.
, the KeyCode
will appear on auto-complete's dropdown list. With lambda, you don't need to know the exact signature of the delegated method.
Upvotes: 2
Reputation: 131
Update: I figured it out. All you have to do is change
(object sender, EventArgs e)
to
(object sender, KeyEventArgs e)
Upvotes: 4