简体   繁体   中英

trigger a keyup without pressing a key C#

i wanna trigger a keyup event just by calling the form and not pressing a key

Heres my code:

if (dr.HasRows)
                {
                    FrmQty fqty = new FrmQty(this);
                    fqty.ProductDetails(dr["ProductID"].ToString(), float.Parse(dr["ProductPrice"].ToString()), lblTransno.Text);
                    fqty.txtQty.Text = "1";
                    fqty.txtQty_KeyUp();//i wanna trigger it here by pressing "Enter" key. its an event from FormQty
                    fqty.ShowDialog();
                }

One (probably better) solution is to make a handler function public and simple call it. Another may be a simulation of key press using SendKeys :

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys?view=net-5.0

You can do:


   private void myForm_KeyDown(object sender, KeyEventArgs  e)
   {

      if(e.KeyCode == Keys.Enter)
           MyEnterKeyDownFunc();
      else
      {
        //your other keys management
      }
   }

   public void MyEnterKeyDownFunc()
   {
      //do your enter key down stuff here

       // if you want to modify anything on the UI, you should make sure it 
       // is invoked in the GUI thread. Maybe something like:
    if (aControl.InvokeRequired)
     {
         aControl.BeginInvoke((MethodInvoker)delegate ()
            {
                // GUI modifications
            });
      }
     else
     {
         // GUI modifications
      }      
   }


Now you can call MyEnterKeyDownFunc() from outside your class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM