Forum guidelines
  • Home
  • »
  • C# Language
  • »
  • send keys can not run inside this application in windows
AuthorQuestion
send keys can not run inside this application in windows
Posted on: 29 Jun 2013
hi
any one can give solution for this error

SendKeys cannot run inside this application because the application is not handling Windows messages.  Either change the application to handle messages, or use the SendKeys.SendWait method

Thanks & Regards
Ravi Kumar

AuthorReply
Re: send keys can not run inside this application in windows
Posted on: 29 Jun 2013  
add Your Code so that i can understand problem
Re: send keys can not run inside this application in windows
Posted on: 29 Jun 2013  
It sounds like you're trying to use SendKeys.Send from something other than a Windows Forms application.

As the error message says, this method only works where the application handles windows messages. For any other type of  application (console, windows service, WPF etc) you have to use SendKeys.SendWait instead.

Re: send keys can not run inside this application in windows
Posted on: 10 Jul 2013  
hi

this is my code

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace VenkataTextBox
{
    [ToolboxItem(true)]

    public class VenkataTextBox : System.Windows.Forms.TextBox
    {
        //private System.Windows.Forms.Label lbl;
        //private System.Windows.Forms.ErrorProvider _errorProvider;
        ////-------------------- OverWrite -------------------------------   
        //#region ErrorField

        //private string _ErrorMessage = "";

        //[Category("New Properties"),
        // Description("Indicates whether the text in the edit control will be overwrited or not"),
        // DefaultValue("")]
        //public string ErrorMessage
        //{
        //    get { return _ErrorMessage; }
        //    set { _ErrorMessage = value; }
        //}

        [Category("New Properties"),
         Description("Indicates whether the text in the edit control will be overwrited or not"),
         DefaultValue(false)]
        private bool _isMandatory;
        public bool IsMandatory
        {
            get { return _isMandatory; }
            set { _isMandatory = value; }
        }

        //protected void VenkatatextBox_Error_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        //{
        //    //if (e.KeyCode == System.Windows.Forms.Keys.Enter && _bTabwithEnterKey)
        //    //{
        //    //    dynamic _obj = sender;
        //    //    if (_obj.AccessibleName != "PRIMARYKEY")
        //    //        System.Windows.Forms.SendKeys.Send("{TAB}");
        //    //}
        //}

        //#endregion

        //-------------------- OverWrite -------------------------------   
        #region OverWrite

        private bool _overWrite = false;

        [Category("New Properties"),
         Description("Indicates whether the text in the edit control will be overwrited or not"),
         DefaultValue(false)]
        public bool OverWrite
        {
            get { return _overWrite; }
            set { _overWrite = value; }
        }

        private void VenkatatextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (_overWrite && this.SelectedText == "")
            {
                this.SelectionLength = 1;
            }
        }

        #endregion

        //-------------------- TabwithEnterKey --------------------------
        #region TabwithEnterKey

        protected bool _bTabwithEnterKey = true;

        [Category("New Properties"),
         Description("Indicates whether the user can use the ENTER key Instead of TAB key " +
                     "to give focus to the next control."),
         DefaultValue(true)]
        public bool TabwithEnterKey
        {
            get { return _bTabwithEnterKey; }
            set { _bTabwithEnterKey = value; }
        }
        protected void VenkatatextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Enter && true)
            {
                dynamic _obj = sender;
                if (_obj.AccessibleName != "PRIMARYKEY")
                    //    this.GetNextControl((Control)sender, true).Focus();
                    System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

   
     
        #endregion

        //-------------------- SelTxtOnEnter --------------------------
        #region SelTxtOnEnter

        public enum SelectTxt { Always, Once, Never };
        protected SelectTxt _SelectTxt = SelectTxt.Never;
        protected bool _SelTxtAllowed = false;

        [Category("New Properties"),
         Description("Indicates how the text will be select when it is focused."),
         Browsable(true),
         EditorBrowsable(EditorBrowsableState.Always),
         DefaultValue(SelectTxt.Never),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public SelectTxt SelTxtOnEnter
        {
            get
            {
                return _SelectTxt;
            }
            set
            {
                _SelectTxt = value;
                _SelTxtAllowed = (value != SelectTxt.Never);
            }
        }

        protected void VenkatatextBox_Enter1(object sender, System.EventArgs e)
        {
            base.SelectionLength = 0;
            if (_SelectTxt != SelectTxt.Never || _SelTxtAllowed)
            {
                if (_SelTxtAllowed)
                {
                    base.SelectAll();
                    _SelTxtAllowed = true;
                }
                _SelTxtAllowed = (_SelectTxt == SelectTxt.Always);
            }
        }

        #endregion

        //-------------------- Focused Color --------------------------
        #region Focusedcolor

        protected System.Drawing.Color _FocusedBackColor;
        protected System.Drawing.Color _BackColor;
        protected System.Drawing.Color _FocusedForeColor;
        protected System.Drawing.Color _ForeColor;

        [Category("New Properties"),
         Browsable(true),
         EditorBrowsable(EditorBrowsableState.Always),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override System.Drawing.Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                _BackColor = value;
                base.BackColor = value;
            }
        }

        [Category("New Properties"),
         Browsable(true),
         EditorBrowsable(EditorBrowsableState.Always),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override System.Drawing.Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                _ForeColor = value;
                base.ForeColor = value;
            }
        }
        [Category("New Properties"),
         Description("The background color of the component when it is focused."),
         Browsable(true),
         DefaultValue("Window"),
         EditorBrowsable(EditorBrowsableState.Always),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public System.Drawing.Color FocusedBackColor
        {
            get
            {
                return _FocusedBackColor;
            }
            set
            {
                _FocusedBackColor = value;
            }
        }

        [Category("New Properties"),
         Description("The foreground color of this component when it is focused,which is used to display text."),
         Browsable(true),
         DefaultValue("WindowText"),
         EditorBrowsable(EditorBrowsableState.Always),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public System.Drawing.Color FocusedForeColor
        {
            get
            {
                return _FocusedForeColor;
            }
            set
            {
                _FocusedForeColor = value;
            }
        }

        protected void VenkatatextBox_Enter2(object sender, System.EventArgs e)
        {
            base.BackColor = _FocusedBackColor;
            base.ForeColor = _FocusedForeColor;
        }

        protected void VenkatatextBox_Leave(object sender, System.EventArgs e)
        {
            base.BackColor = _BackColor;
            base.ForeColor = _ForeColor;
        }

        #endregion

        //-------------------- NumericText -------------------------------   
        #region NumericText

        private bool _NumericText = false;

        [Category("New Properties"),
         Description("Text box can only write numeric values"),
         DefaultValue(false)]
        public bool NumericText
        {
            get { return _NumericText; }
            set { _NumericText = value; }
        }
               
        bool allowSpace = false;

        // Restricts the entry of characters to digits (including hex), the negative sign,
        // the decimal point, and editing keystrokes (backspace). when your starting
        protected void VenkataNumerictextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (_NumericText)
            {
                System.Globalization.NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
                string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
                string groupSeparator = numberFormatInfo.NumberGroupSeparator;
                string negativeSign = numberFormatInfo.NegativeSign;

                string keyInput = e.KeyChar.ToString();

                if (Char.IsDigit(e.KeyChar))
                {
                    // Digits are OK
                }
                else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
                    keyInput.Equals(negativeSign))
                {
                    // Decimal separator is OK
                }
                else if (e.KeyChar == '\b')
                {
                    // Backspace key is OK
                }
                //    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
                //    {
                //     // Let the edit control handle control and alt key combinations
                //    }
                else if (this.allowSpace && e.KeyChar == ' ')
                {

                }
                else
                {
                    // Swallow this invalid key and beep
                    e.Handled = true;
                    //    MessageBeep();
                }
            }
        }

        public int IntValue
        {
            get
            {
                return Int32.Parse(this.Text);
            }
        }

        public decimal DecimalValue
        {
            get
            {
                return Decimal.Parse(this.Text);
            }
        }

        public bool AllowSpace
        {
            set
            {
                this.allowSpace = value;
            }

            get
            {
                return this.allowSpace;
            }
        }
        #endregion
       
        //-------------------- Class Constructor --------------------------
        public VenkataTextBox()
        {
            //if (!IsMandatory)
            //{
            //    _errorProvider = new System.Windows.Forms.ErrorProvider();
            //    this._errorProvider.SetError(this, "Mandatory Field");
            //}
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.VenkatatextBox_KeyPress);
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.VenkataNumerictextBox_KeyPress);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.VenkatatextBox_KeyDown);
            this.Enter += new System.EventHandler(this.VenkatatextBox_Enter1);    
            //this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.VenkatatextBox_Error_KeyDown);           
            //this.Enter += new System.EventHandler(this.VenkatatextBox_Enter2);
            //this.Leave += new System.EventHandler(this.VenkatatextBox_Leave);
        }
    }

}


