Zignd
Zignd

Reputation: 7025

How to set Focus on the Form?

I'm trying the get the inputted values from the user and save it on a char variable, but the problem is that nothing occurs, and I think the problem is with the Form Focus, this is the code, and when I run it no errors occurs, but also nothing happen. What I did wrong?

        char keyPressed;

        public FrmZigndSC()
        {
            InitializeComponent();
            this.Focus();
        }

        private void FrmZigndSC_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            keyPressed = e.KeyChar;

            LblResult.Text += Convert.ToString(keyPressed);
        }

Upvotes: 2

Views: 18527

Answers (6)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can try with this code - Based on KeyPressEventHandler

public FrmZigndSC()
{
       InitializeComponent();
       this.Focus();

       //Subscribe to event
       this.KeyPress += new KeyPressEventHandler(FrmZigndSC_KeyPress);
}


private void FrmZigndSC_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
        keyPressed = e.KeyChar;
        LblResult.Text += Convert.ToString(keyPressed);


        // Indicate the event is handled.
        e.Handled = true;
}

Upvotes: 2

Matthias Meid
Matthias Meid

Reputation: 12521

I tried to reproduce it in an empty little Windows Forms project. This code worked just fine without the Shown event handler:

public partial class FrmZigndSC : Form
{
    public FrmZigndSC()
    {
        InitializeComponent();

        this.KeyPress += (s, e) => this.LblResult.Text += e.KeyChar.ToString();

        // this might be a solution, but i did not need it
        this.Shown += (s, e) => this.Activate();
    }
}

You could try to use this.Activate() anyway and see if it helps. If you got other input controls such as text boxes on your form, try setting the form's KeyPreview property to true.

Upvotes: 1

Tigran
Tigran

Reputation: 62265

If you want to recieve a key notification from the application'e mesage pipeline, relaying on focus of the element, in this case, make architecture fragile. You can not gurantee that from other forms in your app that one would be focused, or the form is nto covered by some control that absorbes that event. You can not forse to a form having a focus, cause it's completely bad UX design (not very sure even if this is possible to implement in 100% working way).

What you can do it, instead, is declare class derived from IMessageFilter:

public class MessageFilter : IMessageFilter
{
        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;

        public bool PreFilterMessage(ref Message m)
        {
            // Intercept KEY down message
            Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;

            if ((m.Msg == WM_KEYDOWN)
            {
                 //get key pressed  

                 return true;
            }        
            else
            {
                return false;
            }


        }
}

and after register it within your application:

MessageFilter filter = new MessageFilter(); //init somewhere 


Application.AddMessageFilter(filter ); // add 

..... 

//on application end don't forget to remove it 
Application.RemoveMessageFilter(filter );

Upvotes: 1

Anish V
Anish V

Reputation: 683

The code you have given is working fine for me. Set the startup page as FrmZigndSC and try again.

Upvotes: 0

Jordy van Eijk
Jordy van Eijk

Reputation: 2766

I Think the problem is that when you have differenct controls on your Form that catch ketpressed. I had the same probmel with an DateTimePicker on my control.

Try to delete all of them and then try it, it will work. And from that point add the controls again to see which on is the problem.

Upvotes: 0

MrEdge
MrEdge

Reputation: 74

Using the FocusManager instead of this.Focus() should do the trick!

http://msdn.microsoft.com/en-us/library/system.windows.input.focusmanager.aspx

Upvotes: 0

Related Questions