Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a textbox (txtStaffid) on my form (frmStaff) which searches for staff details when the enter key is pressed. there is another form (frmMain) which have
a menustrip (Add Staff, and Update Staff). when Add Staff or Update Staff is clicked
frmStaff is shown but with different buttons on it. now i want txtStaffid sholud
respond to the enter key only if Update Staff is clicked on the menustrip.
i tryed this on frmStaff but did not work
pls help me out


C#
private void txtStaffid_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            frmMain fam = new frmMain();

            if (fam.addStaffDetailsToolStripMenuItem1.Selected == true)
            {
                return;
            }

            else
            {
                if (e.KeyCode == Keys.Enter)
                {
                
                searchStaff();
                }
            }
        }
Posted

You are creating a new instance of frmMain in your keydown handler. This
fam.addStaffDetailsToolStripMenuItem1.Selected
will always be the same value that is the default setting during design time when you instantiate a brand new form object.

Personally I would add a parameterized constructor into the child form frmStaff and pass in a value to determine the mode of the form. e.g.
frmStaff frmS = new frmStaff("addMode");


If you don't want to do that you will need to try and get the parent form and do a type check to see if it is frmMain then cast it as frmMain to access the child controls that are either internal or public accessible.
 
Share this answer
 
The question makes no sense because a menu item is stateless. "Is clicked" cannot be a state of some object which you could check up in your handler. So, instead, you need a control which have a Boolean state, such as a check box or a checked menu item.

Of course, creation of a form in the event handler makes no sense and shows your deep misunderstanding of a lot of things. I don't know how to help you with that except an advice to go back to more elementary programming stuff, to understand how types, their methods and other member work, what are roles of the instances, types, static and instance members, etc.

If the code you show is the one of the main form, the instance of the form is "this". If this is not, your main form should pass its reference to your frmStaff form, which should remember this reference at all times.

—SA
 
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