Bassam Badr
Bassam Badr

Reputation: 667

Read an input key from user

I want to read input from the user while my form is active.

For example if a form is active, when I press F1 I expect that a new form will appear.

How can I do that?

This is my main form

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;
using DevComponents.DotNetBar;

namespace SchoolManagmentSystem.Forms
{
    public partial class AddStudent : Office2007Form
    {
        public AddStudent()
        {
            InitializeComponent();
        }



        private void buttonX3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddStudent_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.F3)
                MessageBox.Show("hi");
        }        
    }
}

In my design file I have a textbox.

Upvotes: 1

Views: 119

Answers (1)

Tom Studee
Tom Studee

Reputation: 10452

private void AddStudent_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.F1) 
    {
       // do your stuff
    }
}

Then in your constructor add the following:

public AddStudent()
{
    InitializeComponent();
    this.KeyDown +=new System.Windows.Forms.KeyEventHandler(AddStudent_KeyDown);
}

Upvotes: 3

Related Questions