Thanks & Regards
Ravi Kumar

Re: send keys can not run inside this application in windows
Posted on: 10 Jul 2013  
hi
yes i am using wcf application and windows form in same project
there i am getting sendkeys error

Thanks & Regards
Ravi Kumar

Re: send keys can not run inside this application in windows
Posted on: 10 Jul 2013  

Hi,

Try to Include "System.Windows.Forms" then after try this ,May be its help you 

System.Windows.Forms.SendKeys 


 
 



kingskunal If this post is useful then mark it as "Accepted Answer"
Re: send keys can not run inside this application in windows
Posted on: 10 Jul 2013  
Well, it looks like you're creating a custom textbox, probably in a separate dll, with a view to placing it in the ToolBox.

If that's the case, then there will be no message loop running because the textbox hasn't yet been placed on a form.

You'll therefore need to use SendKeys.SendWait instead of SendKeys.Send to get rid of the error.



Re: send keys can not run inside this application in windows
Posted on: 11 Jul 2013  
hi
i changed sendkeys.send("{TAB}") to sendkeys.sendwait("{TAB}")
same error coming

Thanks & Regards
Ravi Kumar

Re: send keys can not run inside this application in windows
Posted on: 11 Jul 2013  
Just to be clear, are you saying that even when you change it to SendKeys.SendWait("{TAB}") the error message is still:

"SendKeys cannot run inside this application because the application is not handling Windows messages.  Either change the application to handle messages, or use the SendKeys.SendWait method."

Re: send keys can not run inside this application in windows
Posted on: 11 Jul 2013  
hi
sendkeys.send("{TAB}") i changed to SendKeys.SendWait("{TAB}") the error message is still:
yes coming same error

SendKeys cannot run inside this application because the application is not handling Windows messages.  Either change the application to handle messages, or use the SendKeys.SendWait method.


Thanks & Regards
Ravi Kumar

Re: send keys can not run inside this application in windows
Posted on: 11 Jul 2013  
Are you sure that you've changed both occurrences of SendKeys.Send in your code to SendWait?

If you have, then the error is very difficult to fathom.

Which version of Windows and .NET are you using?

Re: send keys can not run inside this application in windows
Posted on: 11 Jul 2013  
hi
i am using .net frame work 4.

the send key error message is showing in wcf form




Thanks & Regards
Ravi Kumar

Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.