'System.EventArgs' is a 'type' but is used like a 'variable'

Whats wrong with my code below Can you correct it

private void txtusername_TextChanged(object sender, EventArgs e)

{

if (EventArgs (e) == Keys.Enter)

this.txtpassword.Focus();

}

}




Answer this question

'System.EventArgs' is a 'type' but is used like a 'variable'

  • RJ

    hi,

    you can try the key code


    public Form1()
    {
    InitializeComponent();
    this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
    }
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar == 13)
    {
    textBox2.Focus();
    }
    }


    hope this helps



  • peter_griffin

    I know why.Did you subsribe to the KeyPress event

    this.txtusername.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtusername_KeyPress);

    Write this code at the very end of the InitializeComponent method.


  • Xface

    Try the KeyPressed Event and it will work fine.

    private void txtusername_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

    {

    if(e.KeyChar == (char)Keys.Enter)

    {

    Console.Write("a");

    }

    }

    Let me know if this is helpful.


  • Ahmed_khemiri

    Still doesn't work, this block of code doesn't executed.

    {

    if (e.KeyChar == 13)

    {

    textbox2.focus();

    }

    }

    But if I removed a pair of curly brackets like the code below, it was executed.

    {

    if (e.KeyChar == 13)

    textbox2.focus();

    }

    Why like that whats really wrong with the given solutions Or does my form has problem I'll try another new form..



  • Ali Gullen

    hi,

    you didn't miss anything simply your removed the useless curly brackets . those brackets for class declaration

    class Form2 : Form

    {

    }

    because you didn't take the form declaration you shouldn't copy those pair of brackets as well

    also when you use if statment you supose to write it like this

    if ( condition is true)

    {

    //block of code here

    }

    but if your block of code is one line you may don't use curly brackets like this

    if(condition is true)

    //one line

    but its a good habit to use them always

    what still remain is that the method pair of curly brackets

    public void keypress(object sender, keypresseventargs e)

    {

    //if statment

    }

    hope this helps



  • Chequer

    I have it on the InitializeComponent() method but still doesn't work. What could be the reason



  • Tijntje

    Karthik is correct, however I'll just add that your syntax is wrong because you specify EventArgs (which is a type ) as if it was a variable ( although you really specify it as if it was a function and you were calling it, passing e as an argument ).

    if (EventArgs (e) == Keys.Enter)

    As you've been shown, you would access e, and it's properties, you wouldn't specify the type again like this, brackets or no brackets.



  • HighDesert_NM

    I had created a new form and the code works. I think im missing some thing with my previous form.

    Thanks.



  • Petulka7

    Thanks for the reply, but how can I get to the txtusername_keypress method I cant see it from the code window, e.g. formname.cs.

    Regards



  • SilentAcorn

    Here is my revised code but when I press ENTER key, nothings happens, it doesn't move to the next control.

    private void txtusername_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

    {

    if(e.KeyChar == (char)Keys.Enter)

    {

    this.txtpassword.Focus();

    }

    }



  • 'System.EventArgs' is a 'type' but is used like a 'variable'