0

I used Winform in C# Visual Studio. At first I had an OnKeyDown and it worked well. After this i added a button to the form. I dragged the button from the toolbox and doubleclicked on it. Then it generated me the public void function.

The Button is also working fine now, but i cant use the KeyEvent anymore. What do i have to change?

protected override void OnKeyDown(KeyEventArgs e)
{
    Debug.WriteLine("hello Keyevent");
}

private void button1_Click_1(object sender, EventArgs e)
{
    Debug.WriteLine("hello button");
}

Paint-Method:

protected override void OnPaint(PaintEventArgs e)
{
    grafik = e.Graphics;
    using (Font font1 = new Font("Arial", 20, FontStyle.Regular, GraphicsUnit.Point))
    {
        StringFormat TextFormat = new StringFormat()
        {
            Alignment = StringAlignment.Center,
        };

        Rectangle myRectangle = new Rectangle();

        for (int i = 0; i < maze.GetLength(0); i++)
        {
            for (int j = 0; j < maze.GetLength(1); j++)
            {
                string sign = char.ToString(Labyrinth[i, j]);
                myRectangle.Location = new Point(i * 30, j * 30);
                myRectangle.Size = new Size(10, 10);

                grafik.DrawString(sign, font1, red, myRectangle, TextFormat);
            }
        }
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Da_Pusti
  • 5
  • 3
  • Do you need to handle all key presses in the window or only specific (for some button or other controls)? – Serg Jun 26 '21 at 17:39
  • i just want to press the arrowKeys. Then a symbol on my form should move then (up, down, left, right). Just wanted to test with the Debug.Writline if the form will understand that i move the keys and if i press the button. – Da_Pusti Jun 26 '21 at 17:49
  • You should assign 'KeyDown' event to some control. Then when you would have focus to specific control then that event will be triggered. Same as we assign click event to button and event is triggered when we click that specific button. – Ishtiaq Jun 26 '21 at 17:50
  • What do you mean by ... _"a symbol on my form should move"_ ... ? What is this "symbol" and what kind of control is it. Technically any control on the form that has focus will receive the Key Down event instead of the form itself. – JohnG Jun 26 '21 at 17:53
  • I just created an 2D-Array. in this array a maze is drawn with "X" as border an a "O" as player. i can move the player in my maze with changing the values on my 2D-Array and redraw it with "refresh()". The OnPain-method will then be called. it´s all working fine and i can move it. Just after i add a button in my form only the button is working now. – Da_Pusti Jun 26 '21 at 17:57
  • So there are no other controls on the form other than the button you just added? Can you show the paint method? – JohnG Jun 26 '21 at 18:09
  • yep right, no other controls. i added the paint-method to my post – Da_Pusti Jun 26 '21 at 21:24
  • i´m not sure if its maybe better to set a control panel and draw into the panel and add the button next to it. i´m rly new to c# and winform – Da_Pusti Jun 26 '21 at 21:44
  • when you drag button control from toolbox and double click on it, then it generated only click event. If you want keydown event then you have to generate it. – Amit Verma Jun 27 '21 at 03:40
  • i still dont rly get it sry. I have a form. in this form i draw many letters. the letters are in an array. My On-Paint-method displays the array (coordiantes+values). I could then build the OnKeyDown - Event and everything ist fine. it always knows which button is pressed. then i drag and dropped a button in my form and it wont work anymore. i also tried something new now: i went to the Events on my form (KeyDown, Key Press etc.). and generated a method. the method now displays my output but only if i press space and space is pressing the button. it somehow connected with the button – Da_Pusti Jun 27 '21 at 11:36
  • i also realized something new now, maybe it helps: i added a new button and wenn ich press the arrowkeys i can only switch between the keys. so the keys are only realized as switching between the buttons and no more for my keyPress event – Da_Pusti Jun 27 '21 at 11:42

1 Answers1

0

I think i got it now. I just used the method:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

instead of

protected override void OnKeyDown(KeyEventArgs e)

I can now control my Form with the ArrowsKeys

Da_Pusti
  • 5
  • 3