How to code KeyPressed to detect KeyDown in C#

I am trying to code the following in C#

protected override void OnKeyPress(KeyPressEventArgs e)
        {
            MessageBox.Show("Debug 0");
            base.OnKeyPress(e);
        }

The code compiles, but when you run it, the MessageBox doesn't show when you type a KeyDown, so something is not right yet.

Can anyone tell me what I did wrong in this example?
Leonard GojerCAD Technician/ProgrammerAsked:
Who is Participating?
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
keypress is only valid for regular ascii characters (letters, digits, space, ...).

for other characters (like arrows), you need to use KeyDown or KeyUp
0
 
Éric MoreauSenior .Net ConsultantCommented:
have you initialized the event handler?

YourControlName.KeyPress += new KeyPressEventHandler(OnKeyPress);



check http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
0
 
Leonard GojerCAD Technician/ProgrammerAuthor Commented:
I tried to add the following two lines to the Form. They did not compile. The compiler
says that "KeyPressEventArgs()" does not take 0 arguments. If you can't answer this quickly,
then I will close the ticket now that I have your documentation link from MSDN.

Thank you for your help.


KeyPressEventArgs e1 = new KeyPressEventArgs();
 treeView1.KeyPress += new KeyPressEventHandler(CMETracker1.HelpForm.OnKeyPress(e1));
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Éric MoreauSenior .Net ConsultantCommented:
>>KeyPressEventArgs e1 = new KeyPressEventArgs();
>> treeView1.KeyPress += new KeyPressEventHandler(CMETracker1.HelpForm.OnKeyPress(e1));

remove line 1
line 2 shoud be: treeView1.KeyPress += new KeyPressEventHandler(OnKeyPress);
0
 
Leonard GojerCAD Technician/ProgrammerAuthor Commented:
That doesn't compile. I think that you're getting closer to the correct solution, but that something little is missing.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Post more complete code...
0
 
Leonard GojerCAD Technician/ProgrammerAuthor Commented:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CMETracker1
{
    public partial class HelpForm : Form
    {
        public const int max1 = 200;

        public string[] notesdata = new string[max1];

        public int treepos = 1;

        public bool firstOpen = false;

        public bool keydownpressed = false;

        public bool keyuppressed = false;

        public HelpForm()
        {
           
            InitializeComponent();
        }

        private void HelpForm_Load(object sender, EventArgs e)
        {
            //treeView1.KeyPress += new KeyPressEventHandler(OnKeyPress);
            StreamReader fileStream = new StreamReader("HelpItems.txt");
            string str1 = "";
            string indexstr = "";
            string notestr = "";
            int i1 = 0;
            for (i1 = 0; i1 < max1; i1++)
            {
                notesdata[i1] = "";
            }
            i1 = 0;
            try
            {
                while (!fileStream.EndOfStream)
                {
                    try
                    {
                        // read from file or write to file
                        str1 = fileStream.ReadLine();
                        indexstr = str1;
                        i1 = i1 + 1;
                        str1 = fileStream.ReadLine();
                        str1 = "";
                        notestr = "";
                        while (!str1.Equals("$"))
                        {
                            str1 = fileStream.ReadLine();
                            if (!str1.Equals("$"))
                            {
                                notestr = notestr + str1;
                            }
                        }
                        treeView1.Nodes.Add(indexstr);
                        notesdata[i1] = notestr;
                        firstOpen = true;
                    }
                    finally
                    {
                        if (fileStream.EndOfStream)
                        {
                           
                        }
                    }
                }
            }
            finally
            {
                fileStream.Close();
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            MessageBox.Show("Debug 0");
            base.OnKeyPress(e);
        }

        private void treeView1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {

            if (keydownpressed)
            {
                MessageBox.Show("Debug 1");
            }
           
        }

        private void treeView1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            keyuppressed = true;
            MessageBox.Show("Debug 2");
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (firstOpen)
            {
                string notestr = "";
                notestr = notesdata[treepos];
                richTextBox1.Text = notestr;
                firstOpen = false;
            }
            else
            {
                if (keydownpressed)
                {
                    treepos = treepos + 1;
                }
                else if (keyuppressed)
                {
                    treepos = treepos - 1;
                }
                firstOpen = true;
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
        }
    }
}
0
 
Éric MoreauSenior .Net ConsultantCommented:
why don't you just use the properties dialog of the form to link the event to the method? or check the .designer.cs file for the wiring of the KeyUp event.
0
 
Leonard GojerCAD Technician/ProgrammerAuthor Commented:
I know how to go to the properties dialog, but I don't know how to do exactly what you are saying when I get there. If I can't answer this problem in a few more tries, I am going to close it, because you have given me a big head start where to look for these things.
0
 
Éric MoreauSenior .Net ConsultantCommented:
when you are in the properties dialog, there is a lightning at the top that let you switch to the events. select the KeyPress event and select the OnKeyPress method beside it.
0
 
Leonard GojerCAD Technician/ProgrammerAuthor Commented:
OK. We almost have this problem solved. That works for characters like "m" or "a" etc...
but it doesn't work for the "Down" arrow, which is what I need it for. What do you suggest?
0
 
Leonard GojerCAD Technician/ProgrammerAuthor Commented:
Thank you very much. I can see that you are very professional with Visual Studio C#.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.