Click here to Skip to main content
15,886,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have MDI form and child of MDI form,
when user press ALT key,it displays shortcut letters as tooltip for picture box on form (as like when we press Alt in MS-Word).
i have used following two events of form & set KeyPreview=true:-

C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.Alt)
           {
               toolTip1.Show("U", this, pic1.Location);
               toolTip2.Show("K", this, pictureBox2.Location);
               toolTip3.Show("L", this, pictureBox3.Location);
           }
       }


protected override void OnKeyPress(KeyPressEventArgs ex)
       {
           MessageBox.Show(ex.KeyChar.ToString());
           string xo = ex.KeyChar.ToString();
           if (xo == "k") //You pressed "q" key on the keyboard
           {
               MessageBox.Show("K pressed");
           }
           else if (xo == "j") //You pressed "q" key on the keyboard
           {
               MessageBox.Show("Chart control event pressed");
           }
       }


When alt key is pressed fires Form1_KeyDown event
& shows all tooltip letters for assigned picture box,
after that if i press 'k' key from keyboard, it will not fires OnKeyPress event,
then again i press 'k' key from keyboard, it will fires OnKeyPress event.
after that all works fine.
Why this happens for MDI form.
Posted

There's a much easier way to do this that doesn't require over-riding 'OnKeyPress. Assume the KeyDown and KeyUp EventHandlers shown below are wired-up to your MDIChildForm, and that its KeyPreview Property is set to 'true.
C#
private bool IsAltDown = false;
private bool IsToolTipsShown = false;

private List<Keys> keyList = new List<Keys> { Keys.K, Keys.L, Keys.U };

private void Form2_KeyDown(object sender, KeyEventArgs e)
{
    IsAltDown = Control.ModifierKeys == Keys.Alt;

    if (IsAltDown)
    {
        toolTip1.Show("U", this, pictureBox1.Location);
        toolTip2.Show("K", this, pictureBox2.Location);
        toolTip3.Show("L", this, pictureBox3.Location);

        IsToolTipsShown = true;
    }

    if (IsAltDown && keyList.Contains(e.KeyCode))
    {
        switch (e.KeyCode)
        {
            case Keys.K:
                textBox1.Text = "K";
                break;
            case Keys.L:
                textBox1.Text = "L";
                break;
            case Keys.U:
                textBox1.Text = "U";
                break;
        }

        textBox1.Text += " pressed with Alt Key Down";
    }
}

// when the Alt key is released hide the ToolTips
private void Form2_KeyUp(object sender, KeyEventArgs e)
{
    if(IsToolTipsShown && ! (Control.ModifierKeys == Keys.Alt))
    {
        toolTip1.Hide(this);
        toolTip2.Hide(this);
        toolTip3.Hide(this);

        IsToolTipsShown = false;
    }
}
You'll note that I wrote a message to a TextBox on the Form rather showing a MessageBox: that's done to avoid the sometimes strange side-effects of "going modal" (suspending the current thread) when developing and intercepting events like Key Events.

I also used the Controls.ModifierKeys Property to determine whether the Alt Key is down, or up.
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 2-Jan-14 4:37am    
5!!,
Bill,
all your answers are neat and explanatory.
you can resolve it by using Process CMD
 
Share this answer
 
Comments
RhishikeshLathe 25-Dec-13 5:21am    
i have used this, it behaves as it is.
agent_kruger 25-Dec-13 5:30am    
can you post the code of Process CMD?
For this set following property of MDI Form:-

MainMenustrip=menustrip1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900