Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Advance Search with text box & datePicker and 1 search button with IsDefault = true
Now if my focus is on Text Box and i hit ENETER ..btnSearchClick event will fire
but if my focus is on DateTimePicker and i hit ENETER ..nothing happens.
This is a common control and will be used with other forms
and my btnSearchClik event is on respective forms.

What I have tried:

DatePicker & Enter Key should call my Default event
Posted
Updated 24-Feb-16 8:07am
Comments
Er. Dinesh Sharma 24-Feb-16 10:07am    
Your application is window form or asp.net.
Richard MacCutchan 24-Feb-16 10:08am    
Have you set the AcceptButton property on your form to the Search Button id?
CHill60 24-Feb-16 10:33am    
By the way the "What I have tried:" section is there for you to enter the code that you have already tried
Priyank Kharote 24-Feb-16 10:43am    
Its Wpf Application.
This xaml is basically in a Library with textBoxes & DatePickers
& Using this in different xamls.
Priyank Kharote 24-Feb-16 10:47am    
I dont want to set Tab's
I want my form to activate Default button's event on pressing ENETER key.
Its working fine if my focus is on Textbox but its not working if my focus is in datepicker

If you have window application then set tab index of each control in which you move focus.Then
write the following code.
C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
      {
          if (e.KeyCode == Keys.Enter)
          {
              e.SuppressKeyPress = true;
              SendKeys.Send("{TAB}");
          }
      }

make sure Form has one property KeyPreview=True
 
Share this answer
 
v3
Comments
Priyank Kharote 24-Feb-16 10:47am    
I dont want to set Tab's
I want my form to activate Default button's event on pressing ENETER key.
Its working fine if my focus is on Textbox but its not working if my focus is in datepicker
Start a new WinForms project, throw a Button and a TextBox on the Form. Run the project. Put the text-edit cursor in the TextBox, and then hit 'Enter: the Button will not be clicked: that's default behavior.

So, why do you think it is that when your focus is on the TextBox and you hit Enter that the Button gets the Click ? It's doing that either because you implemented that at design-time, or run-time (code) ... or, someone implemented that.

There are three things you need to study here to figure out why this is happening in your code:

1. The Form's 'AcceptButton Property

2. The TextBox's 'MultiLine Property

3. The TextBox's 'AcceptsReturn Property

And, now the 'DateTimePicker; try this:

C#
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.Return) button1.PerformClick();
}
You'll notice that this will trigger the Button Click when the DateTimePicker DropDown is "closed," and the control has Focus.

What about the user hitting return while the DTP DropDown is exposed: well ... now, it's time for you to do some study.

I want you to create a test form with a DateTimePicker, and create bound Event Handlers for the 'KeyDown, 'KeyUp, 'PreViewKeyDown, and 'KeyPress Events of the DateTimePicker.

In each of those handlers, write to the Console, or to a TextBox on the Form, the value of the key event ('KeyCode for 'KeyDown, 'KeyUlp, and 'PreViewKeyDown ... 'KeyChar for 'KeyPress).

Also wire-up a handler for the 'ValueChanged Event of the DateTimePicker that will write output to the Console, or to a TextBox.

Now, start the project, and try doing a few things to the DateTimePicker Control:

1. click on its upper area, above the drop-down, and hit return: observe the output

2. cause the Calendar to drop-down, and then with the mouse hovering over a calendar day, hit return.

The idea here is to educate yourself about the behavior of this ... or any other WinForm Control.

Guess what ... they do not behave consistently; these old built-in Controls, valuable as they are, all have their little quirks.

For example, it is common in Windows for hitting Enter while the mouse is over some object to generate a selection of that object, and trigger a selection event; in the Calendar DropDown of the DateTimePicker, you will not get a 'ValueChanged event raised when you hit 'Enter.

Think of yourself as an "astronaut" visitng different planets as you get to know the TextBox, the ListBox, the ListView, the TreeView, etc. :)
 
Share this answer
 
Comments
Priyank Kharote 2-Mar-16 8:54am    
Thank You Bill
I have

<grid name="Grid1" visibility="Visible" margin="0,0,0,3">
<grid.columndefinitions>
<columndefinition width="Auto">
<columndefinition width="Auto">
<columndefinition width="*">

<combobox name="cbColumns1" grid.column="0" width="175" margin="0,0,3,0"
="" selectionchanged="cbColumns1_SelectionChanged" lostfocus="cbColumns1_LostFocus" height="20" verticalcontentalignment="Center" verticalalignment="Center">
<combobox name="cbSearchBy1" grid.column="1" width="150" margin="0,0,3,0" height="20" verticalcontentalignment="Center" verticalalignment="Center">
<textbox name="txtSearchValue1"
="" grid.column="2" horizontalalignment="Stretch" charactercasing="Upper" textchanged="txtSearchValue1_TextChanged">
<combobox name="cbSearchValue1" grid.column="2" width="311" margin="0,0,1,0" horizontalalignment="Stretch" selectionchanged="cbSearchValue1_SelectionChanged">
<datepicker name="dtpSearchValue1" grid.column="2" width="313" height="24" margin="0"
="" horizontalalignment="Stretch" verticalalignment="Top" verticalcontentalignment="Center" selecteddatechanged="dtpSearchValue1_SelectedDateChanged" keyboard.previewkeyup="dtpSearchValue1_PreviewKeyDown">




ANd i am using this as reference in my forms Xaml.
Every xaml have a Button for Search & i am setting it to IsDefault.
DatePicker does note take Enter .. it validates the input on Enter.

you said about event .. i can manage that but my Function is on individual form .

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