• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

KeyPress event stop working when i swith betwen two tabs or forms....

Hi

my keypress event stop working when i switch betwen two tab or forms in my application, it sems like the form lose focus or something, and i have tried to set focus back to the from, panel, control and so on...but nothing helps...

It is a textfields in a flowlayoutpanel on the form for searching text, amount and so on.....
What shoud i do to so it fire up the keypress event again or how can i get my focus back on that form..?

below i have pasted my keypress event code.....
private void searchTextDecimalFieldAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                ((MainForm)this.ParentForm).SetBlockOnApplication();
                SearchDb();
            }
        }

Open in new window

0
Adnan
Asked:
Adnan
  • 4
  • 2
1 Solution
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Did you tried enabling the form KeyPreview property? then capturing the form KeyDown event? something like:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (searchTextDecimalFieldAmount.Focused && e.KeyCode == Keys.Enter)
    {
        ((MainForm)this.ParentForm).SetBlockOnApplication();
        SearchDb();
    }
}

Open in new window

0
 
AdnanAuthor Commented:
oki, i tried with PrewievKeyDownEvenHandler but still it wont work...if i write something in textbox then it fires up, but if i write something in textbox and hit enter button then it wont fire it up....and its every time i switch between my Tabs in applicatyion....
public SearchControl()
        {
            InitializeComponent();
            if (this.DesignMode) return;

            this.DoubleBuffered = true;
            this.BorderStyle = BorderStyle.None;
            myDetailColumn = ColumnDetails.Index;
            //cmbOpenReconciled.SelectedIndex = 2;
            foreach (Control ctrl in flowLayoutPanel1.Controls)
            {
                if (ctrl.GetType().IsSubclassOf(typeof(SearchField)) || ctrl.GetType() == typeof(SearchField) || ctrl.GetType() == typeof(SearchTextDecimalField))
                {
                    ((SearchField)ctrl).ValueChanged += new EventHandler(searchTextField_ValueChanged);
                }
                if (ctrl.GetType() == typeof(SearchTextField))
                {
                   
                    ((SearchTextField)ctrl).textBox1.KeyPress += new KeyPressEventHandler(searchTextDecimalFieldAmount_KeyPress);
                    ((SearchTextField)ctrl).textBox1.PreviewKeyDown += new PreviewKeyDownEventHandler(searchTextDecimalFieldAmount_PreviewKeyDown);
                }
                if (ctrl.GetType() == typeof(SearchTextDecimalField))
                {
                    ((SearchTextField)ctrl).textBox1.KeyPress += new KeyPressEventHandler(searchTextDecimalFieldAmount_KeyPress);
                    ((SearchTextField)ctrl).textBox1.PreviewKeyDown += new PreviewKeyDownEventHandler(searchTextDecimalFieldAmount_PreviewKeyDown);
                }
            }
}

Open in new window

0
 
AdnanAuthor Commented:
i have one search tab to search transactions....and a textbox for search string....!! every thing works fine until i change the tab to another tab and back again to my search tab then it wont fire up when i hit the enter button...but if i write something in textbox then it works fine....??!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi, dont use PrewievKeyDown Event use KeyDown event! follow my example.
0
 
AdnanAuthor Commented:
((SearchTextField)ctrl).textBox1.KeyDown += new KeyEventHandler(searchTextDecimalFieldAmount_KeyDown);
0
 
AdnanAuthor Commented:
or here is my solution,

private void flowLayoutPanel1_VisibleChanged(object sender, EventArgs e)
        {
            
          
            foreach (Control ctrl in flowLayoutPanel1.Controls)
            {
                if (ctrl.GetType() == typeof(SearchTextField))
                {
                    //((SearchTextField)ctrl).textBox1.PreviewKeyDown += new PreviewKeyDownEventHandler(searchTextDecimalFieldAmount_PreviewKeyDown);
                    ((SearchTextField)ctrl).textBox1.KeyDown += new KeyEventHandler(searchTextDecimalFieldAmount_KeyDown);
                }
                if (ctrl.GetType() == typeof(SearchTextDecimalField))
                {
                    //((SearchTextField)ctrl).textBox1.PreviewKeyDown += new PreviewKeyDownEventHandler(searchTextDecimalFieldAmount_PreviewKeyDown);
                    ((SearchTextField)ctrl).textBox1.KeyDown += new KeyEventHandler(searchTextDecimalFieldAmount_KeyDown);
                }
            }
        }

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now